"undefined reference to" errors in my cmake project

674 Views Asked by At

I'm having a bit of trouble organizing my c++ project as a cmake project, which is something I'm new to. I'm trying to mimic the template given by this github repository. I think I'm almost there, but I'm getting a few linker errors when I try to compile with the following commands:

cd markets
rm -rf build/manual
mkdir build/manual
cd build/manual
cmake -G "Unix Makefiles" -D CMAKE_CXX_COMPILER=/usr/bin/g++-8 ../..
make

There's a lot of output, but here are the first few lines:

[100%] Linking CXX executable markets_tests
CMakeFiles/markets_tests.dir/src/test_data_handler.cpp.o: In function `Testtest_data_handler::RunImpl() const':
test_data_handler.cpp:(.text+0x12a): undefined reference to `Instrument::Instrument(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test_data_handler.cpp:(.text+0x153): undefined reference to `Instrument::Instrument(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test_data_handler.cpp:(.text+0x37f): undefined reference to `DataHandler::DataHandler(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, unsigned int const&, unsigned int const&)'
CMakeFiles/markets_tests.dir/src/test_data_reader.cpp.o: In function `Testtest_csv_reader::RunImpl() const':
test_data_reader.cpp:(.text+0xa9): undefined reference to `csv_reader(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int)'

I guess this must mean that something is wrong with my markets/test/CMakeLists.txt file, which I have now as:

cmake_minimum_required(VERSION 3.10)
project(markets_tests)


find_package(UnitTest++ REQUIRED)
find_package(Eigen3 3.3 REQUIRED NO_MODULE)

SET(GCC_COVERAGE_COMPILE_FLAGS "-no-pie")
SET(CMAKE_CXX_FLAGS  "${GCC_COVERAGE_COMPILE_FLAGS}")

include_directories(${MARKETS_HEADERS_DIR})
include_directories(${UTPP_INCLUDE_DIRS})

set(SOURCE_FILES main.cpp 
         src/test_data_handler.cpp
         src/test_data_reader.cpp
         src/test_exec_handler.cpp
         src/test_fill.cpp
         src/test_instrument.cpp
         src/test_market_bar.cpp
         src/test_market_snapshot.cpp
         src/test_order.cpp
         src/test_pnl_calculator.cpp
         src/test_portfolio.cpp
         src/test_position_summary.cpp) 

add_executable(markets_tests ${SOURCE_FILES})
target_link_libraries(markets_tests markets Eigen3::Eigen UnitTest++)  # added Eigen
install(TARGETS markets_tests DESTINATION bin)

Any ideas? Other questions seem to suggest that I could be linking things in the wrong order, but this project structure isn't that complicated (just an executable, a library, and some unit tests). This was compiling with my old makefile, so I doubt it's because I haven't defined some of the header definitions.

Here is the tree of my markets/ root directory if it helps:

.
├── bin
├── build
│   └── manual
├── CMakeLists.txt
├── docs
├── lib
├── README.md
├── src
│   ├── CMakeLists.txt
│   ├── main.cpp
│   └── markets
│       ├── CMakeLists.txt
│       ├── data_handlers.cpp
│       ├── data_handlers.h
│       ├── data_readers.cpp
│       ├── data_readers.h
│       ├── execution_handler.cpp
│       ├── execution_handler.h
│       ├── fill.cpp
│       ├── fill.h
│       ├── instrument.cpp
│       ├── instrument.h
│       ├── market_bar.cpp
│       ├── market_bar.h
│       ├── market_snapshot.cpp
│       ├── market_snapshot.h
│       ├── order.cpp
│       ├── order.h
│       ├── pnl_calculator.cpp
│       ├── pnl_calculator.h
│       ├── portfolio.cpp
│       ├── portfolio.h
│       ├── position_summary.cpp
│       └── position_summary.h
└── test
    ├── CMakeLists.txt
    ├── main.cpp
    ├── src
    │   ├── test_data_handler.cpp
    │   ├── test_data_reader.cpp
    │   ├── test_exec_handler.cpp
    │   ├── test_fill.cpp
    │   ├── test_instrument.cpp
    │   ├── test_market_bar.cpp
    │   ├── test_market_snapshot.cpp
    │   ├── test_order.cpp
    │   ├── test_pnl_calculator.cpp
    │   ├── test_portfolio.cpp
    │   └── test_position_summary.cpp
    └── test_data
        ├── QLD.csv
        └── SPY.csv

Edit:

Here is src/markets/CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(markets)### C CXX)

set(SOURCE_FILES
    data_handlers.h
    data_handlers.cpp
    data_readers.h
    data_readers.cpp
    execution_handler.h
    execution_handler.cpp
    fill.h
    fill.cpp
    instrument.h
    instrument.cpp
    market_bar.h
    market_bar.cpp
    market_snapshot.h
    market_snapshot.cpp
    order.h
    order.cpp
    pnl_calculator.h
    pnl_calculator.cpp
    portfolio.h
    portfolio.cpp
    position_summary.h
    position_summary.cpp
)

find_package (Eigen3 3.3 REQUIRED NO_MODULE)
add_library(markets SHARED STATIC ${SOURCE_FILES})
target_link_libraries(markets Eigen3::Eigen stdc++fs) #added this

install(TARGETS markets DESTINATION ${MARKETS_INSTALL_LIB_DIR})
install(FILES markets.h DESTINATION ${MARKETS_INSTALL_INCLUDE_DIR})
0

There are 0 best solutions below