CMake Boost linker error - c++

I'm trying to build a C++ executable that uses a Boost library. Building with CMake.
CMake snippet:
find_package(Boost REQUIRED system)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(main src/cpp/main.cpp)
target_link_libraries(main ${BOOST_LIBRARIES})
Getting following CMake build error message:
Undefined symbols for architecture x86_64:
"boost::system::system_category()", referenced from:
...
Why am I getting the error and how can I fix it?

The variables created by the find_package(Boost ...) call you are using are case sensitive, i.e.
${BOOST_LIBRARIES}
will be empty and you need to make sure you are using it like this:
${Boost_LIBRARIES}
The same goes for Boost_INCLUDES which you have used (read: copy-pasted) correctly here:
include_directories(${Boost_INCLUDE_DIRS})

To complete seeekr's answer, you should consider using imported target instead of Boost_ variables:
find_package(Boost REQUIRED system)
add_executable(main ...)
target_link_libraries(main Boost::system)
The imported target will handle linkage, include directories, compile definitions, and all needed stuff with a single call.

Related

Linking GSL using CMake, undefined symbols

I'm attempting to link GSL to my C++ code using cmake, and all goes well until I attempt to build. CMakeLists.txt:
cmake_minimum_required(VERSION 3.9)
include_directories(${project_SOURCE_DIR}/src)
link_directories(${project_BINARY_DIR}/src)
FIND_PACKAGE(Boost COMPONENTS system filesystem unit_test_framework REQUIRED )
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
find_package(GSL REQUIRED)
set(CMAKE_CXX_STANDARD 14)
set (lib_SOURCES
../src/file.h
../src/file.cpp
# etc
)
target_include_directories(project PRIVATE ${PROJECT_SOURCE_DIR} ${GSL_INCLUDE_DIR})
target_link_libraries(sigma OpenMP::OpenMP_CXX GSL::gsl GSL:gslcblas)
When I add #include <gsl/gsl_integration.h> and call something from the library in the C++, e.g. a gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);, at the make I get an Undefined symbols for architecture x86_64:..., so the library isn't linking, but I don't know why. CMake clearly recognises GSL, but isn't liking to it.
I'm on Big Sur 11.3.1, GSL 2.6 installed via homebrew.
Fixed this by removing my CMakeCache.txt files and running cmake again.
I realized this as I was preparing minimal working example in response to Tsyvarev's comment. I hope this can help somebody.

How to build static library with bundled dependencies - CMake

I am currently using CMake to create a static library which utilizes a few of the static libraries from OpenCV 4 ( core imgcodecs video highgui imgproc ). My intention is to be able to bundle all of the required OpenCV static libraries into my own library so that I can distribute it as one library. Additionally, I want for the user of my library to not have to install OpenCV 4 on their system (but do not mind if the user has to do simple installs using apt-get install). I know there are tools for bundling static libraries (such as using ar for linux).
However, where I really am having the issue is with all the dependencies of OpenCV (such as libjpeg, libpng, etc). I don't necessarily mind if these libraries are bundled with mine or linked dynamically as they are relatively easy to install (can be installed with sudo apt-get install, whereas opencv4 needs to be built from source).
What is the best way to go about doing this?
This is my current CMakeLists.txt
It is currently working, but that is because I am using find_package(OpenCV REQUIRED) (which defeats the purpose of what I am trying to do). When I remove that line, the linker complains about not being able to find the OpenCV dependencies.
cmake_minimum_required(VERSION 2.8)
project(myproject)
set(CMAKE_CXX_STANDARD 14)
include_directories(${CMAKE_CURRENT_LIST_DIR}/include)
link_directories(${CMAKE_CURRENT_LIST_DIR}/lib)
find_package(OpenMP REQUIRED)
find_package(OpenCV REQUIRED)
set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(nlohmann_json)
list(APPEND LINKER_LIBS opencv_core opencv_highgui opencv_video opencv_imgcodecs libmxnet.so libncnn.a nlohmann_json::nlohmann_json)
file(GLOB SRC${CMAKE_CURRENT_LIST_DIR}/src/*.cpp${CMAKE_CURRENT_LIST_DIR}/main.cpp)
add_library(myproject ${SRC})
target_link_libraries(myproject ${LINKER_LIBS} ${OpenMP_CXX_FLAGS})
To elaborate on my question. I build my project which generates libmyproject.a. I then take this library and will eventually extract the symbols from the OpenCV libs (libopencv_core.a libopencv_highgui.a libopencv_imgcodecs.a libopencv_video.a) and add them to my lib (for the time being, I have not yet done this step, which is why in the below example I am linking libopencv_*). I then use my library in a new project, for which the CMakeLists.txt is shown below:
cmake_minimum_required(VERSION 2.8)
project(myproject-driver)
set(CMAKE_CXX_STANDARD 14)
include_directories(${CMAKE_CURRENT_LIST_DIR}/include)
link_directories(${CMAKE_CURRENT_LIST_DIR}/lib)
find_package(OpenMP REQUIRED)
add_executable(myproject-driver main.cpp)
target_link_libraries(myproject-driver myproject libncnn.a ${OpenMP_CXX_FLAGS} libmxnet.so libopencv_core.a libopencv_highgui.a libopencv_imgcodecs.a libopencv_video.a)
Building this generates the following errors:
Linking CXX executable myproject-driver
/usr/bin/ld: /home/nchafni/Cyrus/myproject/lib/libopencv_imgcodecs.a(grfmt_jpeg.cpp.o): undefined reference to symbol 'jpeg_default_qtables##LIBJPEG_8.0'
//usr/lib/x86_64-linux-gnu/libjpeg.so.8: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
How can I fix this. Is there some CMake command which will link all these dependencies for me? Do I need to manually track down each dependency of those libopencv_* libs and link those manually? Once again, this is assuming that the person using libmyproject.a can't use find_package(OpenCV REQUIRED) as it won't be defined as they have not installed OpenCV on their machine.
First of all, don't use the super old and outdated version 2.8 of CMake. CMake 3.x is so much more powerful and pretty straightforward to use.
Some tips for modern CMake.
Don't use file(GLOB), see here why that is.
Don't use directory wide instructions, rather use target instructions, e.g. target_include_directories vs. include_directories.
Don't use string variables like ${<PACKAGE_NAME>_LIBRARIES}, rather use targets, e.g. <Package_NAME>::lib
When using targets instead of string variables, all the properties (including LINK_INTERFACE) of that target will be populated to the library/executable when calling target_link_libraries, so no more include_directories,link_directories, etc.
myproject
cmake_minimum_required(VERSION 3.14)
project(myproject)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenMP REQUIRED)
find_package(OpenCV REQUIRED)
set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(nlohmann_json)
set(SOURCES ...) # list all the source files here
add_library(myproject ${SOURCES})
target_include_directories(myproject PUBLIC # give it a scope
${CMAKE_CURRENT_LIST_DIR}/include
)
target_link_libraries(myproject PUBLIC # give it a scope
opencv_core # using the target, you will get all LINK_LIBRARIES
opencv_highgui
opencv_video
opencv_imgcodecs
libmxnet.so # where is this coming from?
libncnn.a # where is this coming from?
nlohmann_json::nlohmann_json
OpenMP::OpenMP_CXX ## linking against a target, CXX_FLAGS will be populated automatically
)
myprojec-driver
cmake_minimum_required(VERSION 3.14)
project(myproject-driver)
set(CMAKE_CXX_STANDARD 14)
add_executable(myproject-driver main.cpp)
target_link_libraries(myproject-driver PUBLIC # give it a scope
myproject # gets all dependencies through the LINK_INTERFACE
)

Compiling with CMake and Boost unit tests

I'm relatively new to C++, and I'm trying to compile a project using CMake with the following #include:
#include <boost/test/unit_test.hpp>
I get the following error
undefined symbols for architecture x86_64:
"boost::unit_test::unit_test_log_t::instance()", referenced from:
___cxx_global_var_init in functions1.cpp.o
___cxx_global_var_init in functions2.cpp.o
___cxx_global_var_init in main.cpp.o
ld: symbol(s) not found for architecture x86_64
I'm pretty sure this is something to do with my CMakeLists.txt, so here it is:
cmake_minimum_required(VERSION 3.13)
project(MyProject)
set(CMAKE_CXX_STANDARD 14)
include_directories(.)
include_directories(/usr/local/include/)
add_executable(MyProject
functions1.cpp
functions1.h
functions2.cpp
functions2.h
main.cpp
main.h
utillity.cpp
utillity.h)
set(BOOST_ROOT "/usr/local/Cellar/boost/1.69.0_2")
find_package(Boost COMPONENTS filesystem system unit_test_framework REQUIRED)
#find_package(Boost 1.69.0)
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
include_directories (${Boost_INCLUDE_DIRS})
I'm on OSX Mojave, and installing with brew install boost. I'm aware there are several posts out there that report very similar issues, but none of the solutions seem to work for me.
Edit:
Have adjusted my CMakeLists to the following based on Guillaume's suggestion below.
make_minimum_required(VERSION 3.13)
project(MyProject)
set(CMAKE_CXX_STANDARD 14)
add_executable(MyProject
functions1.cpp
functions1.h
functions2.cpp
functions2.h
main.cpp
main.h
utillity.cpp
utillity.h)
set(BOOST_ROOT "/usr/local/Cellar/boost/1.69.0_2")
find_package(Boost COMPONENTS filesystem system test REQUIRED)
target_include_directories(MyProject PUBLIC ".")
target_link_libraries(MyProject PUBLIC
Boost::filesystem Boost::system Boost::test)
I understand that this is, in principle, better, but it's giving me:
Unable to find the requested Boost libraries.
Boost version: 1.69.0
Boost include path: /usr/local/Cellar/boost/1.69.0_2/include
Could not find the following Boost libraries:
boost_test
Some (but not all) of the required Boost libraries were found. You may need to install these additional Boost libraries. Alternatively, set BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT to the location of Boost.
Edit 2:
Have tried edits and upgrading to boost 1.7, but still have:
Could not find a package configuration file provided by "boost_test"
(requested version 1.70.0) with any of the following names:
boost_testConfig.cmake
boost_test-config.cmake
Add the installation prefix of "boost_test" to CMAKE_PREFIX_PATH or set
"boost_test_DIR" to a directory containing one of the above files. If
"boost_test" provides a separate development package or SDK, be sure it
has been installed.
You have to link properly to boost instead of adding include directory flags.
Linking libraries in cmake will apply all required property to use boost onto your targets.
First off, don't do that:
include_directories(.)
include_directories(/usr/local/include/)
This is asking for link error. It will enable you to use headers of libraries you don't link properly to. This will cause linking errors, even within your own project.
cmake_minimum_required(VERSION 3.13)
project(MyProject)
set(CMAKE_CXX_STANDARD 14)
add_executable(MyProject
functions1.cpp
functions1.h
functions2.cpp
functions2.h
main.cpp
main.h
utillity.cpp
utillity.h)
list(APPEND CMAKE_PREFIX_PATH "/usr/local/Cellar/boost/1.69.0_2")
set(Boost_ADDITIONAL_VERSIONS "1.69.0" "1.69")
find_package(Boost COMPONENTS filesystem system test REQUIRED)
# Not needed
#if(NOT Boost_FOUND)
# message(FATAL_ERROR "Could not find boost!")
#endif()
# include_directories (${Boost_INCLUDE_DIRS})
target_include_directories(MyProject PUBLIC ".")
# adds include directories, definitions and link libraries
target_link_libraries(MyProject PUBLIC
Boost::filesystem Boost::system Boost::test
)

CMake find_package Boost fails on multiple usage

I am trying to use a library that I wrote (is-msgs) that depends on boost::filesystem and have the following is-msgsConfig.cmake file:
include(CMakeFindDependencyMacro)
find_dependency(Protobuf)
find_dependency(spdlog)
find_dependency(Boost COMPONENTS filesystem)
include("${CMAKE_CURRENT_LIST_DIR}/is-msgsTargets.cmake")
If I try to use the library to build an executable, for instance, everything works like expected:
find_package(is-msgs)
add_executable(example example.cpp)
target_link_libraries(example is-msgs::is-msgs)
Although if my executable also depends on another boost component, for instance, boost::chrono the following code will not work:
find_package(Boost REQUIRED COMPONENTS chrono)
find_package(is-msgs)
add_executable(example example.cpp)
target_link_libraries(example is-msgs::is-msgs boost::chrono)
Outputs:
CMake Error at CMakeLists.txt:11 (add_executable):
Target "main" links to target "Boost::filesystem" but the target was not
found. Perhaps a find_package() call is missing for an IMPORTED target, or
an ALIAS target is missing?
However, if I reorder the find_package instructions it works without a problem:
find_package(is-msgs)
find_package(Boost REQUIRED COMPONENTS chrono)
add_executable(example example.cpp)
target_link_libraries(example is-msgs::is-msgs boost::chrono)
Is something wrong with my is-msgsConfig.cmake file or is this a bug in find_package(Boost) ? I am using CMake 3.11.1.

unresolved symbols while linking boost program options

A user of my project reported this error to me. I cannot reproduce it on my computer or my lab's server, so I ask it here.
The project uses CMake to generate build environment. It uses the FindBoost utility (provided with CMake) to find Boost resources.
On the beginning, my user said while linking the final programs, the compiler was provided with "/usr/lib64/lib64/libboost_XXX.so", instead of the correct "/usr/lib64/libboost_XXX.so". I failed to find why CMake generated such weird library location, and asked him to manually set the variable Boost_LIBRARIES, and print them:
Boost libraries are: /usr/lib64/libboost_thread-mt.so;/usr/lib64/libboost_program_options-mt.so;/usr/lib64/libboost_filesystem-mt.so
Things seem to be correct. The compilation was successful. But when it goes to linking, the program cries about many undefined symbols:
CMakeFiles/ht-filter.dir/ht-filter.cpp.o: In function `parse_options(int, char**)':
/public/home/yli/Downloads/htqc-0.15.0-Source/ht-filter.cpp:43: undefined reference to `boost::program_options::options_description::options_description(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, unsigned int)'
......
/usr/local/include/boost/program_options/errors.hpp:372: undefined reference to `boost::program_options::validation_error::get_template(boost::program_options::validation_error::kind_t)'
This is the two typical of the so-many errors: one locates from my source code, the other locates from boost's header. In the corresponding line of my source code, I just created the options_description object
// I renamed boost::program_options to opt
opt::options_description opt_main("Options:");
The OS of my user is CentOS 6.2, and his Boost version is 1.50.0 which is similiar with the one in my computer. The version of CMake of my user is 2.8.11 which is also same with mine.
While using CMake's find_package for Boost, you can give a few hints to CMake which may help it find the correct library, such as:
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(BOOST_ROOT "/usr")
find_package(Boost 1.50.0)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARY_DIRS: ${Boost_LIBRARY_DIRS}")
Also if you are linking with dynamic libs make sure to let Boost headers know about it (I think you shouldn't mess with the order in here):
link_directories(${Boost_LIBRARY_DIRS})
include_directories(${Boost_INCLUDE_DIRS})
add_definitions( -DBOOST_ALL_DYN_LINK )