1

I want to use Google Benchmark, for that I have a simple test written in main.cpp file. to build my project I have a CMake file as follow:

 cmake_minimum_required(VERSION 3.10)

include_directories(${CMAKE_SOURCE_DIR}/include)

find_library(BENCHMARK_LIBRARY NAMES benchmark HINTS "${CMAKE_SOURCE_DIR}/externals/lib")
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

# benchmark binary
add_executable(benchmark main.cpp)
target_link_libraries(benchmark ${BENCHMARK_LIBRARY} Threads::Threads)

But every time I run "make" it gives me the error:

In function `benchmark::RunSpecifiedBenchmarks(benchmark::BenchmarkReporter*, 
benchmark::BenchmarkReporter*)':
benchmark.cc:(.text+0x214b): undefined reference to `std::thread
::_M_start_thread(std::unique_ptr<std::thread::_State, 
std::default_delete<std::thread::_State> >, void (*)())'

What am I doing wrong? I just started working with CMake so I don't have a lot of knowledge. I searched but couldn't get a solution. Thanks in advance for any help.

4

1 回答 1

1

观察:这是一个链接器错误,代码编译正常,但是当链接器试图将所有对象和库组合成一个可执行文件时没有找到符号。

获取对 std::thread::_M_start_thread <- 相关问题的未定义引用

您可能安装了多个编译器/库版本,并且正在使用较新的 c++ 头文件进行编译并链接到较旧的 c++ 库。

于 2018-10-09T15:54:56.310 回答