This question already has answers here:
CMake link to external library
(6 answers)
Closed 2 years ago.
I'm new to building with CMake. I'm using Ubuntu and I have a .cpp file (say xyz.cpp located somewhere in ~/mydir) which links to three custom shared libraries (libA.so, libB.so & libC.so libraries). These three *.so files are located in /usr/local/lib.
I want to create a CMakeLists.txt to get this compiled. The below script throws errors like:
cmake_minimum_required(VERSION 3.11)
project(xyz VERSION 1.0.0 LANGUAGES C CXX)
# Set C standard to C11
set(CMAKE_C_STANDARD 11)
set(SOURCES src/xyz.cpp)
include_directories(/usr/local/include)
#For the shared library:
target_link_libraries(libA -L/usr/local/lib/)
target_link_libraries(libB -L/usr/local/lib/)
target_link_libraries(libC -L/usr/local/lib/)
target_link_libraries(xyz libA libB libC )
add_executable(xyz src/xyz.cpp)
ERROR:
CMake Error at CMakeLists.txt: (target_link_libraries):
Cannot specify link libraries for target "libA" which is not built
by this project.
-- Configuring incomplete, errors occurred!
A couple important notes:
The first argument to target_link_libraries() should be a valid CMake target, created by add_library() or add_executable(). Therefore, any target_link_libraries() call should be placed after the add_executable() call in your code.
You only need one call to target_link_libraries() to link all of your *.so libraries to the executable. The first three calls are unnecessary (and invalid because libA, libB, and libC are all undefined). You should provide the full path to each of these libraries to ensure that the correct library is used.
It is good practice to include the scoping argument in target_* calls, such as target_link_libraries(). This specifies whether the libraries are linked-to (PRIVATE), added to the link interface (INTERFACE), or both (PUBLIC). This distinction becomes more important as your CMake project grows to include more targets.
With these notes, and a few others commented below, your CMake file could look something like this:
cmake_minimum_required(VERSION 3.11)
# No need to specify C and CXX for LANGUAGES here, these are enabled by default!
project(xyz VERSION 1.0.0)
# Set C standard to C11
set(CMAKE_C_STANDARD 11)
set(SOURCES src/xyz.cpp)
include_directories(/usr/local/include)
# Define your executable target. You can pass in the SOURCES variable defined above.
add_executable(xyz ${SOURCES})
# Link the other libraries to your executable, using their full path.
# Note, the '-L' flag is not necessary.
target_link_libraries(xyz PRIVATE
/usr/local/lib/libA.so
/usr/local/lib/libB.so
/usr/local/lib/libC.so
)
Related
I am currently migrating a couple of CodeBlock projects to use CMake and Visual Studio Code. I am facing an issue related to the include directory not being known by the consuming target.
I followed a guide so far as I was not aware of the "modern CMake way" using targets at all until yet. Mainly referring to here: https://rix0r.nl/blog/2015/08/13/cmake-guide/ and the official CMake docs.
There are two libraries (.so). One of them needs to other one to be there (build) when it get's loaded by the application later on.
I am not sure if everything is even intended to work like I assume it to work so I will just continue with the code and the expected result.
Library A (which is needed by Library B) has following CMakeLists
cmake_minimum_required(VERSION 3.0.0)
project(A)
set(SOURCE somesource.c)
include(GNUInstallDirs)
find_package(JNI REQUIRED)
add_library(JNI SHARED IMPORTED)
set_property(TARGET JNI PROPERTY INTERFACE_INCLUDE_DIRECTORIES $ENV{JAVA_HOME}/include $ENV{JAVA_HOME}/include/linux)
set_property(TARGET JNI PROPERTY IMPORTED_LOCATION ${JNI_LIBRARIES})
add_library(${PROJECT_NAME} SHARED ${SOURCE})
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include $ENV{JAVA_HOME}/include $ENV{JAVA_HOME}/include/linux>
$<INSTALL_INTERFACE:include>
)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}Config
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(EXPORT ${PROJECT_NAME}Config DESTINATION ${CMAKE_INSTALL_PREFIX}/cmake)
Notice I only have one include file which is public and located in "include", so its used by the library itself but should also be part of the install interface
used by the "consuming" target.
Library B (which uses Library A and its header)
cmake_minimum_required(VERSION 3.0)
project(B)
set(SOURCE somesource.c)
add_library(${PROJECT_NAME} SHARED ${SOURCE})
find_package(A REQUIRED)
target_include_directories(${PROJECT_NAME}
PRIVATE ${PROJECT_SOURCE_DIR}
)
So in Library B I assume that I do not need to add the include_directories from Library A again. This is why I actually did the target approach so there is no need to keep track of linker/include dependencies. "find_package" should handle these on it's own right? I do not link neither because it is a shared object, not a static library.
Both libraries use the same CMAKE_INSTALL_PREFIX ofc and library A has been installed (make install). I double-checked on this. In case of using a different prefix or not doing the install, CMake already complains about not finding it.
So considering the issue now:
Within a source file from library B, I am using
#include <libcobjava.h>
resulting in an error when building as the compiler complains about "No such file or directory" Am I missing something? The file is definitely located at
CMAKE_INSTALL_PREFIX/include.
Apart from that as I am still new to CMake: Is this even the way you would do things if you own all the source?
Edit: Some more details on the follow-up issue as discussed in comments. Library A is libcobjava.
First I had the target_include_directories part as following (including the path where jni.h is located in $<INSTALL_INTERFACE:)
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include $ENV{JAVA_HOME}/include $ENV{JAVA_HOME}/include/linux>
$<INSTALL_INTERFACE:include $ENV{JAVA_HOME}/include $ENV{JAVA_HOME}/include/linux>
)
results in following error:
[cmake] Configuring done
[cmake] CMake Error in CMakeLists.txt:
[cmake] Target "libcobjava" INTERFACE_INCLUDE_DIRECTORIES property contains path:
[cmake]
[cmake] "/media/rbs42/data/Gebos/RBS42/tools/libcobjava/${_IMPORT_PREFIX}/include"
[cmake]
[cmake] which is prefixed in the source directory.
[cmake]
[cmake]
[cmake] Generating done
[cms-driver] Error during CMake configure: [cmake-server] Failed to compute build system.
This is why I removed $ENV{JAVA_HOME}/include $ENV{JAVA_HOME}/include/linux> from $<INSTALL_INTERFACE again.
The actual error I am getting in library B now is during compiling:
[build] /media/rbs42/data/rbs42/usr/include/libcobjava.h:1:10: fatal error: jni.h: No such file or directory
[build] #include <jni.h>
because library B does not have the include directories from library A that are not included in the $<INSTALL_INTERFACE. This is what one would expect. And thats the reason I added the following code before in library A.
set_property(TARGET JNI PROPERTY INTERFACE_INCLUDE_DIRECTORIES $ENV{JAVA_HOME}/include $ENV{JAVA_HOME}/include/linux)
which I assumed is taking care of propagating the include dependencies from library A to the "consuming" library B, although they are not part of the $<INSTALL_INTERFACE.
I'm pretty new to CMake, but I already encountered an error which I do not understand.
For given reasons, I have the following C++ setup: I have two own static libraries, multiple external static libraries and one executable. My own libraries a defines common stuff and all external libraries are linked into it. My library b defines more special stuff and has library a linked into it. Finally, b is linked into my executable c. Both b and c use functions from different external libraries, which should be visible through a.
I have the following CMakeLists.txt, where I reduced the problem by using only one external library: ZeroC Ice.
Root CMakeLists.txt
cmake_minimum_required (VERSION 3.3.0)
project (myproject)
# some settings
if (CMAKE_COMPILER_IS_GNUCC)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fmessage-length=0")
endif (CMAKE_COMPILER_IS_GNUCC)
if (CMAKE_COMPILER_IS_GNUCXX)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmessage-length=0")
endif (CMAKE_COMPILER_IS_GNUCXX)
set (CMAKE_VERBOSE_MAKEFILE ON)
set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--export-all-symbols")
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
include_directories (${CMAKE_CURRENT_SOURCE_DIR})
# ZeroC Ice
find_package (Ice 3.6.0 REQUIRED COMPONENTS Ice IceUtil)
include_directories (${Ice_INCLUDE_DIRS})
# libraries and executable
add_subdirectory (a)
add_subdirectory (b)
add_subdirectory (c)
CMakeLists.txt for a
set (a_FILES
stuff.cpp
)
add_library (a STATIC ${a_FILES})
# link to third-party libraries
target_link_libraries (a PUBLIC
${Ice_LIBRARIES}
)
CMakeLists.txt for b
set (b_FILES
more_stuff.cpp
)
add_library (b STATIC ${b_FILES})
# link to a
target_link_libraries (b PUBLIC
a
)
CMakeLists.txt for c
set (c_FILES
main.cpp
)
add_executable (c ${c_FILES})
# link to b
target_link_libraries (c PUBLIC
b
)
In this example, stuff.cpp, more_stuff.cpp and main.cpp use classes from the Ice library. a and strangely even b compile and link without any problem. Only c throws a lot of similar linker errors:
C:/PROGRA~2/ZeroC/ICE-36~1.0/include/Ice/FactoryTableInit.h:27: undefined reference to `_imp___ZN11IceInternal16FactoryTableInitD1Ev'
For me, the CMakeLists.txt setup looks just fine and googleing for days did not change anything. I would appreciate any help!
I'm working on Windows 10 with CMake 3.3.1 and CMake Generator "Eclipse CDT4 - MinGW Makefiles" (g++ version 4.8.1).
Thanks in advance! :)
EDIT:
The g++ linker output for c shows that it tries to link against liba.a libb.a C:/.../ice.lib C:/.../iceutil.lib, which seems just fine to me. Anyways, the undefined reference linker errors occur in header files whose classes are implemented inside of ice.lib, so I don't know what is going wrong here...
a and b are static libraries which are just archieved objects with symbols for external references, no linking happens. c is an executable which finally resolves these symbols and links the dependencies. It doesn't look like CMake transports the Ice library link upwards, you can try to just link her for the executable.
I found a "workaround" by myself: Using another CMake generator. By using Visual Studio as underlying compiler and linker, the issue does not appear. It seems like the problem was mixing up .lib and .a libraries. Since the default Ice installation on windows only provides .lib files, I ended up switching to VS and everything worked.
I am building a C++ library called alpha in Ubuntu with cmake, which contains one source file:
cmake_minimum_required(VERSION 2.8)
project(Alpha)
add_library (alpha alpha.cpp)
This creates a file called libalpha.a, which I now want to link to. So, I copy it into the source directory of another C++ projected called beta, which also contains one source file:
cmake_minimum_required(VERSION 2.8)
project(Beta)
add_executable(beta beta.cpp)
target_link_libraries(beta alpha)
However, I get the following error:
/usr/bin/ld: cannot find -lalpha
The same thing happens if I use the line:
target_link_libraries(beta libalpha.a)
Why can beta not find the alpha library?
If you wish to build the library and the program completely separately, you have to use imported targets. When you try to link your executable against a "completely unknown" library, CMake build system automatically passes the task of locating the library to the linker, simply adding -lalpha option. When the linker encounters the option it attempts to locate libalpha.so in one of the standard library locations (i.e. /usr/lib/, /usr/local/lib etc) and expectedly fails. You can use an absolute path to the libalpha.a: target_link_libraries(beta /path/to/libalpha.a).
However if you can build things together, this greatly simplifies the task. Consider
<project>/CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
Project(Test)
add_subdirectory(alpha)
add_subdirectory(beta)
<project>/alpha/CMakeLists.txt
cmake_minimum_required(VERSION 2.8.11)
project(alpha)
set(SOURCES alpha.c)
add_library(alpha ${SOURCES})
target_include_directories(
alpha INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
)
target_include_directories() with the complex expression within is required to automatically add libalpha include directories to all components which later are linked against libalpha.
<project>/beta/CMakeLists.txt
project(beta)
set(SOURCES beta.c)
add_executable(beta ${SOURCES})
target_link_libraries(beta alpha)
Add this line with the path to alpha-library.
link_directories( <library path> )
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
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.
I use this line to compile a simple program:
g++ main.cc -lntl -lm -lgmp
How do you include this into CMake?
find_package(NTL REQUIRED)
find_package(GMP REQUIRED)
Doesn't work. And gives the following error:
CMake Error at CMakeLists.txt:30 (find_package):
Could not find module FindNTL.cmake or a configuration file for package
NTL.
...
.
and
SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++0x -lntl -lm -lgmp)
Doesn't work either (but I think it's just wrong in general).
Thank you!
If ntl, m, and gmp libraries are usually installed to somewhere in the default path (e.g. /usr/ or /usr/local/), you could simply do something like:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(Test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
add_executable(test main.cc)
target_link_libraries(test ntl m gmp)
This is probably closest to your original g++ command, but it isn't very robust however; if any of the libraries aren't found, you won't know about it until you try linking. If you want to fail at configure time (i.e. while running CMake), you could add find_library calls for each of the required libs, e.g.
find_library(NTL_LIB ntl)
if(NOT NTL_LIB)
message(FATAL_ERROR "ntl library not found. Rerun cmake with -DCMAKE_PREFIX_PATH=\"<path to lib1>;<path to lib2>\"")
endif()
You'd then have to change your target_link_libraries command to
target_link_libraries(test ${NTL_LIB} ${M_LIB} ${GMP_LIB})
You'd probably also then have to do a find_file for one of each lib's header files to find out the appropriate path to add via the include_directories command (which translates to -I for g++).
Note, it's important to put quotes around the extra CXX_FLAGS arguments, or CMake treats them like separate values in a list and inserts a semi-colon between the flags.
For further information about find_library, find_file, etc. run:
cmake --help-command find_library
cmake --help-command find_file
Regarding your error:
It doesn't look like there's a FindNTL.cmake module included with CMake. That means you'll have to either:
Write your own FindNTL.cmake,
Find another that somebody else has written,
Hack together a solution that:
Checks if NTL is installed
Provides link targets, relevant flags, etc.
From a (rather quick) Google search, it appears somebody has an NTL module for CMake. Since NTL use GMP, you will probably need the associated GMP module for CMake. It doesn't look like a fully-featured CMake module, but it also appears to be the best thing out there (again, it was a quick Google search, and I don't use NTL).
To use, you'll want to add some things to your CMakeLists.txt:
# Let CMake know where you've put the FindNTL.cmake module.
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/build/CMake/Modules")
# Call the FindNTL module:
find_package(NTL REQUIRED)
SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++0x -lntl -lm -lgmp)
Yes, this is wrong. You don't want to be setting your CXX_FLAGS with linking directives. I would use:
SET ( CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=cxx0x )
to set the Cxx standard you want to use. To actually link to libraries, you'll want to:
Ensure that you've found the libraries (with the relevant find_package ( FOO ) lines)
Link those against your target, like this:
# Build the Foo executable. (Or library, or whatever)
add_executable (FooEXE ${Foo_SOURCES} )
target_link_libraries (FooEXE
${bar_LIBRARIES}
${baz_LIBRARY}
)
Please note! ${bar_LIBRARIES} and ${baz_LIBRARY} is not a typo; there's no standard way of setting the relevant libraries in the FindFOO.cmake modules, which is, in my opinion, an annoyance. If one doesn't work, try the other, or, worst case, have a look in the relevant FindFOO.cmake file (there's a bunch installed with CMake by default) to see what each one uses. With the link i provided, you can use ${NTL_LIB} and ${GMP_LIB}.