如何以正确的方式构建CMakeLists?

How to build CMakeLists the right way?

本文关键字:方式 构建 CMakeLists      更新时间:2023-10-16

>我有以下问题:我有两组不同的文件(主文件和一组附加文件),我想将它们分开。因此,我有一组文件(主文件),我以这种方式配置:

set(libplayersource
....
)
add_library( # Sets the name of the library.
libplayer
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${libplayersource})

然后我有第二组文件(附加),我以这种方式配置:

set(codec_source
...)
add_library(libcodec SHARED ${codec_source})

最终,我需要链接这两组文件:

target_link_libraries( # Specifies the target library.
libplayer
libcodec)

在此配置之后,我还需要包含log库才能使其工作。首先,我需要找到这个log库,然后将其包含在我的本机库中。

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以包含log库:

target_link_libraries( # Specifies the target library.
libplayer
libcodec
${log-lib})

如果您要在libplayer中使用此loglib,一切都很好,但是如果您要在libcodec集中使用它,则会收到此错误:

对"__android_log_print"的未定义引用

clang++.exe:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

这意味着链接器看不到此方法的实现。

我在SO上找到了这个答案:

https://stackoverflow.com/a/47803975/5709159

为了解决这个问题,我在CMakeLists文件中添加了这一行:

target_link_libraries( # Specifies the target library.
libcodec
android
${log-lib}
)

Main CMake 文件实现:

...
#Main module
set(libplayersource
....
)
add_library( # Sets the name of the library.
libplayer
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${libplayersource})
#Additional module
set(codec_source
...)
add_library(libcodec SHARED ${codec_source})
#Log lib
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)
#Linking
target_link_libraries( # Specifies the target library.
libcodec
${log-lib}
)
target_link_libraries( # Specifies the target library.
libplayer
libcodec
${log-lib})
...

因此,我需要在两个库中都提到log库。

问题是- 为什么链接器在libcodec中看不到log库?为什么我必须添加额外的块?

target_link_libraries( # Specifies the target library.
libcodec
${log-lib}
)

使log库对libcodec中的链接器可见?

P.S在Visual Studio中,如果你有主项目A和两个库B和C,你可以在A中包含这些B和C库,就是这样;每个人都知道每个人。我可以从 C 调用 B 中的方法,依此类推。为了从 C 调用 B 方法,我不需要在 B 中包含 C。这两个库都包含在 A 中作为主项目就足够了......

如果我错过了问题中的某些内容,请随时提问。

如果您的libcodec使用log-lib中定义的实现,则必须log-lib显式链接到libcodec此调用:

target_link_libraries( # Specifies the target library.
libplayer
libcodec
${log-lib})

libcodeclog-lib链接到libplayer,它不会log-lib链接到libcodec。它暗示了这个依赖关系图:

libplayer
/       
libcodec     log-lib

target_link_libraries()调用的第一个参数是"链接到"库,以下所有目标都链接到第一个。因此,您需要将log-lib链接到libcodec,如下所示:

target_link_libraries( # Specifies the target library.
libcodec
${log-lib}
)

现在,libcodec将了解log-lib中定义的实现,这意味着这里的依赖关系图:

libplayer
/       
libcodec     log-lib
/
log-lib

不过,你可以让它更干净。我们可以删除libplayerlog-lib之间的直接链接,并允许log-lib实现通过libcodec传播libplayer

target_link_libraries(libcodec PUBLIC
${log-lib}
)
target_link_libraries(libplayer PRIVATE
libcodec
)

这会将依赖关系图简化为以下内容:

libplayer
/
libcodec
/
log-lib

请参阅 CMake 文档中的此部分,了解如何以及何时在链接时使用PUBLICPRIVATE关键字。