繁体   English   中英

如何防止基于我的 g++ 编译器版本的不同编译?

[英]How do I prevent different compilation based on the version of my g++ compiler?

设想:
我正在两台不同的机器上构建软件。
其中一台机器具有完全兼容的 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提供了一个几乎无缝的替代方案。

在 Windows 上,我建议切换到 MSYS2,特别是如果您需要 C++11 支持。 最新版本有 mingw64 g++4.9.2。 我能够大量使用 c++11/boost/python 成功构建我的 Ubuntu 项目,而我无法在 Cygwin 或 MSVC2015 RC 上做到这一点。 MSYS2 有一个名为pacman的包管理器,它类似于apt-get/yum

有相当多的预建内容,您可以在此处查看它们的可用性。

非常简洁的介绍在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM