0

有人问我是否可以在我正在处理的项目中开始使用 cmake 而不是 Makefiles。我已经开始编译我的主项目,但是,我很难为使用 UnitTest++CMakeLists.txt的目录编写文件。test/我找到了这些说明,但我很难遵循它们。我跑

mkdir build cd build/ cmake .. make

但我得到一些链接器错误。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/目录中的所有 s 吗?我没有看到 g++ 编译这些的任何输出。

4

1 回答 1

0

这个答案建议添加-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++)
于 2019-09-12T12:49:45.783 回答