0

I've been asked to see if I can start using cmake instead of Makefiles for a project I'm working on. I've gotten it working to compile my main project, however, I'm having a hard time writing a CMakeLists.txt file for the test/ directory, which uses UnitTest++. I've found these instructions, but I'm having a hard time following them. I run

mkdir build cd build/ cmake .. make

but I get some linker errors. It mentions the phrase can not be used when making a PIE object; recompile with -fPIC a few times. Here's my CMakeLists.txt file:

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++)

As per the advice given [here], I've added set(CMAKE_POSITION_INDEPENDENT_CODE OFF), but to no avail.

Here's the entire output from running 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

Even though the error message isn't really suggesting this, could it be because it isn't recompiling all those .cpps in the directory that precedes the test/ directory? I don't see any output that g++ compiled those.

Taylor
  • 1,797
  • 4
  • 26
  • 51
  • Why are you adding `.h` files as source files??? This can't be right. – SergeyA Sep 11 '19 at 19:41
  • @SergeyA those are the headers for the classes the test `.cpp` files need. It's probably more common to compile the project as library, and link that, but I've done it this way here. – Taylor Sep 11 '19 at 19:42
  • You never ever list .h files as sources, no matter what your project is. – SergeyA Sep 11 '19 at 19:43
  • Headers are intended to be included, not compiled. – user4581301 Sep 11 '19 at 19:47
  • @user4581301 I'm generally aware of hwo the build process works. I think what I'm having difficulty with is the cmake commands. As you can see, I've resorted to the trial and error and approach. – Taylor Sep 11 '19 at 19:50
  • If I understand your terse error description correctly, 'recompile with -fPIC' means to reverse the setting, i.e. 'set(CMAKE_POSITION_INDEPENDENT_CODE ON)`. – vre Sep 11 '19 at 20:23
  • @vre that yields the same output – Taylor Sep 11 '19 at 20:42
  • Looks like libUnitTest++ is not compiled with -fPIC. When building a position independent executable (PIE) all libs and objects must be compiled with -fPIC. So either you do not add GCC_COVERAGE_COMPILE_FLAGS to CMAKE_CXX_FLAGS or need to rebuild libUnitTest++.a with -fPIC. – vre Sep 11 '19 at 20:47
  • @vre I've tried the first one a few different ways, and still no luck. Not sure if I should try the second. – Taylor Sep 12 '19 at 12:22

1 Answers1

0

This answer suggests adding the -no-pie flag, and that "fixed" it. My current cmake file is as follows (it still doesn't work, but at least these specific linker errors aren't there anymore).

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++)
Taylor
  • 1,797
  • 4
  • 26
  • 51