c++ - 在 makefile 中添加 c++11 以消除错误 to_string is not declared in this scope

标签 c++ c++11 makefile

我想这个问题在其他一些线程中被问到,我在调用 make 时遇到错误:to_string 未在此范围内声明。我发现我必须在 makefile 中添加 c++11。但是我尝试了几个线程中提到的一些选项。你能在这里提供一些解决方案吗?谢谢

最佳答案

-std=c++11 添加到 CFLAGS 将导致 g++ 使用 C++11 标准进行编译。像这样

CFLAGS=-std=c++11 -c -g -O3 -finline-functions -fstack-protector

但是,正如注释中强调的那样,使用 makefile 编译 C++ 程序的适当语法是使用这样的规则

$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c

您的 C++ 文件使用后缀 .cc [ 1 ].然后,您可以将 -std=c++11 添加到 CXXFLAGSCPPFLAGSCXXFLAGS 之间的区别是 [ 2 ]

CPPFLAGS is supposed to be for flags for the C PreProcessor; CXXFLAGS is for flags for the C++ compiler.

这需要在您的 makefile 中进行一些重写,即

CXX=g++
LD=g++
CXXFLAGS=-c -g -O3 -fstack-protector -I./Eigen

规则来自

$(CC) $(INCLUDE) $(CFLAGS) -c

$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c

如上。

$(INCLUDE) 也可以从您的链接命令 ($(LD)) 中删除,因为它仅在编译期间需要。您的链接命令也可以简化为

ParEGOIteration13: ParEGOIteration13.o Utilities.o WeightVector.o SearchSpace.o DACE.o GeneticAlgorithm.o Matrix.o
    $(CXX) $? -o $@

通过使用自动变量[ 3 ]

  • $? 扩展到所有先决条件
  • $@ 扩展为目标名称

我将让您了解如何在编译规则中使用自动变量。

注意:我删除了 -finline-functions 作为 -O3(和 -O2)转默认情况下打开 gcc .

关于c++ - 在 makefile 中添加 c++11 以消除错误 to_string is not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31959563/

相关文章:

c++ - 无法在 Visual Studio 程序中运行合并排序

c++ - 如何在C++中打印上标3

c++ - 智能指针是否排除了两阶段 build 的需要?

c++ - 如何配置 Vim 插件 YouCompleteMe 来读取 Makefile?

c - "make clean"导致 "make all"失败

c++ - 在 Windows 中的不同位置初始化变量会出错

c++ - 模板类中 protected 模板成员函数

c++ - 有 C++11 临界区吗?

c++ - ADL 未选取带有模板参数列表的后缀表达式

makefile - cmake cygwin make.exe错误