简体   繁体   English

从C ++ 98升级到C ++ 11会导致错误

[英]Upgrading from C++98 to C++11 causes error

I am using QT Creator to make a C++ program on Ubuntu. 我正在使用QT Creator在Ubuntu上制作C ++程序。 The program I had written was compiling fine, until I decided to start using C++11 rather than C++98 (which is the default in QT Creator). 我编写的程序可以很好地编译,直到我决定开始使用C ++ 11而不是C ++ 98(这是QT Creator中的默认设置)。 I am using my own cmake file, rather than qmake, and so to do this, I included the following line in my CMakeLists.txt file : 我使用的是我自己的cmake文件,而不是qmake,因此,我在CMakeLists.txt file加入了以下内容:

set(CMAKE_CXX_FLAGS "-std=c++0x")

Now, part of my code has the following (which was not written by me): 现在,我的部分代码具有以下内容(不是我编写的):

#if (linux && (i386 || __x86_64__))
#   include "Linux-x86/OniPlatformLinux-x86.h"
#elif (linux && __arm__)
#   include "Linux-Arm/OniPlatformLinux-Arm.h"
#else
#   error Unsupported Platform!
#endif

After transferring to C++11, I get an error at the line error Unsupported Platform! 转移到C ++ 11后,出现以下错误:线路error Unsupported Platform! . This is because, from what I can see, the variable linux is not defined anywhere, although the variable __x86_64__ is defined. 这是因为,据我__x86_64__ ,尽管定义了变量__x86_64__ ,但变量linux并未在任何地方定义。

Therefore, I have two questions: 因此,我有两个问题:

1) Why is the variable linux not defined, even though I am using Linux? 1)即使我使用Linux,为什么未定义变量linux

2) How can I tell C++11 to ignore this error? 2)如何告诉C ++ 11忽略此错误?

Thanks. 谢谢。

The identifier linux is not reserved. 标识符linux不保留。 A conforming compiler may not predefine it as a macro. 合格的编译器可能不会将其预定义为宏。 For example, this program: 例如,该程序:

int main() {
    int linux = 0;
    return linux;
}

is perfectly valid, and a conforming compiler must accept it. 是完全有效的,并且合格的编译器必须接受它。 Predefining linux causes the declaration to be a syntax error. 预定义linux会导致声明是语法错误。

Some older compilers (including the compiler you were using, with the options you were giving it) predefine certain symbols to provide information about the target platform -- including linux to indicate a Linux system. 一些老的编译器(包括你正在使用的编译器,用你给它的选项)预先定义某些符号,以提供有关目标平台信息-包括linux指示Linux系统。 This convention goes back to early C compilers, written before there was a distinction between reserved and unreserved identifiers. 该约定可以追溯到早期的C编译器,该C编译器是在保留标识符和未保留标识符之间进行区分之前编写的。

The identifier __linux__ , since it starts with two underscores, is reserved for use by the implementation, so compilers are allowed to predefine it -- and compilers for Linux systems typically do predefine it as a macro expanding to 1 . 标识符__linux__由于以两个下划线开头,因此保留给实现使用,因此允许编译器对其进行预定义-并且Linux系统的编译器通常会将其预定义为扩展为1的宏。

Confirm that your compiler predefines __linux__ , and then change your code so it tests __linux__ rather than linux . 确认您的编译器预定义了__linux__ ,然后更改代码,以便它测试__linux__而不是linux You should also find out what reserved symbol is used instead of i386 (likely __i386__ ). 您还应该找出使用了什么保留符号而不是i386 (可能是__i386__ )。

Related: Why does the C preprocessor interpret the word "linux" as the constant "1"? 相关: 为什么C预处理程序将单词“ linux”解释为常量“ 1”?

Change your standard-selection flag to -std=gnu++0x instead of c++0x . 将您的标准选择标志更改为-std=gnu++0x而不是c++0x The gnu flavors provide some non-standard extensions, apparently including predefining the macro linux . gnu风格提供了一些非标准的扩展,显然包括预定义了linux宏。 Alternatively, check for __linux__ instead. 另外,也可以检查__linux__

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

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