我正在尝试链接一个名为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)
如上所述,我收到以下错误:
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
如果我注释掉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.
在库中链接时,似乎绝对需要源文件(.c,cpp等).但是如何链接.so文件?文档说下了关于target_link_libraries()的内容:
必须通过add_executable()或add_library()等命令在当前目录中创建named.
如果我用add_executable()替换add_library(),我会得到同样的错误.在CMake中链接.so文件的正确方法是什么?
我认为你想要的是为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")
https://cmake.org/Wiki/CMake/Tutorials/Exporting_and_Importing_Targets了解更多信息