Environment
ubuntu 16.04, gcc 5.4.0, cmake 3.5.1
Question
- target_link_libraries(promise pthread)
- target_link_libraries(promise -pthread)
- target_link_libraries(promise -lpthread)
What's the differences, which is better ?
Problem
promise.cpp
std::promise<int> pr;
auto fut = pr.get_future();
pr.set_value(10); // throw std::exception and terminate
CMakeLists.txt
add_executable(promise promise.cpp)
target_link_libraries(promise pthread)
Solution
Modify CMakeLists.txt slightly.
add_executable(promise promise.cpp)
target_link_libraries(promise -pthread)
I found the answer from here. But I don't know why ?
But, the best solution is portable.
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)
target_link_libraries(promise Threads::Threads)