I have a C++ file in a project that uses multithreading and I can’t compile it without adding the compiler flag “-pthread”. I am compiling using CMake and want to add the flag for only this one file.
The “setup” is:
add_executable(someClass thisClass.cpp)
The one solution that works is set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -pthread”) but I understand this is bad practice.
Other things I have seen and tried that didn’t work are:
target_compile_options(someClass PRIVATE -pthread)
set_target_properties(someClass PROPERTIES COMPILE_OPTIONS -pthread)
I am not super familar with CMake so I don’t know if quotations actually do much but I have also tried adding quotations around -pthread.
For these other solutions, I know they don’t work because the build fails and because I run it with
make VERBOSE=1
and don’t see the flag being passed to the compiler.
Thanks for your time and help!
VSL612 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.