问题描述
我在 Mac OSX Mountain Lion 上使用来自 http://hpc.sourceforge.net 的 gcc 4.8.1.我正在尝试编译一个使用 <string>
中的 to_string
函数的 C++ 程序.我每次都需要使用标志 -std=c++11
:
I use gcc 4.8.1 from http://hpc.sourceforge.net on Mac OSX Mountain Lion. I am trying to compile a C++ program which uses the to_string
function in <string>
. I need to use the flag -std=c++11
every time:
g++ -std=c++11 -o testcode1 code1.cpp
有没有办法默认包含这个标志?
Is there a way to include this flag by default?
推荐答案
H2CO3 是对的,您可以使用带有 -std=c++11 设置的 CXXFLAGS 的 makefilemakefile 是一个简单的文本文件,其中包含有关如何编译程序的说明.创建一个名为 Makefile 的新文件(大写 M).要自动编译您的代码,只需在终端中键入 make 命令.您可能需要安装make.
H2CO3 is right, you can use a makefile with the CXXFLAGS set with -std=c++11A makefile is a simple text file with instructions about how to compile your program. Create a new file named Makefile (with a capital M). To automatically compile your code just type the make command in a terminal. You may have to install make.
这是一个简单的:
CXX=clang++
CXXFLAGS=-g -std=c++11 -Wall -pedantic
BIN=prog
SRC=$(wildcard *.cpp)
OBJ=$(SRC:%.cpp=%.o)
all: $(OBJ)
$(CXX) -o $(BIN) $^
%.o: %.c
$(CXX) $@ -c $<
clean:
rm -f *.o
rm $(BIN)
它假定所有 .cpp 文件与 makefile 位于同一目录中.但是您可以轻松地调整您的 makefile 以支持 src、包含和构建目录.
It assumes that all the .cpp files are in the same directory as the makefile. But you can easily tweak your makefile to support a src, include and build directories.
编辑:我修改了默认的 c++ 编译器,我的 g++ 版本不是最新的.使用 clang++ 这个 makefile 可以正常工作.
Edit : I modified the default c++ compiler, my version of g++ isn't up-to-date. With clang++ this makefile works fine.
这篇关于如何在 gcc 中启用 C++11?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!