【发布时间】:2021-03-08 19:08:23
【问题描述】:
在使用带有预编译头 (PCH) 的 Boost.Test 时出现链接器错误,如果没有 PCH,该错误不会发生。我使用Usage variants 中描述的动态链接库。 如何修复错误以将 Boost.Test 也与 PCH 一起使用?
问题至少出现在 Fedora 和 boost 1.73(只有动态库)和 g++ 10/clang 11 中。
$ cmake ../ && make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/.../boost_test_pch/build
[ 33%] Building CXX object CMakeFiles/boost_utf_pch.dir/test_driver.cpp.o
[ 66%] Building CXX object CMakeFiles/boost_utf_pch.dir/test.cpp.o
[100%] Linking CXX executable boost_utf_pch
[100%] Built target boost_utf_pch
对比
$ cmake -DEDA_ENABLE_PCH=TRUE ../ && make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/.../boost_test_pch/build
[ 25%] Building CXX object CMakeFiles/boost_utf_pch.dir/cmake_pch.hxx.gch
[ 50%] Building CXX object CMakeFiles/boost_utf_pch.dir/test_driver.cpp.o
cc1plus: warning: /home/.../boost_test_pch/build/CMakeFiles/boost_utf_pch.dir/cmake_pch.hxx.gch: not used because `BOOST_TEST_DYN_LINK' is defined [-Winvalid-pch]
[ 75%] Building CXX object CMakeFiles/boost_utf_pch.dir/test.cpp.o
[100%] Linking CXX executable boost_utf_pch
/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/crt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/boost_utf_pch.dir/build.make:138: boost_utf_pch] Error 1
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/boost_utf_pch.dir/all] Error 2
make: *** [Makefile:103: all] Error 2
我对之前的警告信息无能为力...
这里是操场文件:
-
CMakeLists.txt:
project(boost_utf_pch LANGUAGES CXX) cmake_minimum_required(VERSION 3.18) add_executable(${PROJECT_NAME} "") find_package(Boost 1.73.0 REQUIRED COMPONENTS unit_test_framework) target_sources(${PROJECT_NAME} PRIVATE test_driver.cpp test.cpp) target_link_libraries(${PROJECT_NAME} PRIVATE Boost::unit_test_framework) set_source_files_properties(test_driver.cpp APPEND PROPERTIES COMPILE_DEFINITIONS "BOOST_TEST_DYN_LINK") option(EDA_ENABLE_PCH "Enable PCH" OFF) if (EDA_ENABLE_PCH) target_precompile_headers(${PROJECT_NAME} PRIVATE pch.hpp) endif() -
pch.hpp
#pragma once #include <boost/test/unit_test.hpp> -
test.cpp
#include <boost/test/unit_test.hpp> BOOST_AUTO_TEST_SUITE( my_test ) BOOST_AUTO_TEST_CASE( test_case1 ) { BOOST_TEST_WARN( sizeof(int) < 4U ); } BOOST_AUTO_TEST_SUITE_END() -
test_driver.cpp
#define BOOST_TEST_MODULE "Boost.UTF PCH Test Suite" #include <boost/test/unit_test.hpp>
【问题讨论】:
-
注意,使用“仅标题使用变体”并不是一个真正的选择,因为我有很多这样的测试用例/测试套件。
-
我不认为你可以,只需从你的预编译头文件中删除 boost 测试。预编译头文件中的所有头文件必须具有相同的
#defines
标签: c++ boost-test