Ale*_*nko 1 c++ linker cmake android-ndk
我有以下问题:我有两组不同的文件(主文件和另一组文件),我希望将它们分开。因此,我有一组文件(主文件),我以这种方式配置:
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})
Run Code Online (Sandbox Code Playgroud)
然后,我有了第二组文件(其他文件),我以这种方式配置:
set(codec_source
...)
add_library(libcodec SHARED ${codec_source})
Run Code Online (Sandbox Code Playgroud)
最终,我需要链接这两套文件:
target_link_libraries( # Specifies the target library.
libplayer
libcodec)
Run Code Online (Sandbox Code Playgroud)
在完成此配置之后,我还需要包含loglib才能使其正常运行。首先,我需要找到该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)
Run Code Online (Sandbox Code Playgroud)
另外,我应该进行编辑target_link_libraries以包含loglib:
target_link_libraries( # Specifies the target library.
libplayer
libcodec
${log-lib})
Run Code Online (Sandbox Code Playgroud)
如果要在中使用此log库,一切都很好libplayer,但是如果要在libcodecset中使用它,则会出现此错误:
未定义对__android_log_print的引用
clang ++。exe:错误:链接器命令失败,退出代码为1(使用-v查看调用)
这意味着链接器看不到此方法的实现。
我在SO上找到了这个答案:
为了解决该问题,我在CMakeLists文件中添加了以下这一行:
target_link_libraries( # Specifies the target library.
libcodec
android
${log-lib}
)
Run Code Online (Sandbox Code Playgroud)
主要的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})
...
Run Code Online (Sandbox Code Playgroud)
因此,我需要log在两个库中都提到lib。
问题是 -链接器为什么看不到loglib libcodec?为什么必须添加额外的块?
target_link_libraries( # Specifies the target library.
libcodec
${log-lib}
)
Run Code Online (Sandbox Code Playgroud)
使loglib对于链接器可见libcodec?
PS在Visual Studio中,如果您有主项目A和两个库B和C,则将这些B和C库包含在A中,就是这样;每个人都知道每个人。我可以从C调用B中的方法,依此类推。为了从C调用B方法,我不需要在B中包含C。这两个库都作为主项目包含在A中就足够了……
如果我错过了问题中的任何内容,请随时提出。
如果您libcodec使用的实现定义中log-lib,您必须链接log-lib到libcodec明确。这个电话:
target_link_libraries( # Specifies the target library.
libplayer
libcodec
${log-lib})
Run Code Online (Sandbox Code Playgroud)
链接libcodec和log-lib到libplayer,它不会链接log-lib到libcodec。它暗示了这个依赖图:
libplayer
/ \
libcodec log-lib
Run Code Online (Sandbox Code Playgroud)
target_link_libraries()调用的第一个参数是“链接到”库,所有随后的目标都链接到第一个。因此,您需要像这样链接log-lib到libcodec:
target_link_libraries( # Specifies the target library.
libcodec
${log-lib}
)
Run Code Online (Sandbox Code Playgroud)
现在,libcodec将了解有关中定义的实现log-lib,这意味着在此处具有依赖关系图:
libplayer
/ \
libcodec log-lib
/
log-lib
Run Code Online (Sandbox Code Playgroud)
您可以使它更清洁。我们可以删除的直接联系之间libplayer和log-lib,并允许log-lib执行繁殖,通过libcodec对libplayer。
target_link_libraries(libcodec PUBLIC
${log-lib}
)
target_link_libraries(libplayer PRIVATE
libcodec
)
Run Code Online (Sandbox Code Playgroud)
这会将依赖关系图简化为以下内容:
libplayer
/
libcodec
/
log-lib
Run Code Online (Sandbox Code Playgroud)
有关链接时如何以及何时使用and 关键字的信息,请参阅CMake文档中的本节。PUBLICPRIVATE
| 归档时间: |
|
| 查看次数: |
61 次 |
| 最近记录: |