1

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?

Taylor
  • 1,797
  • 4
  • 26
  • 51
  • 2
    What is the contents of the `${Boost_LIBRARIES}` variable? Have you tried printing this to see if the libraries are actually found? – Kevin Sep 24 '19 at 15:55
  • 1
    You are using `Boost_LIBRARIES` (list of library names) for `link_directories`. I think you are supposed to use `Boost_LIBRARY_DIRS` there. Also you should add `REQUIRED` to `find_package` when looking for boost. – user7860670 Sep 24 '19 at 15:56
  • @squareskittles I added `message( ${Boost_LIBRARIES})`, and i've added the output of calling `cmake ../..` to the body of the question – Taylor Sep 24 '19 at 20:01
  • @VTT still no dice. same errors – Taylor Sep 24 '19 at 20:09
  • 1
    You have checked [this question](https://stackoverflow.com/questions/35007134/c-boost-undefined-reference-to-boostfilesystemdetailcopy-file) about unresolved reference to the `boost::filesystem`, haven't you? Also, according to the error message, it is `libmarkets.a` which cause the linker error, which is built in `add_subdirectory(markets)` **without** `find_package(Boost)` in effect. This is not a very bad - static libraries are not linked at creation - but this smells no good. – Tsyvarev Sep 24 '19 at 20:58
  • @Tsyvarev ah yes, the only place I'm using boost is in that library – Taylor Sep 24 '19 at 21:43

1 Answers1

1

Here i've made a few modifications to your CMakeLists.txt and added a few comments for clarity.

project(run_backtest)

add_subdirectory(markets)
set(SOURCE_FILES main.cpp)

# You shouldn't need these, i got my copy working without them 
# set(Boost_USE_STATIC_LIBS OFF) 
# set(Boost_USE_MULTITHREADED ON)  
# set(Boost_USE_STATIC_RUNTIME OFF) 
# Here i added 'Required' so that the build will fail if it cannot find boost
find_package(Boost 1.65.1 REQUIRED COMPONENTS system filesystem) 

find_package (Eigen3 3.3 REQUIRED NO_MODULE)

# You shouldn't have to use find_library, it's a lower level command
# and find_package should encapsulate it's functionality
find_package (mysqlcppconn 1.1.12 REQUIRED)

add_executable(run_backtest ${SOURCE_FILES})

# Here i replaced the variables (e.g ${BOOST_Libraries}) with Boost::system
# This is called an 'alias'. They're added by the Find_Package() call.
target_link_libraries(run_backtest markets
    Eigen3::Eigen
    stdc++fs
    mysqlcppconn
    Boost::system)
install(TARGETS run_backtest DESTINATION ${MARKETS_INSTALL_BIN_DIR})
vpaladino778
  • 157
  • 14
  • Thanks that's very helpful (+1) – Taylor Sep 24 '19 at 22:27
  • @Taylor Were you able to get it working with the changes that i suggested? One other thing i thought of, if your compiling targeting x64 and your boost is x32, sometimes that can be the cause of the trouble. – vpaladino778 Sep 25 '19 at 20:58
  • I was able to get it working, but not from your information. Sorry if that sounds mean, but it was my fault...the question was ill-posed, and I posted a follow up question to this that got everything squared away for me. – Taylor Sep 26 '19 at 14:39