【发布时间】:2014-10-13 15:08:36
【问题描述】:
我正在尝试使用 CMake 2.8.12.2 使用 g++ 4.8.2 编译一个简单的 C++ 程序,该程序利用 C++11 功能和多线程。为此,必须使用编译器标志 -std=c++11 和 -pthread。据我了解,在 CMake 中,可以通过多种方式设置这些标志,一种是使用 set 命令:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
另一种(据说是首选的)方法是使用 add_compile_options 命令:
add_compile_options(-std=c++11)
add_compile_options(-pthread)
(或一行:add_compile_options(-std=c++11 -pthread))
所以,问题在于,就我而言,只有第一种方法有效 - 通过使用 set 命令。问题在于使用 add_compile_options 会导致编译的输出可执行文件崩溃并显示以下消息(就像根本没有指定 -pthread 一样):
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)
我测试的代码:
#include <future>
int main()
{
auto a = std::async(std::launch::async, [](){});
a.wait();
return 0;
}
从这个程序编译的事实,我可以推断出应用了-std=c++11。问题是为什么没有应用-pthread?
【问题讨论】:
-
如果您分享您的编译行输出可能会有所帮助。你能在上面看到你正在编译的对象的这些标志吗?
-
好吧,我不知道我能看到它,但是当我执行
make -n时我能想到。使用 add_compile_options 编译命令为:/usr/bin/c++ -g -pthread -std=c++11 -o CMakeFiles/test.dir/main.o -c /home/batman/stuff/testprojects/test/main.cpp使用 set,编译命令为:/usr/bin/c++ -pthread -std=c++11 -g -o CMakeFiles/test.dir/main.o -c /home/batman/stuff/testprojects/test/main.cpp
标签: c++ multithreading c++11 cmake