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)
在完成此配置之后,我还需要包含log
lib才能使其正常运行。首先,我需要找到该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
以包含log
lib:
target_link_libraries( # Specifies the target library.
libplayer
libcodec
${log-lib})
Run Code Online (Sandbox Code Playgroud)
如果要在中使用此log
库,一切都很好libplayer
,但是如果要在libcodec
set中使用它,则会出现此错误:
未定义对__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。
问题是 -链接器为什么看不到log
lib libcodec
?为什么必须添加额外的块?
target_link_libraries( # Specifies the target library.
libcodec
${log-lib}
)
Run Code Online (Sandbox Code Playgroud)
使log
lib对于链接器可见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文档中的本节。PUBLIC
PRIVATE
归档时间: |
|
查看次数: |
61 次 |
最近记录: |