c++ - g++ -std=c++11,标志没有生效

标签 c++ c++11 makefile g++ ubuntu-14.04

我正在尝试将 c++ 方言的版本更改为 c++11,但该标志似乎没有生效。

这是我的 makefile 的片段:

#Path Definitions
PATHT = test/
PATHI = inc/
PATHB = build/
PATHS = src/

#Files of interest
CODER_C   = $(PATHS)E8b9b.cpp
CODER_O   = $(PATHB)E8b9b.o
CODER_H   = $(PATHI)E8b9b.hpp
CODER_C_T = $(PATHT)Test_E8b9b_encoder.cpp
CODER_O_T = $(PATHB)Test_E8b9b_encoder.o
TRAN_C    = $(PATHS)TransactionID.cpp
TRAN_O    = $(PATHB)TransactionID.o
TRAN_H    = $(PATHI)TransactionID.hpp
TRAN_C_T  = $(PATHT)Test_TransactionID.cpp
TRAN_O_T  = $(PATHB)Test_TransactionID.o
SIGV_C    = $(PATHS)SigView.cpp
SIGV_O    = $(PATHB)SigView.o
SIGV_H    = $(PATHI)SigView.hpp
SIGV_C_T  = $(PATHT)Test_SigView.cpp
SIGV_O_T  = $(PATHB)Test_SigView.o
TESOUT    = $(PATHB)Test.out
RUNOUT    = $(PATHB)Run.out

#Libraries of interest
BOOSTL = -lboost_unit_test_framework 
INCH   = -Iinc 

#Tool Definitions 
CC=g++
FLAGS = -std=c++11 -Wall

test: $(CODER_O_T) $(CODER_O) $(TRAN_O_T) $(TRAN_O) $(SIGV_O_T) $(SIGV_O)
    g++ $(FLAGS) $(CODER_O_T) $(CODER_O) $(TRAN_O_T) $(TRAN_O) $(SIGV_O_T) $(SIGV_O) $(BOOSTL)  \
    -o $(TESOUT) 2> buildLog.txt
    ./$(TESOUT) 1> buildLog.txt

$(CODER_O_T): $(CODER_C_T) $(CODER_H)  
    g++ $(FLAGS) $(INCH) -c $< -o $@ 2> buildLog.txt

$(CODER_O): $(CODER_C) $(CODER_H)
    g++ $(FLAGS) $(INCH) -c $< -o $@ 2> buildLog.txt 

$(TRAN_O_T): $(TRAN_C_T) $(TRAN_H)  
    g++ $(FLAGS) $(INCH) -c $< -o $@ 2> buildLog.txt

$(TRAN_O): $(TRAN_C) $(TRAN_H)
    g++ $(FLAGS) $(INCH) -c $< -o $@ 2> buildLog.txt

$(SIGV_O_T): $(SIGV_C_T) $(SIGV_H)  
    g++ $(FLAGS) $(INCH) -c $< -o $@ 2> buildLog.txt

$(SIGV_O): $(SIGV_C) $(SIGV_H)
    g++ $(FLAGS) $(INCH) -c $< -o $@ 2> buildLog.txt 

我正在使用以下单元测试检查方言:

#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "SigView.hpp"
#include <fstream>
#include <string>
#include <stdio.h> // For the __cplusplus define 

/*
These are the C++ standards and what value you should be able to expect in __cplusplus:
C++ pre-C++98: __cplusplus is 1.
C++98: __cplusplus is 199711L.
C++98 + TR1: This reads as C++98 and there is no way to check that I know of.
C++11: __cplusplus is 201103L.
C++14: __cplusplus is 201402L.
*/

BOOST_AUTO_TEST_CASE(RandomSystemTest)
{
    BOOST_TEST_REQUIRE(__cplusplus == 201103L, "c++ dialect is " << __cplusplus);
}

测试用例失败,输出如下:

fatal error: in "RandomSystemTest": c++ v is 199711

版本仍然是 199711。

我已经尝试了多个标志,但都没有什么不同:

c11
c++11
c++0x
gnu++11

g++ 版本 = 4.8.4 Ubuntu 版本 14.04.4 LTS

编辑:

根据请求的 make 输出:

$ make test
g++ --std=c++11 -Wall -Iinc  -c test/Test_SigView.cpp -o build/Test_SigView.o 2> buildLog.txt
g++ --std=c++11 -Wall build/Test_E8b9b_encoder.o build/E8b9b.o build/Test_TransactionID.o build/TransactionID.o build/Test_SigView.o build/SigView.o -lboost_unit_test_framework   \
    -o build/Test.out 2> buildLog.txt
./build/Test.out 
Running 11 test cases...

*** No errors detected

现在可以了!我不知道为什么。我所做的唯一区别是执行 g++ -std=c++11 -dM -E -xc++/dev/null | grep __cplusplus 按照@Dietrich Epp 的建议

最佳答案

从您的 Makefile 来看,您的链接顺序似乎是错误的。将 -std=c++11 放在源代码文件之后。我学到的一个最佳实践是始终将源 .cpp 文件放在第一位,紧跟在 g++ 之后。

关于c++ - g++ -std=c++11,标志没有生效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36142950/

相关文章:

c++ - DirectX CreateOverlay函数库?

c++ - constexpr 是否暗示内联?

c++ - 使用 c++11 range-based-for-loop 搜索插入位置

C++ ofstream 无法从变量名打开文件(即使在使用 c_str() 之后)

c - 对 sqrt 的 undefined reference

shell - GNU shell 函数在意外标记 `(' 附近出现语法错误

makefile - Makefile 中的递归...还有其他解决方案吗?

c++ - 将 MBCS 和 Unicode 库链接在一起

c++ - 在带有模板参数的模板中使用重载函数

c++ - 在 windows 中包含 .h 路径,但在 visual studio 中忽略 unix