场景:
我正在两台不同的机器上构建软件。
其中一台机器具有完全兼容 C++11 版本的 g++
。
另一个则不然。
机器 1 (Linux):
$ g++ --version
g++ (Ubuntu 5.1.0-0ubuntu11~14.04.1) 5.1.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
机器 2(带有 Cygwin 的 Windows):
$ g++ --version
g++ (GCC) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
我在这两台机器上构建的 C++ 软件一直支持 C++98。
但是,如果 C++ 的新功能可用,我们将使用它们。
(有问题的软件是C++ Catch,用于单元测试)。
问题:
我有一个通用的 makefile 来构建这个软件。在 Cygwin
上它成功构建了软件。在 Linux 上,使用较新的编译器,它无法构建,因为我假设它检测编译器的版本并尝试使用更现代的 C++ 功能。
这里是错误转储,供浏览之用。大部分只是与 nullptr_t 相关。也许与模板推导的新规则有关(不完全确定):
catch.hpp:833:17: error: ‘nullptr_t’ in namespace ‘std’ does not name a type
inline std::nullptr_t opCast(std::nullptr_t) { return nullptr; }
^
catch.hpp:954:58: error: ‘template<Catch::Internal::Operator Op, class T> bool Catch::Internal::compare’ conflicts with a previous declaration
template<Operator Op, typename T> bool compare( std::nullptr_t, T* rhs ) {
^
catch.hpp:948:44: note: previous declaration ‘namespace Catch::Internal { }::compare’
template<Operator Op, typename T> bool compare( T* lhs, int rhs ) {
^
catch.hpp:954:53: error: ‘nullptr_t’ is not a member of ‘std’
template<Operator Op, typename T> bool compare( std::nullptr_t, T* rhs ) {
^
catch.hpp:954:70: error: expected primary-expression before ‘*’ token
template<Operator Op, typename T> bool compare( std::nullptr_t, T* rhs ) {
^
catch.hpp:954:72: error: ‘rhs’ was not declared in this scope
template<Operator Op, typename T> bool compare( std::nullptr_t, T* rhs ) {
^
catch.hpp:954:76: error: expression list treated as compound expression in initializer [-fpermissive]
template<Operator Op, typename T> bool compare( std::nullptr_t, T* rhs ) {
^
catch.hpp:954:44: warning: variable templates only available with -std=c++14 or -std=gnu++14
template<Operator Op, typename T> bool compare( std::nullptr_t, T* rhs ) {
^
catch.hpp:954:78: error: expected ‘;’ before ‘{’ token
template<Operator Op, typename T> bool compare( std::nullptr_t, T* rhs ) {
^
catch.hpp:957:66: error: ‘std::nullptr_t’ has not been declared
template<Operator Op, typename T> bool compare( T* lhs, std::nullptr_t ) {
^
catch.hpp:957:44: error: redefinition of ‘template<Catch::Internal::Operator Op, class T> bool Catch::Internal::compare(T*, int)’
template<Operator Op, typename T> bool compare( T* lhs, std::nullptr_t ) {
^
catch.hpp:948:44: note: ‘template<Catch::Internal::Operator Op, class T> bool Catch::Internal::compare(T*, int)’ previously declared here
template<Operator Op, typename T> bool compare( T* lhs, int rhs ) {
^
In file included from main.cpp:2:0:
catch.hpp:1088:38: error: ‘std::string Catch::toString’ redeclared as different kind of symbol
std::string toString( std::nullptr_t );
^
catch.hpp:1085:13: note: previous declaration ‘std::string Catch::toString(unsigned char)’
std::string toString( unsigned char value );
^
catch.hpp:1088:23: error: ‘nullptr_t’ is not a member of ‘std’
std::string toString( std::nullptr_t );
^
In file included from main.cpp:2:0:
catch.hpp:7336:38: error: ‘std::string Catch::toString’ redeclared as different kind of symbol
std::string toString( std::nullptr_t ) {
^
In file included from main.cpp:2:0:
catch.hpp:1232:13: note: previous declaration ‘template<class T, class Allocator> std::string Catch::toString(const std::vector<_Tp, _Alloc>&)’
std::string toString( std::vector<T,Allocator> const& v ) {
^
In file included from main.cpp:2:0:
catch.hpp:7336:23: error: ‘nullptr_t’ is not a member of ‘std’
std::string toString( std::nullptr_t ) {
通过确保使用 --std=c++11
标志编译 g++
,可以在 Linux
机器上轻松修复此问题。但是,我不希望两台机器上有两个单独的 makefile。我无法将 --std=c++11
标志添加到两个版本,因为版本 4.9
还没有该标志。
问题:
如何在不同版本的 g++
上使用相同的命令启用交叉编译?根据 g++
的版本,代码的构建方式有所不同 - 在这种情况下,有时需要传递 std
标志。
附加:
我尝试为两个构建都赋予 g++
标志 --std=c++98
,但在 Linux
构建上仍然失败。这里的目标是在两台机器上使用相同类型的命令。
最佳答案
在 Windows 平台上,我建议使用 Stephan Lavavej 的 Nuwen Distro 。唯一的障碍是您无法使用 std::thread
工具,但 boost::thread
提供了几乎无缝的替代方案。
关于c++ - 如何防止根据我的 g++ 编译器版本进行不同的编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31010973/