Reputation: 453
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:
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:
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():
The named must have been created in the current directory by a command such as add_executable() or 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?
Upvotes: 19
Views: 112875
Reputation: 1774
This is another (little cleaner) way (Derek and ref):
# -----
# Shared Library for runtime: your SO file(s) without directory
# The actual file should be "libtest-lib.so", but you put without "lib" in here. (you could still put "libtest-lib.so" in here)
# This is guide from my reference link
set ( PROJECT_LINK_LIBS test-lib.so )
link_directories( /projectspath/LinkTest/TestLib/app/build/intermediates/cmake/debug/obj/armeabi-v7a )
.
.
.
# -----
# Compiling CPP code
set ( SOURCES <your CPP file with directory> )
add_executable ( testlib ${SOURCE} )
# Calling Shared Library
# Please match the executable with above "add_executable"
target_link_libraries( testlib PROJECT_LINK_LIBS )
Upvotes: 0
Reputation: 5547
I think that what you want is to import a library for 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-Targets for more information
Upvotes: 31
Reputation: 1
I found another workaround, to mention path where the library is present while linking lib to the executable file.
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.
Upvotes: 0
Reputation: 16197
add_library creates a new 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.
Upvotes: 11
Reputation: 453
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")
Upvotes: 0
Reputation: 4609
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?
Upvotes: 2