Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CMake to statically link to a library outside of the project

I would like to use CMake to link my project to my shared library. The library is only shared between a handful of projects and is rather small, so I would really like to build it before it is linked. Building it every time seems a better idea than having to maintain an up-to-date precompiled version, because I ten to change it together with the project. It is separate, because it contains stuff I will almost certainly need in the next project.

How can I configure CMake to do it?

My current CMakeLists.txt for the relevant project looks like this:

find_package( Boost REQUIRED COMPONENTS unit_test_framework)

include_directories(${BaumWelch_SOURCE_DIR}/../../grzesLib/src
                    ${BaumWelch_SOURCE_DIR}/src 
                    ${Boost_INCLUDE_DIRS})

if(CMAKE_COMPILER_IS_GNUCXX)
    add_definitions(-g -std=c++11 -Wall -Werror -Wextra -pedantic -Wuninitialized)
endif()


# Create the unit tests executable
add_executable(
 baumwelchtests stateindextest.cpp baumiterationtest.cpp baumwelchtest.cpp sampleparameters.cpp sdetest.cpp
 # Key includes for setting up Boost.Test
 testrunner.cpp
 # Just for handy reference
 exampletests.cpp
)

# Link the libraries
target_link_libraries( baumwelchtests ${Boost_LIBRARIES} baumwelchlib grzeslib)

but obviously the compilation fails with:

/usr/bin/ld: cannot find -lgrzeslib
like image 722
Grzenio Avatar asked Mar 22 '13 11:03

Grzenio


People also ask

How do I create a static library in CMake?

After you use command cmake . Then the Makefile will have the option like make default_target , revoke it and then you get a . a file in you r directory. Visit https://cmake.org/cmake/help/v3.0/command/add_library.html will help you!

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 add a shared library to CMake?

Calling GCC directly to build the application and link the shared library. Build the application and link the shared library with a Makefile. Generating the build environment with CMake and then building the application using the generated Makefile.


1 Answers

You mentioned you'd like to build the library rather than use a precompiled version. If the library has a CMakeList, you should add it using add_subdirectory(path/to/the/library/source/directory). It will then become a subproject of your project and you can use names of its targets normally in your CMakeList.

Note that while the command is called add_subdirectory, it can be an arbitrary directory on disk; it doesn't have to be a subdirectory of the master project's source dir. In case it's not a subdirectory, you have to explicitly specify a binary directory for it as well. Example:

add_subdirectory(/path/to/the/library/source/directory subproject/grzeslib)

The second argument, if given as a relative path, is interpreted relative to CMAKE_CURRENT_BINARY_DIR.

like image 87
Angew is no longer proud of SO Avatar answered Oct 27 '22 00:10

Angew is no longer proud of SO