I've tried including boost into a cmake project of mine, and I'm having some difficulty getting the project to compile. Specifically, I would like to use things defined in the boost/filesystem.hpp header.
I've installed boost using apt install libboost-all-dev, and I've tried making the necessary adjustments to the cmake files in the necessary places, recommended by a few posts from this website. However, the following CMakeLists.txt file is not working for me (there are a few, but this is the only one that should reference the new boost headers and libraries):
project(run_backtest) add_subdirectory(markets) set(SOURCE_FILES main.cpp) set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) find_package(Boost 1.65.1 COMPONENTS system filesystem) find_package (Eigen3 3.3 REQUIRED NO_MODULE) include_directories(${Boost_INCLUDE_DIRS}) link_directories( ${Boost_LIBRARIES}) find_library(mysqlcppconn 1.1.12 REQUIRED) add_executable(run_backtest ${SOURCE_FILES}) target_link_libraries(run_backtest markets Eigen3::Eigen stdc++fs mysqlcppconn ${Boost_LIBRARIES}) install(TARGETS run_backtest DESTINATION ${MARKETS_INSTALL_BIN_DIR}) I navigate to a directory called ~/markets/build/manual and type in cmake ../... That looks like it works; it prints this out:
taylor@taylor-XPS-13-9365:~/markets/build/manual$ cmake ../.. -- The C compiler identification is GNU 7.4.0 -- The CXX compiler identification is GNU 7.4.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Boost version: 1.65.1 -- Found the following Boost libraries: -- system -- filesystem /usr/lib/x86_64-linux-gnu/libboost_system.so/usr/lib/x86_64-linux-gnu/libboost_filesystem.so -- Configuring done -- Generating done -- Build files have been written to: /home/taylor/markets/build/manual Then make && make install results in some undefined reference to errors. Here's the first one copy/pasted:
../src/markets/libmarkets.a(data_readers.cpp.o): In function `number_of_files_in_directory(boost::filesystem::path)': data_readers.cpp:(.text+0x28b): undefined reference to `boost::filesystem::detail::directory_iterator_construct(boost::filesystem::directory_iterator&, boost::filesystem::path const&, boost::system::error_code*)' The top of the data_readers.h file looks like this:
. . . #include <string> #include <vector> #include <queue> #include <boost/filesystem.hpp> . . . Looking at the cmake docs, I've noticed there are a tremendous amount of variables that give the library directory. Perhaps I'm using the worng one?
${Boost_LIBRARIES}variable? Have you tried printing this to see if the libraries are actually found?Boost_LIBRARIES(list of library names) forlink_directories. I think you are supposed to useBoost_LIBRARY_DIRSthere. Also you should addREQUIREDtofind_packagewhen looking for boost.message( ${Boost_LIBRARIES}), and i've added the output of callingcmake ../..to the body of the questionboost::filesystem, haven't you? Also, according to the error message, it islibmarkets.awhich cause the linker error, which is built inadd_subdirectory(markets)withoutfind_package(Boost)in effect. This is not a very bad - static libraries are not linked at creation - but this smells no good.