C++ 使用 CMake 链接共享库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41642341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Link a shared library with CMake
提问by Dimitri Lozovoy
I'm trying to link in a pre-compiled shared library file called libtest-lib.so. This is what I have at the bottom of my CMakeLists.txt:
我正在尝试链接一个名为 libtest-lib.so 的预编译共享库文件。这是我在 CMakeLists.txt 底部的内容:
link_directories("/projectspath/LinkTest/TestLib/app/build/intermediates/cmake/debug/obj/armeabi-v7a")
add_library(testlib libtest-lib.so)
target_link_libraries(testlib libtest-lib.so)
As above, I get the following error:
如上所述,我收到以下错误:
CMake Error at CMakeLists.txt:49 (add_library):
Cannot find source file:
libtest-lib.so
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
CMake Error: CMake can not determine linker language for target: testlib
If I comment out the add_library line, I get the following:
如果我注释掉 add_library 行,我会得到以下信息:
CMake Error at CMakeLists.txt:51 (target_link_libraries):
Cannot specify link libraries for target "testlib" which is not built by this project.
It seems that source files (.c, cpp, etc) are absolutely required when linking in a library. But how do I link in an .so file? The docs say the following about target_link_libraries():
在库中链接时,似乎绝对需要源文件(.c、cpp 等)。但是如何在 .so 文件中进行链接?文档对 target_link_libraries() 说了以下内容:
The named must have been created in the current directory by a command such as add_executable() or add_library().
命名必须已通过诸如 add_executable() 或 add_library() 之类的命令在当前目录中创建。
If I substitute add_library() with add_executable() I get the same error. What is the proper way to link an .so file in CMake?
如果我用 add_executable() 替换 add_library() 我得到同样的错误。在 CMake 中链接 .so 文件的正确方法是什么?
回答by oLen
I think that what you want is to import a library for CMake:
我认为您想要的是为 CMake 导入一个库:
add_library(testlib SHARED IMPORTED)
set_property(TARGET testlib PROPERTY IMPORTED_LOCATION "/projectspath/LinkTest/TestLib/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libtest-lib.so")
See https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/Exporting-and-Importing-Targetsfor more information
有关更多信息,请参阅https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/Exporting-and-Importing-Targets
回答by Unapiedra
add_library creates a new library.
add_library 创建一个新库。
Instead you want to link your library to some other target.
相反,您希望将您的库链接到某个其他目标。
Let's say
让我们说
add_executable(main main.cpp)
target_link_libraries(main libtest-lib)
This should already work.
这应该已经起作用了。
回答by KeerthiKishan Alavala
I found another workaround, to mention path where the library is present while linking lib to the executable file.
我找到了另一种解决方法,提到在将 lib 链接到可执行文件时存在库的路径。
INCLUDE_DIRECTORIES(/path/to/headers)
ADD_EXECUTABLE(TARGET target.c)
TARGET_LINK_LIBRARIES(TARGET_FILE "-L/path/to/shared/library" SHARED_LIB_name)
Which is indirect including library search path flag. One might also have to link the location of header files while using the library.
这是间接的,包括库搜索路径标志。在使用库时,可能还需要链接头文件的位置。
回答by fedepad
You should have:
你应该有:
link_directories("/projectspath/LinkTest/TestLib/app/build/intermediates/cmake/debug/obj/armeabi-v7a")
set(MY_SOURCES mylib.cpp)
add_library(testlib ${MY_SOURCES})
target_link_libraries(testlib libtest-lib)
which means that you should specify the sources of YOUR library as second argument to add_library()
or add_executable()
and not the library that is already compiled.
You need your sources because generally you build something (a library or an executable) that requires linking to some library that already exist.
Otherwise, what would you build? Nothing? And link the library to what? Who would be the consumer of that library?
这意味着您应该将您的库的源指定为add_library()
oradd_executable()
而不是已经编译的库的第二个参数。
您需要源代码,因为通常您构建的东西(库或可执行文件)需要链接到某个已经存在的库。
否则,你会建造什么?没有?并将图书馆链接到什么?谁会是那个图书馆的消费者?
回答by Dimitri Lozovoy
The proper way to do this is:
正确的做法是:
target_link_libraries(native-lib "/projectspath/LinkTest/TestLib/app/build/intermediates/cmake/debug/obj/${ANDROID_ABI}/libtest-lib.so")