Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么CMake似乎没有使用add\u compile\u options命令应用-pthread? 我尝试使用C++4.2.2编译一个简单的C++程序,它使用C++ 11的特性和多线程。为此,必须使用编译器标志-std=c++11和-pthread。据我所知,在CMake中,可以通过多种方式设置这些标志,一种是使用set命令: set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")_C++_Multithreading_C++11_Cmake - Fatal编程技术网

为什么CMake似乎没有使用add\u compile\u options命令应用-pthread? 我尝试使用C++4.2.2编译一个简单的C++程序,它使用C++ 11的特性和多线程。为此,必须使用编译器标志-std=c++11和-pthread。据我所知,在CMake中,可以通过多种方式设置这些标志,一种是使用set命令: set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")

为什么CMake似乎没有使用add\u compile\u options命令应用-pthread? 我尝试使用C++4.2.2编译一个简单的C++程序,它使用C++ 11的特性和多线程。为此,必须使用编译器标志-std=c++11和-pthread。据我所知,在CMake中,可以通过多种方式设置这些标志,一种是使用set命令: set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread"),c++,multithreading,c++11,cmake,C++,Multithreading,C++11,Cmake,另一种(据说是首选)方法是使用add_compile_options命令: add_compile_options(-std=c++11) add_compile_options(-pthread) (或者在一行中:添加编译选项(-std=c++11-pthread)) 所以,问题是在我的例子中,只有第一种方法有效——通过使用set命令。问题在于使用add_compile_选项会导致编译输出可执行文件崩溃,并显示以下消息(就像根本没有指定-pthread一样): 我测试的代码是: #inclu

另一种(据说是首选)方法是使用add_compile_options命令:

add_compile_options(-std=c++11)
add_compile_options(-pthread)
(或者在一行中:
添加编译选项(-std=c++11-pthread)

所以,问题是在我的例子中,只有第一种方法有效——通过使用set命令。问题在于使用add_compile_选项会导致编译输出可执行文件崩溃,并显示以下消息(就像根本没有指定-pthread一样):

我测试的代码是:

#include <future>

int main()
{
    auto a = std::async(std::launch::async, [](){});
    a.wait();
    return 0;
}
#包括
int main()
{
自动a=std::async(std::launch::async,[](){});
a、 等待();
返回0;
}

根据这个程序编译的事实,我可以推断应用了-std=c++11。问题是为什么不应用-pthread?

假设您确实是使用g++编译的(只需通过检查
/usr/bin/c++
实际上是什么来澄清-我假设它是到g++的链接),g++的手册页给出:

gcc [-c|-S|-E] [-std=standard]
    [-g] [-pg] [-Olevel]
    [-Wwarn...] [-pedantic]
    [-Idir...] [-Ldir...]
    [-Dmacro[=defn]...] [-Umacro]
    [-foption...] [-mmachine-option...]
    [-o outfile] [@file] infile...
这意味着您需要:

g++ -std=c++11 -g -pthread ...
按这个顺序

也许值得尝试通过这种调整手动运行您的命令行,以查看生成的内容是否如您所期望的那样工作

在我看来(在没有任何研究的情况下,我会补充说)
add\u compile\u options
可能只对添加严格的编译器选项有用,因此,对于所有其他编译器选项,您可能必须使用
set
来设置
-std=c++11
添加编译选项
,即在
-g

之后指定,请尝试以下操作:

set (
  CMAKE_CXX_FLAGS
  "${CMAKE_CXX_FLAGS} "
  "-Wall -Werror -pthread -std=c++11 -Wl,--no-as-needed"
)

来源:

如果共享编译行输出,可能会有所帮助。你能在上面看到你正在编译的对象的这些标志吗?好的,我不知道我能看到它,但我可以在执行
make-n
时识别出来。使用add_compile_选项,编译命令是:
/usr/bin/c++-g-pthread-std=c++11-o CMakeFiles/test.dir/main.o-c/home/batman/stuff/testprojects/test/main.cpp
With set,命令是:
/usr/bin/c++-pthread-std=c++11-g-ocmakefiles/test.dir/main.o-c/home/batman/stuff/testprojects/test/main.cpp
我想我现在明白发生了什么。如果我使用add_compile_选项设置-std=c++11,但是使用-pthread如下:
set(CMAKE_EXE_LINKER_FLAGS“${CMAKE_EXE_LINKER_FLAGS}-pthread”)
则应用程序运行时不会出现错误。我的假设是-pthread是一个链接器标志,但CMake不会将add_compile_选项传递给链接器,因此必须单独添加它。为什么将两个标志都设置为CMAKE_CXX_标志有效,是因为这两个标志(-std=c++11和-pthread)都被添加到编译和链接这两个阶段。好的,添加-pthread的更好方法是像这样使用target_link_libraries命令:
target_link_libraries(projectname-pthread)
set (
  CMAKE_CXX_FLAGS
  "${CMAKE_CXX_FLAGS} "
  "-Wall -Werror -pthread -std=c++11 -Wl,--no-as-needed"
)