我在将我的 c++ 项目组织为 cmake 项目时遇到了一些麻烦,这是我的新手。我正在尝试模仿这个 github 存储库给出的模板。我想我快到了,但是当我尝试使用以下命令进行编译时,我遇到了一些链接器错误:
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
有很多输出,但这里是前几行:
[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)'
我想这一定意味着我的markets/test/CMakeLists.txt
文件有问题,我现在有:
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)
有任何想法吗?其他问题似乎表明我可能以错误的顺序链接事物,但这个项目结构并不复杂(只是一个可执行文件、一个库和一些单元测试)。这是用我的旧 makefile 编译的,所以我怀疑这是因为我没有定义一些头文件定义。
如果有帮助,这是tree
我的根目录:markets/
.
├── 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
编辑:
这是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})