如何为UnitTest ++项目编写CMakeLists.txt文件

时间:2019-09-11 19:40:05

标签: c++ unit-testing cmake linker unittest++

有人要求我查看是否可以开始为正在工作的project使用cmake而不是Makefile。我已经完成了编译主项目的工作,但是,我很难为使用UnitTest ++的CMakeLists.txt目录编写test/文件。我找到了这些instructions,但在跟踪它们时遇到了困难。我跑

mkdir构建 光盘构建/ cmake .. 做

但是出现一些链接器错误。它多次提到短语can not be used when making a PIE object; recompile with -fPIC。这是我的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 "-fPIC")
SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")

include_directories(
        "${PROJECT_SOURCE_DIR}"
        "${PROJECT_SOURCE_DIR}/../include"
        "${PROJECT_SOURCE_DIR}/../src"
        ${UTPP_INCLUDE_DIRS}
    )


file(GLOB all_SRCS
        "${PROJECT_SOURCE_DIR}/*.cpp"
        "${PROJECT_SOURCE_DIR}/../include/data_handlers.h"
        "${PROJECT_SOURCE_DIR}/../include/data_readers.h"
        "${PROJECT_SOURCE_DIR}/../include/data_execution_handler.h"
        "${PROJECT_SOURCE_DIR}/../include/fill.h"
        "${PROJECT_SOURCE_DIR}/../include/instrument.h"
        "${PROJECT_SOURCE_DIR}/../include/market_bar.h"
        "${PROJECT_SOURCE_DIR}/../include/market_snapshot.h"
        "${PROJECT_SOURCE_DIR}/../include/model_bank.h"
        "${PROJECT_SOURCE_DIR}/../include/order.h"
        "${PROJECT_SOURCE_DIR}/../include/pnl_calculator.h"
        "${PROJECT_SOURCE_DIR}/../include/portfolio.h"
        "${PROJECT_SOURCE_DIR}/../include/position_summary.h"
        "${PROJECT_SOURCE_DIR}/../src/data_handlers.h"
        "${PROJECT_SOURCE_DIR}/../src/data_readers.h"
        "${PROJECT_SOURCE_DIR}/../src/data_execution_handler.h"
        "${PROJECT_SOURCE_DIR}/../src/fill.h"
        "${PROJECT_SOURCE_DIR}/../src/instrument.h"
        "${PROJECT_SOURCE_DIR}/../src/market_bar.h"
        "${PROJECT_SOURCE_DIR}/../src/market_snapshot.h"
        "${PROJECT_SOURCE_DIR}/../src/model_bank.h"
        "${PROJECT_SOURCE_DIR}/../src/order.h"
        "${PROJECT_SOURCE_DIR}/../src/pnl_calculator.h"
        "${PROJECT_SOURCE_DIR}/../src/portfolio.h"
        "${PROJECT_SOURCE_DIR}/../src/position_summary.h"
        )

add_executable(run_tests ${all_SRCS})
target_link_libraries (run_tests Eigen3::Eigen stdc++fs UnitTest++)

根据[此处]的建议,我添加了set(CMAKE_POSITION_INDEPENDENT_CODE OFF),但无济于事。

这是运行make的全部输出:

t@t-XPS-13-9365:~/markets/test/build$ make
Scanning dependencies of target run_tests
[  7%] Building CXX object CMakeFiles/run_tests.dir/run_tests.cpp.o
[ 15%] Building CXX object CMakeFiles/run_tests.dir/test_data_handler.cpp.o
[ 23%] Building CXX object CMakeFiles/run_tests.dir/test_data_reader.cpp.o
[ 30%] Building CXX object CMakeFiles/run_tests.dir/test_exec_handler.cpp.o
[ 38%] Building CXX object CMakeFiles/run_tests.dir/test_fill.cpp.o
[ 46%] Building CXX object CMakeFiles/run_tests.dir/test_instrument.cpp.o
[ 53%] Building CXX object CMakeFiles/run_tests.dir/test_market_bar.cpp.o
[ 61%] Building CXX object CMakeFiles/run_tests.dir/test_market_snapshot.cpp.o
[ 69%] Building CXX object CMakeFiles/run_tests.dir/test_order.cpp.o
[ 76%] Building CXX object CMakeFiles/run_tests.dir/test_pnl_calculator.cpp.o
[ 84%] Building CXX object CMakeFiles/run_tests.dir/test_portfolio.cpp.o
[ 92%] Building CXX object CMakeFiles/run_tests.dir/test_position_summary.cpp.o
[100%] Linking CXX executable run_tests
/usr/bin/ld: /usr/local/lib/libUnitTest++.a(MemoryOutStream.cpp.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: /usr/local/lib/libUnitTest++.a(RequiredCheckException.cpp.o): relocation R_X86_64_32S against symbol `_ZTVN8UnitTest22RequiredCheckExceptionE' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: /usr/local/lib/libUnitTest++.a(Test.cpp.o): relocation R_X86_64_32 against `.bss' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: /usr/local/lib/libUnitTest++.a(TestRunner.cpp.o): relocation R_X86_64_32S against symbol `_ZTVN8UnitTest18TestReporterStdoutE' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: /usr/local/lib/libUnitTest++.a(CurrentTest.cpp.o): relocation R_X86_64_32 against `.bss' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: /usr/local/lib/libUnitTest++.a(SignalTranslator.cpp.o): relocation R_X86_64_32S against `.text' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: /usr/local/lib/libUnitTest++.a(AssertException.cpp.o): relocation R_X86_64_32S against symbol `_ZTVN8UnitTest15AssertExceptionE' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: /usr/local/lib/libUnitTest++.a(TestReporterStdout.cpp.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
CMakeFiles/run_tests.dir/build.make:381: recipe for target 'run_tests' failed
make[2]: *** [run_tests] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/run_tests.dir/all' failed
make[1]: *** [CMakeFiles/run_tests.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

即使错误消息并未真正暗示这一点,也可能是因为它没有重新编译.cpp目录之前的目录中的所有test/吗?我看不到g ++编译它们的任何输出。

1 个答案:

答案 0 :(得分:0)

This answer建议添加-no-pie标志,并对其进行“修复”。我当前的cmake文件如下(它仍然不起作用,但至少这些特定的链接器错误不再存在)。

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}"
        "${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++)