我正在尝试为我正在处理的项目中的CMakeLists.txt
目录编写一个test/
文件。 test/
的目录树是
test/
├── CMakeLists.txt
├── data_handlers.o
├── Makefile
├── run_tests.cpp
├── test_data
│ ├── QLD.csv
│ └── SPY.csv
├── 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
我正在做的不寻常的事情是这些测试正在测试的所有源文件都没有编译为库,并且该目录也有自己的CMakeLists.txt
。因此,属于test/
的cmake文件也必须编译并链接这些家伙。但是,我收到一百万个undefined reference
错误,并且找不到在此test/
目录之外定义的内容。
这里是test/CMakeLists.txt
:
cmake_minimum_required(VERSION 3.10)
project(markets_tests)
set(CMAKE_CXX_STANDARD 17)
SET(CMAKE_CXX_COMPILER /usr/bin/g++-8)
set(CMAKE_POSITION_INDEPENDENT_CODE OFF)
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
find_package(UnitTest++ REQUIRED)
SET(GCC_COVERAGE_COMPILE_FLAGS "-no-pie")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
include_directories(
"${PROJECT_SOURCE_DIR}/../include"
${UTPP_INCLUDE_DIRS}
)
file(GLOB all_SRCS
"${PROJECT_SOURCE_DIR}/*.cpp"
"${PROJECT_SOURCE_DIR}/../src/data_handlers.cpp"
"${PROJECT_SOURCE_DIR}/../src/data_readers.cpp"
"${PROJECT_SOURCE_DIR}/../src/data_execution_handler.cpp"
"${PROJECT_SOURCE_DIR}/../src/fill.cpp"
"${PROJECT_SOURCE_DIR}/../src/instrument.cpp"
"${PROJECT_SOURCE_DIR}/../src/market_bar.cpp"
"${PROJECT_SOURCE_DIR}/../src/market_snapshot.cpp"
"${PROJECT_SOURCE_DIR}/../src/model_bank.cpp"
"${PROJECT_SOURCE_DIR}/../src/order.cpp"
"${PROJECT_SOURCE_DIR}/../src/pnl_calculator.cpp"
"${PROJECT_SOURCE_DIR}/../src/portfolio.cpp"
"${PROJECT_SOURCE_DIR}/../src/position_summary.cpp"
)
add_executable(run_tests ${all_SRCS})
target_link_libraries (run_tests Eigen3::Eigen stdc++fs UnitTest++)
这是整个项目的树
t@t-XPS-13-9365:~/markets$ tree
.
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ │ ├── 3.10.2
│ │ │ ├── CMakeCCompiler.cmake
│ │ │ ├── CMakeCXXCompiler.cmake
│ │ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ │ ├── CMakeSystem.cmake
│ │ │ ├── CompilerIdC
│ │ │ │ ├── a.out
│ │ │ │ ├── CMakeCCompilerId.c
│ │ │ │ └── tmp
│ │ │ └── CompilerIdCXX
│ │ │ ├── a.out
│ │ │ ├── CMakeCXXCompilerId.cpp
│ │ │ └── tmp
│ │ ├── cmake.check_cache
│ │ ├── CMakeDirectoryInformation.cmake
│ │ ├── CMakeOutput.log
│ │ ├── CMakeTmp
│ │ ├── feature_tests.bin
│ │ ├── feature_tests.c
│ │ ├── feature_tests.cxx
│ │ ├── Makefile2
│ │ ├── Makefile.cmake
│ │ ├── progress.marks
│ │ ├── run_backtest.dir
│ │ │ ├── build.make
│ │ │ ├── cmake_clean.cmake
│ │ │ ├── CXX.includecache
│ │ │ ├── DependInfo.cmake
│ │ │ ├── depend.internal
│ │ │ ├── depend.make
│ │ │ ├── flags.make
│ │ │ ├── link.txt
│ │ │ ├── progress.make
│ │ │ └── src
│ │ │ ├── data_handlers.cpp.o
│ │ │ ├── data_readers.cpp.o
│ │ │ ├── execution_handler.cpp.o
│ │ │ ├── fill.cpp.o
│ │ │ ├── instrument.cpp.o
│ │ │ ├── main.cpp.o
│ │ │ ├── market_bar.cpp.o
│ │ │ ├── market_snapshot.cpp.o
│ │ │ ├── model_bank.cpp.o
│ │ │ ├── order.cpp.o
│ │ │ ├── pnl_calculator.cpp.o
│ │ │ ├── portfolio.cpp.o
│ │ │ └── position_summary.cpp.o
│ │ └── TargetDirectories.txt
│ ├── cmake_install.cmake
│ ├── Makefile
│ └── run_backtest
├── CMakeLists.txt
├── data
│ ├── QLD.csv
│ └── SPY.csv
├── include
│ ├── data_handlers.h
│ ├── data_readers.h
│ ├── execution_handler.h
│ ├── fill.h
│ ├── instrument.h
│ ├── market_bar.h
│ ├── market_snapshot.h
│ ├── model_bank.h
│ ├── order.h
│ ├── pnl_calculator.h
│ ├── portfolio.h
│ └── position_summary.h
├── README.md
├── scripts
│ └── download_raw_daily_data.r
├── src
│ ├── data_handlers.cpp
│ ├── data_readers.cpp
│ ├── execution_handler.cpp
│ ├── fill.cpp
│ ├── instrument.cpp
│ ├── main.cpp
│ ├── market_bar.cpp
│ ├── market_snapshot.cpp
│ ├── model_bank.cpp
│ ├── order.cpp
│ ├── pnl_calculator.cpp
│ ├── portfolio.cpp
│ └── position_summary.cpp
└── test
├── CMakeLists.txt
├── data_handlers.o
├── Makefile
├── run_tests.cpp
├── test_data
│ ├── QLD.csv
│ └── SPY.csv
├── 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
我尝试考虑了Squareskittles在下面提出的解决方案。这是我在顶层目录中的CMakeLists.txt
:
cmake_minimum_required(VERSION 3.10)
project(markets)
set(CMAKE_CXX_STANDARD 17)
SET(CMAKE_CXX_COMPILER /usr/bin/g++-8)
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
include_directories(
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/src
)
file(GLOB all_SRCS
"${PROJECT_SOURCE_DIR}/include/*.h"
"${PROJECT_SOURCE_DIR}/src/*.cpp"
)
add_executable(run_backtest ${all_SRCS})
add_library(MarketLibrary ${all_SRCS})
add_subdirectory(test)
target_link_libraries (run_backtest Eigen3::Eigen stdc++fs)
当我尝试cmake ..
时,它将永远运行。
答案 0 :(得分:0)
如果顶级CMakeLists.txt
文件创建了库(例如MarketLibrary
),则可以引用它并将其链接到其他CMake文件中的测试可执行文件。这应该有助于解决未定义的参考错误:
CMakeLists.txt :
...
add_library(MarketLibrary
src/data_handlers.cpp
src/data_readers.cpp
...
)
...
add_subdirectory(test)
test / CMakeLists.txt (修改您的target_link_libraries()
命令):
project(markets_tests)
...
add_executable(run_tests ${all_SRCS})
target_link_libraries(run_tests Eigen3::Eigen stdc++fs UnitTest++ MarketLibrary)