如何在makefile中设置编译标志?

时间:2015-12-30 19:24:51

标签: c++ c++11 makefile

我习惯在IDE中编程,但最近切换到vim和插件。现在我尝试为c ++项目编写一个makefile,但不知怎的,如果我运行make我总是得到错误

g++    -c -o *.o createOutput.cpp
In file included from /usr/include/c++/4.8/thread:35:0,
                 from createOutput.cpp:5:
/usr/include/c++/4.8/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.
 #error This file requires compiler and library support for the \
  ^

这是我的makefile:

CC = clang++

# compiler flags
CFLAGS = -O3 -Wall -Werror -std=c++11
CFLAGS_SFML = -lsfml-graphics -lsfml-window -lsfml-system

all: program.exe clean

program.exe: *.o
    $(CC) -o program.exe *.o $(CFLAGS) $(CFLAGS_SFML) 

getInput.o: getInput.cpp
    $(CC) -c getInput.cpp $(CFLAGS) 

createOutput.o: createOutput.cpp
    $(CC) -c createOutput.cpp $(CFLAGS) 

main.o: main.cpp
    $(CC) -c main.cpp $(CFLAGS)

.PHONY: clean
clean:
    rm *.o
    @echo clean done

我的错误在哪里?为什么使用g++代替clang?为什么不使用-std=c++11参数?对不起初学者的问题,遗憾的是我无法找到谷歌的解决方案。

2 个答案:

答案 0 :(得分:3)

您要设置CXXFLAGSmake会自动选择(并发送给您的编译器(例如 g++clang++)。

答案 1 :(得分:1)

make试图制作目标' *。o'。

因此,您可以明确指定源列表:

CC = clang++

#compiler flags
CFLAGS = -O3 -Wall -Werror -std=c++11
CFLAGS_SFML = -lsfml-graphics -lsfml-window -lsfml-system
SRCS = getInput.cpp createOutput.cpp main.cpp
OBJS = $(SRCS:.cpp=.o)

all: program.exe

program.exe: $(OBJS)
        $(CC) -o program.exe *.o $(CFLAGS) $(CFLAGS_SFML)

getInput.o: getInput.cpp
        $(CC) -c getInput.cpp $(CFLAGS)

createOutput.o: createOutput.cpp
        $(CC) -c createOutput.cpp $(CFLAGS)

main.o: main.cpp
        $(CC) -c main.cpp $(CFLAGS)

.PHONY : clean
clean:
        rm *.o
        @echo clean done

注意变量OBJSSRCS的定义。