C++ Makefile修改支持c++11

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10787126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:27:14  来源:igfitidea点击:

Makefile modification to support c++11

c++makefilec++11

提问by Ari

I am able to compile a single file using gcc with -std=c++0x option. But I can't do this through makefile. Here are the set of flags in my makefile (which after make complains about c++11 keywords):

我可以使用带有 -std=c++0x 选项的 gcc 编译单个文件。但是我不能通过makefile来做到这一点。这是我的 makefile 中的一组标志(在 make 之后抱怨 c++11 关键字):

MACHINE = $(shell echo `uname -s`-`uname -m` | sed "s/ //g")
CCC     = CC
CCC     = g++
CFLAGS  = -O3
CFLAGS  = -std=c++0x
CFLAGS  = -pg -D_DEBUG -g -c -Wall
LFLAGS  = -O
LFLAGS  = -pg -g

What am I missing?

我错过了什么?

Edit: I changed it to the following, but I still get compile errors, which I don't get with command line gcc invocation.

编辑:我将其更改为以下内容,但仍然出现编译错误,而命令行 gcc 调用不会出现这些错误。

CXXFLAGS=-O3 -std=c++0x -pg -D_DEBUG -g -c -Wall

回答by mfontanini

This way your makefile will use all of your CFLAGS:

这样你的 makefile 将使用你所有的CFLAGS

CFLAGS=-O3 -std=c++0x -pg -D_DEBUG -g -c -Wall

You're overriding them with each "CFLAGS=..." declaration.

您正在使用每个“CFLAGS=...”声明覆盖它们。

Moreover, it should be CXXFLAGS, not CFLAGS. CFLAGSis for C applications.

而且,应该是CXXFLAGS,不是CFLAGSCFLAGS用于 C 应用程序。

As @Florian Sowade said, you could use CFLAGS += -O3 -std....(or CXXFLAGS..), so that users can provide their own flags when executing make.

正如@Florian Sowade 所说,您可以使用CFLAGS += -O3 -std....(或 CXXFLAGS ..),以便用户在执行 make 时可以提供自己的标志。

回答by Ari

You can keep it in multiple lines, but you probably want to append, not to assign:

您可以将其保存在多行中,但您可能想要append,而不是分配

# e.g.
CFLAGS  += -O3
CFLAGS  += -std=c++0x
CFLAGS  += -pg -D_DEBUG -g -c -Wall

回答by Jonathan Wakely

Where did you get that mess from? That makefile was wrong long before you modified it for C++11.

你从哪里弄来的这些烂摊子?在你为 C++11 修改它之前,那个 makefile 是错误的。

Let's start with the first line:

让我们从第一行开始:

MACHINE = $(shell echo `uname -s`-`uname -m` | sed "s/ //g")

Try running the command echo `uname -s`-`uname -m`and you'll see if has no spaces in anyway, so what's the point in the sedcommand?

尝试运行该命令echo `uname -s`-`uname -m`,您将看到无论如何都没有空格,那么该sed命令有什么意义呢?

Maybe you want this:

也许你想要这个:

MACHINE = $(shell uname -s -m | sed "s/ /-/g")

That only runs two processes (uname and sed) not four (two unames, echo and sed)

只运行两个进程(uname 和 sed)而不是四个(两个 unames、echo 和 sed)

If you're using GNU make it should be

如果你使用 GNU make 应该是

MACHINE := $(shell uname -s -m | sed "s/ /-/g")

So it's only run once.

所以它只运行一次。

CCC     = CC

What is CC?

什么是CC

CCC     = g++

Oh, it doesn't matter, you've replaced the value anyway. The conventional make variable for the C++ compiler is CXX

哦,没关系,反正你已经替换了值。C++ 编译器的常规 make 变量是CXX

CFLAGS  = -O3
CFLAGS  = -std=c++0x
CFLAGS  = -pg -D_DEBUG -g -c -Wall

As others have pointed out, you set CFLAGS, then set it to something different, then to something different. Only the last value will be kept.

正如其他人指出的那样,您设置了 CFLAGS,然后将其设置为不同的内容,然后再设置为不同的内容。只会保留最后一个值。

LFLAGS  = -O
LFLAGS  = -pg -g

Same here, and what is LFLAGS? Do you mean LDFLAGS?

这里也一样,什么是LFLAGS?你的意思是LDFLAGS

A good way to debug makefiles is to create a target to print out the variables you are setting, to check they have the values you expect:

调试 makefile 的一个好方法是创建一个目标来打印您正在设置的变量,以检查它们是否具有您期望的值:

print-vars:
        echo CFLAGS is $(CFLAGS)
        echo LDFLAGS is $(LDFLAGS)