cmake c++ boost link - c++

I know, there are plenty of different question out there regarding how to link boost libraries with a c++ project. However after hours of trying I still couldn't figure it out.
Here is my problem:
I need a filesystem library. Unluckily I didn't manage to use the new std::filesystem library so I thought let's simply use boost... until now I only needed some header only boost libraries, so I spend a couple of hours to finally manage to build boost (at least I think I have build it) in my directory C:/boost/stage/lib I have files called libboost_filesystem-mgw53-1_64.dll and so on...
I'm trying to use cmake to link these libraries to my own file, but I really get confused... First of all: in CMAKE should I use "BOOST" "Boost" or "boost"...
At the moment I have a mixture... I use something like:
set(CMAKE_CXX_STANDARD 17)
SET(BOOST_ROOT "C:/boost")
SET(BOOST_LIBRARYDIR "C:/boost/stage/lib")
find_package(BOOST 1.64.0 COMPONENTS system filesystem REQUIRED)
include_directories(${BOOST_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
set(SOURCE_FILES list of all my source files)
add_executable(myExecuteable ${SOURCE_FILES})
TARGET_LINK_LIBRARIES(myExecuteable ${Boost_SYSTEM_LIBRARY} ${Boost_FILESYSTEM_LIBRARY})
After hours I figured out that my find_package command only works if I type "BOOST" instead of "Boost"...
But I am not allowed to change:
SET(BOOST_ROOT "C:/boost")
to
SET(Boost_ROOT "C:/boost")
I'm using CLION and my Cmake now runs without error messages (finally...)
but if I try to build my program I get this error messages:
CMakeFiles\myFolder.dir/objects.a(main.cpp.obj): In function `_static_initialization_and_destruction_0':
C:/boost/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
C:/boost/boost/system/error_code.hpp:223: undefined reference to `boost::system::generic_category()'
C:/boost/boost/system/error_code.hpp:224: undefined reference to `boost::system::system_category()'
collect2.exe: error: ld returned 1 exit status
I have tried a lot of different solution provided here in previous questions but unluckily nothing worked so far...
I think I'm missing something obvious

Related

Getting tbb linker errors when including the <execution> header

I have been writing a visualisation tool using OpenGL. It has been compiling and linking just fine (using gcc 11.2.0) until I recently installed the Linux dependencies for OpenVDB (listed under Using UNIX apt-get). I am now getting linker errors that I have narrowed down to the inclusion of the <execution> header file:
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `tbb::detail::d1::execution_slot(tbb::detail::d1::execution_data const&)':
main.cpp:(.text._ZN3tbb6detail2d114execution_slotERKNS1_14execution_dataE[_ZN3tbb6detail2d114execution_slotERKNS1_14execution_dataE]+0x18): undefined reference to `tbb::detail::r1::execution_slot(tbb::detail::d1::execution_data const*)'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `tbb::detail::d1::current_thread_index()':
main.cpp:(.text._ZN3tbb6detail2d120current_thread_indexEv[_ZN3tbb6detail2d120current_thread_indexEv]+0x12): undefined reference to `tbb::detail::r1::execution_slot(tbb::detail::d1::execution_data const*)'
The above is from the following minimal example.
main.cpp:
#include <execution>
#include <iostream>
int main()
{
std::cout << "Hello, World!" << std::endl;
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
project(test)
set(CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 20)
add_executable(test main.cpp)
I have tried linking TBB with my project like so:
# Find and link TBB.
find_package(TBB REQUIRED)
include_directories(${TBB_INCLUDE_DIRS})
link_directories(${TBB_LIBRARY_DIRS})
add_definitions(${TBB_DEFINITIONS})
if(NOT TBB_FOUND)
message("Error: TBB not found")
endif(NOT TBB_FOUND)
add_executable(test main.cpp)
target_link_libraries(test ${TBB_LIBRARIES})
...and also adding the -ltbb linker flag (as per the answer to this post) using
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -ltbb")
However, this does not solve the issue.
What I find particularly strange is that I didn't use to have to link against tbb despite having included the <excution> header all along. Only after installing the OpenVDB dependencies has this become an issue.
Can anyone advise me on how to solve this (either by appropriately linking tbb, or not having to link it at all, as was the case before)? Any form of insight would be much appreciated.
I have managed to successfully link tbb as follows:
find_package(TBB REQUIRED COMPONENTS tbb)
add_executable(test main.cpp)
target_link_libraries(test tbb)
Although I am still clueless as to why I suddenly needed to link with tbb, considering I previously didn't need to.
Uninstalling libtbb-dev also works and dont require editing the CMakeFiles.txt.

Cmake undefined reference to `boost::gregorian::greg_month::as_short_string() const'

I spend whole day trying to link the data_time library to my c++ cmake project.
So I am using cmake 3.4 and boost 1.61.0.
I have the class where is a function which is taking local time:
void TestClass::getTime() {
this->endDate = boost::posix_time::second_clock::local_time();
}
Then I want to return the value of endDate by function returning string:
string testCalss::Info() {
return to_simple_string(this->beginningDate);
}
I need to convert the type of endDate variable to string, because function is string type.
I am getting error messages while program is linking:
In function boost::date_time::month_formatter<boost::gregorian::greg_month, boost::date_time::simple_format<char>, char>::format_month(boost::gregorian::greg_month const&, std::ostream&)':
/usr/include/boost/date_time/date_formatting.hpp:44: undefined reference to `boost::gregorian::greg_month::as_short_string() const'
/usr/include/boost/date_time/date_formatting.hpp:49: undefined reference to `boost::gregorian::greg_month::as_long_string() const'
I have read that data_time is not only header-only library and I should build and add it to my project.
I have tried this command gcc myapp.cpp -omyapp -lboost_date_time, and it's does not work because I am using g++ in cmake project and I haven't find nothing for g++.
I also tried this:
c++ -I path/to/boost_1_61_0 example.cpp -o example \
~/boost/stage/lib/libboost_regex-gcc34-mt-d-1_36.a
Its example from official boost docs how to link libraries.
In cmake project I have few cpp files. Should I run this command using file where is my converting command?
Is there any easier way to fix this error?
I found out how to solve the problem. The way I choose is simple. First of all I have used the locate */boost/date_time* command to find where is my boost and date_time libraries installed. I did not know what version of boost library I am using, so I used locate command again to find boost/version.hpp file. After that I added few lines to my CmakeLists.txt:
find_package( Boost 1.60.0 COMPONENTS date_time)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries( Program ${Boost_DATE_TIME_LIBRARY} )
endif()
Thats all.
cmake_minimum_required(VERSION 3.17)
project(boost_ptime)
set(CMAKE_CXX_STANDARD 17)
set(SOURCE_FILES main.cpp)
add_executable(boost_ptime ${SOURCE_FILES})
find_package( Boost 1.73 COMPONENTS date_time)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries( boost_ptime ${Boost_DATE_TIME_LIBRARY} )
endif()
Thanks to Warszawski Koks, I was able to compile my program in this way. Above is the full CMakeLists.txt file.

problems with cmake and boost-signals

I'm using cmake for project with boost, I've set boost-signals as required and cmake states it finds it, but when I compile the project I get a linking error.
I can resolve it with a linker directive set(CMAKE_EXE_LINKER_FLAGS -lboost_signals) but
this seems contrary to what it should be doing. Can someone suggest a better way? Thanks
in CMakeLists.txt
IF(UNIX)
find_package(Boost COMPONENTS system filesystem random regex signals thread program_options date_time REQUIRED)
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIRS} )
ENDIF()
Running cmake:
$ cmake ../
-- Boost version: 1.49.0
-- Found the following Boost libraries:
-- system
-- filesystem
-- random
-- regex
-- signals
-- thread
-- program_options
-- date_time
-- Configuring done
-- Generating done
-- Build files have been written to: /home/me/myproject/build
Linker error:
/usr/bin/ld: /usr/local/lib/libwt.so: undefined reference to symbol '_ZN5boost7signals9trackableD2Ev'
/usr/bin/ld: note: '_ZN5boost7signals9trackableD2Ev' is defined in DSO /usr/lib/libboost_signals.so.1.49.0 so try adding it to the linker command line
/usr/lib/libboost_signals.so.1.49.0: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
Boost Signals is not a header-only library. In addition to giving the include directory, you need to tell CMake to link against it too.
add_executable(MyProgram [...] )
target_link_libraries(MyProgram ${Boost_LIBRARIES})
In case you plan to build on Windows as well, you might also want to disable auto-linking:
add_definitions(-DBOOST_ALL_NO_LIB)
Note that Boost Signals has been deprecated and replaced by Signals2, which is in fact a header-only library. If it is up to you to make the choice, consider using Signals2 instead.

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 )

Google Earth C ++ API Library Errors on Ubuntu

I'm new to this forum, but I've seen it a few times while trying to search for a solution to this problem. I'm trying to generate KML files to use in Google Earth using a C++ API library I found for Ubuntu (libkml-dev_1.2.0-1ubuntu6_amd64). I installed the package using the command sudo apt-get install libkml-dev, came back successful. Afterwards, I used the command line terminal to navigate to the examples folder to try and execute the program 'helloworld.cc' with the command g++ helloworld.cc -o helloworld, but then got a slew of errors (mainly claiming that kmldom is an undefined reference). Sorry, I wanted to attach the text file, but don't know how so I included a sample of the error below. I've searched high and low all week, made sure the header files were indeed included in the download, and even contacted the Google Earth developers about the problem (and they responded that they have nothing to do with this and to redirect all questions regarding this issue to StackOverflow).
Does anyone know what is causing this problem(s) and what I can do to resolve this so I can move on please?
/tmp/cc5u2JyV.o: In function HelloKml(bool)': helloworld.cc:(.text+0x17): undefined reference to kmldom::KmlFactory::GetFactory()'
helloworld.cc:(.text+0x27): undefined reference to kmldom::KmlFactory::CreateCoordinates() const' helloworld.cc:(.text+0x328): undefined reference to kmldom::AsPoint(boost::intrusive_ptrkmldom::Element)'
/tmp/cc5u2JyV.o: In function boost::intrusive_ptr<kmldom::Coordinates>::intrusive_ptr(kmldom::Coordinates*, bool)': helloworld.cc:(.text._ZN5boost13intrusive_ptrIN6kmldom11CoordinatesEEC2EPS2_b[_ZN5boost13intrusive_ptrIN6kmldom11CoordinatesEEC5EPS2_b]+0x3d): undefined reference to kmlbase::intrusive_ptr_add_ref(kmlbase::Referent*)'
/tmp/cc5u2JyV.o: In function boost::intrusive_ptr<kmldom::Coordinates>::~intrusive_ptr()': helloworld.cc:(.text._ZN5boost13intrusive_ptrIN6kmldom11CoordinatesEED2Ev[_ZN5boost13intrusive_ptrIN6kmldom11CoordinatesEED5Ev]+0x23): undefined reference to kmlbase::intrusive_ptr_release(kmlbase::Referent*)'
/tmp/cc5u2JyV.o: In function boost::intrusive_ptr<kmldom::Geometry>::intrusive_ptr(boost::intrusive_ptr<kmldom::Geometry> const&)': helloworld.cc:(.text._ZN5boost13intrusive_ptrIN6kmldom8GeometryEEC2ERKS3_[_ZN5boost13intrusive_ptrIN6kmldom8GeometryEEC5ERKS3_]+0x35): undefined reference to kmlbase::intrusive_ptr_add_ref(kmlbase::Referent*)'
collect2: ld returned 1 exit status
When you compile application that uses library, you need to link it when your application compiles. So, try using this parameters:
To specify a directory to search for your libs, use -L:
-L/data[...]/lib
To specify the actual library name, use -l:
-labc (links abc.a or abc.so)
To specify a directory to search for include files, use -I:
-I/data[...]/lib
I've found the solution. The problem is that you are linking libkml incrorrectly. You should find the paths to the header files and to the library files (*.so). I found them here:
/usr/include/kml/ (include dit with headers),
/usr/lib/x86_64-linux-gnu/ (library dir).
I use CMake and CMakeLists.txt for a project. Using libkml may look like this:
`cmake_minimum_required (VERSION 3.2)
# Set language standard
set(CMAKE_CXX_STANDARD "11")
project (test_proj)
add_definitions(-std=c++11)
# Set default build type to RelWithDebInfo if not specified
if (NOT CMAKE_BUILD_TYPE)
message (STATUS "Default CMAKE_BUILD_TYPE not set using Release with Debug Info")
set (CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE
STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel"
FORCE)
endif()
# linking boost library
find_package(Boost COMPONENTS system filesystem thread REQUIRED)
if(NOT Boost_FOUND)
message(SEND_ERROR "Failed to find boost.")
return()
else()
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(test_proj main.cpp)
set (LibKML_INCLUDE_DIRS /usr/include/kml/)
set (LibKML_LIBRARIES /usr/lib/x86_64-linux-gnu/libkmlbase.so /usr/lib/x86_64-linux-gnu/libkmlconvenience.so /usr/lib/x86_64-linux-gnu/libkmldom.so /usr/lib/x86_64-linux-gnu/libkmlengine.so /usr/lib/x86_64-linux-gnu/libkmlregionator.so /usr/lib/x86_64-linux-gnu/libkmlxsd.so)
message("LibKML is at: ${LibKML_INCLUDE_DIRS} and ${LibKML_LIBRARIES}")
message("Boost is at: ${Boost_INCLUDE_DIRS} and ${Boost_LIBRARIES}")
target_include_directories(test_proj
PUBLIC
${LibKML_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS} )
target_link_libraries(test_proj
PUBLIC
${LibKML_LIBRARIES} ${Boost_LIBRARIES})
install(TARGETS test_proj EXPORT test_proj_export)
export(EXPORT test_proj_export FILE cmake/test_proj-targets.cmake)`