[英]How to include -std=c++11 and -lpthread in makefile?
I tried the advice in this answer , but it's for GCC and didn't help anyways. 我在这个答案中尝试了这个建议,但它适用于GCC并且无论如何都没有帮助。
I want to #include <thread>
in a file, so I have a make file as the following: 我想在文件中
#include <thread>
,所以我有一个make文件如下:
OBJS = clitest.o Sources/NClient.o
CC = g++
DEBUG = -g
CFLAGS = -Wall -c $(DEBUG)
LFLAGS = -Wall $(DEBUG)
clitest: $(OBJS)
$(CC) $(LFLAGS) $(OBJS) -o clitest
Where should I include -std=c++11
and -lpthread
in this? 我应该在哪里包含
-std=c++11
和-lpthread
? I've tried just about every combination I can, but I continue to get this error when I run make
: 我已经尝试了几乎所有的组合,但是当我运行
make
时我仍然遇到这个错误:
/usr/include/c++/4.8.3/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
I believe this is the command it's running? 我相信这是它正在运行的命令?
[jaska@localhost NClient]make
g++ -c -o clitest.o clitest.cpp
Here's the source code file, too: 这里也是源代码文件:
#include <thread>
#include <string>
void task(std::string msg){
std::cout << msg << '\n';
}
...
...
std::thread t1(task, "message");
client->create();
t1.join();
Your makefile doesn't have an explicit rule for compiling the objects in $(OBJS)
so that means the implicit rule will be used, which is what produces this command: 你的makefile没有一个明确的规则来编译
$(OBJS)
的对象,这意味着将使用隐式规则,这就是产生这个命令的原因:
g++ -c -o clitest.o clitest.cpp
The implicit rule for turning a .cpp
file into a .o
file is approximately: 将
.cpp
文件转换为.o
文件的隐式规则大致为:
$(CXX) $(CXXFLAGS) -c -o $@ $<
so to add options that affect that rule you should add them to the CXXFLAGS
variable ( CFLAGS
is conventionally used for compiling C files, and CC
is the C compiler, the conventional variable for the C++ compiler is CXX
). 因此,要添加影响该规则的选项,您应该将它们添加到
CXXFLAGS
变量( CFLAGS
通常用于编译C文件, CC
是C编译器,C ++编译器的常规变量是CXX
)。
The -lpthread
option is a linker option, so need to be given during linking. -lpthread
选项是链接器选项,因此需要在链接期间给出。 You have defined your own rule for linking, so you should either add -lpthread
to that recipe or add it to the LFLAGS
variable. 您已经定义了自己的链接规则,因此您应该将
-lpthread
添加到该配方或将其添加到LFLAGS
变量。
NB using -Wall
and -g
during linking is useless, they have no effect. NB在链接期间使用
-Wall
和-g
是没用的,它们没有效果。
NB adding -c
to the CFLAGS
is wrong, the implicit rules for compiling .c
files already include -c
, just as the implicit one for C++ files includes -c
. NB将
-c
添加到CFLAGS
是错误的,编译.c
文件的隐式规则已经包含-c
,就像C ++文件的隐式规则包含-c
。 This doesn't cause any problems because the CFLAGS
variable is not used by your makefile, or by the implicit rule for compiling .cpp
files. 这不会导致任何问题,因为您的makefile不使用
CFLAGS
变量,也不会使用编译.cpp
文件的隐式规则。
NB instead of linking to -lpthread
you should compile and link with -pthread
NB而不是链接到
-lpthread
你应该编译并链接-pthread
NB the implicit rule for linking, and makefile convention, is to use the variable LDFLAGS
for linker options, and LIBS
for libraries such as -lpthread
, so I would rewrite your makefile as: NB链接和makefile约定的隐式规则是使用变量
LDFLAGS
作为链接器选项, LIBS
作为-lpthread
等库使用,所以我会将makefile重写为:
OBJS = clitest.o Sources/NClient.o
CXX = g++
DEBUG = -g
CXXFLAGS = -Wall $(DEBUG) -std=c++11 -pthread
LDFLAGS = -pthread
clitest: $(OBJS)
$(CXX) $(LDFLAGS) -o $@ $^ $(LIBS)
This still relies on the implicit rule for turning the .cpp
files into .o
files, but now that implicit rule will pick up the right options from the CXXFLAGS
variable. 这仍然依赖于将
.cpp
文件转换为.o
文件的隐式规则,但现在隐式规则将从CXXFLAGS
变量中选择正确的选项。
Add to CFLAGS
and LFLAGS
加入
CFLAGS
和LFLAGS
CFLAGS = -Wall -c $(DEBUG) -std=c++11 -pthread
LFLAGS = -Wall $(DEBUG) -std=c++11 -pthread
that should be enough. 那应该够了。
For implicit rules also overwrite the CXXFLAGS
variable 对于隐式规则,还会覆盖
CXXFLAGS
变量
CXXFLAGS = -Wall -c $(DEBUG) -std=c++11 -pthread
As you see from your command line output 正如您从命令行输出中看到的那样
[jaska@localhost NClient]make g++ -c -o clitest.o clitest.cpp
the other flags from CFLAGS
also weren't used for compiling. CFLAGS
的其他标志也不用于编译。
They are effectively command line parameters to gcc/g++ or ld, the linker. 它们实际上是gcc / g ++或ld(链接器)的命令行参数。 So they must be passed as additions to CFLAGS and LFLAGS (or possibly as CXXFLAGS, CPPFLAGS, LDFLAGS depending on how the Makefile functions).
所以它们必须作为CFLAGS和LFLAGS的补充传递(或者可能作为CXXFLAGS,CPPFLAGS,LDFLAGS,具体取决于Makefile的功能)。 Your example does not show you passing CFLAGS to the C++ compiler though, so that is also problematic.
您的示例并未显示您将CFLAGS传递给C ++编译器,因此这也存在问题。 Pass it explicitly or set the CXXFLAGS for your C++ compiler (C is not C++).
明确传递它或为您的C ++编译器设置CXXFLAGS(C不是C ++)。
I would suggest you resolve your problem initially on the command line and then when it works move on to the Makefile. 我建议你最初在命令行解决你的问题,然后当它工作时移动到Makefile。 Effectively you're trying to get Make to create the line:
实际上你正试图让Make来创建这条线:
g++ -Wall -g -std=c++11 -pthread clitest.o Sources/NCLient.o -o clitest
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.