Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link a shared library with CMake

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?

like image 271
Dimitri Lozovoy Avatar asked Jan 13 '17 19:01

Dimitri Lozovoy


People also ask

How do I link a .so file to CMake?

Just do TARGET_LINK_LIBRARIES(program /path/to/library.so) > > > > 2. Better one is to: > > > > FIND_LIBRARY(MY_LIB NAMES library > > PATHS /path/to) > > TARGET_LINK_LIBRARIES(program ${MY_LIB}) > > > > Andy > > > > On Fri, 2004-10-29 at 10:19, Ning Cao wrote: > >> I want to link a .

How do I connect to a shared library?

Shared libraries (also called dynamic libraries) are linked into the program in two stages. First, during compile time, the linker verifies that all the symbols (again, functions, variables and the like) required by the program, are either linked into the program, or in one of its shared libraries.

How do I add a library to CMake?

To add a library in CMake, use the add_library() command and specify which source files should make up the library. Rather than placing all of the source files in one directory, we can organize our project with one or more subdirectories. In this case, we will create a subdirectory specifically for our library.


2 Answers

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

like image 139
oLen Avatar answered Oct 20 '22 04:10

oLen


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.

like image 28
Unapiedra Avatar answered Oct 20 '22 02:10

Unapiedra