CMakeLists.txt文件添加日志时出现了CMake错误(target_link_libraries)。

3

我有一个使用NDK的项目,它的CMakeLists.txt文件如下所示:

cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Werror")

add_library( # Specifies the name of the library.
        main

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        main.c)

target_link_libraries(
        android
        log
)

这里的it遵循googlesamples github存储库中列出的所有NDK示例项目的模式。我一直收到CMakeLists.txt(target_link_libraries)的CMake错误,似乎大多数人都是通过添加以下行来解决的:

add_library(debug <files Name>)

但是没有人为日志添加这个功能。我做错了什么?

请将错误信息本身添加到问题帖子中。短语“CMakeLists.txt(target_link_libraries)中的CMake错误”仅有助于确定错误位置,而无法解释错误原因。 - Tsyvarev
2个回答

5

将以下内容添加到您的 CMakeLists.txt 文件中,放在 target_link_libraries 代码行之前。

find_library( # Sets the name of the path variable.
          log-lib

          # Specifies the name of the NDK library that
          # you want CMake to locate.
          log )

然后根据以下方式修改target_link_libraries以链接Android日志

target_link_libraries( # Specifies the target library.
                   main

                   # Links the target library to the log library
                   # included in the NDK.
                   ${log-lib} )

我不确定这个官方文档在哪里。但是,当你按照Android Studio项目创建向导(具有本地支持)的步骤进行操作时,你将自动获得类似的CMakeLists.txt文件。 - shizhen
它在示例中。它也应该在CMake文档中(我认为这种链接库的风格并不是特定于Android的)。 - Dan Albert
我遇到了以下错误:CMake错误:此项目中使用了以下变量,但它们被设置为NOTFOUND。请在CMake文件中正确设置它们或确保它们已经被设置并测试过: log-lib - Amit Hooda

-1

1) 在你的代码源文件夹中,查找类似于 "Android.mk" 或 "project.mk" 的内容,然后记住其路径。

2) 进入 Android Studio 并单击 File ==> link c++ project with gradle ==> 选择构建系统 ndk-build,然后查找你先前找到的 .mk 文件。 点击确定,然后同步项目。


这是使用CMake构建的,而不是使用NDK-Bundle。 - Rafa
@Rafa,无论你在代码中找到的是Cmake还是ndk,两者都可以使用。 - zomba
只有CMakeLists.txt文件存在于项目中,其他文件不存在。 - Rafa
然后选择Cmake并点击确定。 - zomba

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接