简体   繁体   English

Makefile修改以支持c ++ 11

[英]Makefile modification to support c++11

I am able to compile a single file using gcc with -std=c++0x option. 我能够使用带有-std = c ++ 0x选项的gcc编译单个文件。 But I can't do this through makefile. 但我不能通过makefile来做到这一点。 Here are the set of flags in my makefile (which after make complains about c++11 keywords): 以下是我的makefile中的标志集(在抱怨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

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 . 此外,它应该是CXXFLAGS ,而不是CFLAGS CFLAGS is for C applications. CFLAGS适用于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时可以提供自己的标志。

You can keep it in multiple lines, but you probably want to append , not to assign : 你可以把它保存在多行,但你可能想要追加 ,而不是分配

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

Where did you get that mess from? 你从哪里得到那个烂摊子? That makefile was wrong long before you modified it for C++11. 在你为C ++ 11修改它之前很久就认错了。

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 sed command? 尝试运行命令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. 正如其他人所指出的那样,你设置CFLAGS,然后将其设置为不同的东西,然后设置为不同的东西。 Only the last value will be kept. 只保留最后一个值。

LFLAGS  = -O
LFLAGS  = -pg -g

Same here, and what is LFLAGS ? 同样在这里,什么是LFLAGS Do you mean LDFLAGS ? 你的意思是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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM