如何在gcc中启用C ++ 11? [英] How do I enable C++11 in gcc?

查看:213
本文介绍了如何在gcc中启用C ++ 11?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac OSX Mountain Lion上使用来自 http://hpc.sourceforge.net 的gcc 4.8.1。我试图编译一个C ++程序,它在< string> 中使用 to_string 函数。我每次都需要使用 -std = c ++ 11 标志:

  g ++ -std = c ++ 11 -o testcode1 code1.cpp 

有没有办法在默认情况下包含这个标志?

解决方案

H2CO3是正确的,你可以使用makefile来设置CXXFLAGS和-std = c ++ 11
makefile是一个简单的文本文件,包含有关如何编译程序的说明。创建一个名为Makefile的新文件(使用大写M)。要自动编译代码,只需在终端中输入 make 命令即可。您可能需要安装make。



以下是一个简单的例子:

  CXX = clang ++ 
CXXFLAGS = -g -std = c ++ 11 -Wall -pedantic
BIN = prog

SRC = $(通配符* .cpp)
OBJ = $(SRC: %.cpp =%。o)

全部:$(OBJ)
$(CXX)-o $(BIN)$ ^

%.o: %.c
$(CXX)$ @ -c $<

clean:
rm -f * .o
rm $(BIN)

它假定所有.cpp文件与makefile位于同一目录中。但是你可以很容易地调整你的makefile以支持src,include和build目录。
$ b

编辑:我修改了默认的c ++编译器,我的版本的g ++不是最新的。随着铿锵++这个makefile工作正常。


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 is right, you can use a makefile with the CXXFLAGS set with -std=c++11 A 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.

Here's a simple one :

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)

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.

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆