这个代码在gcc版本4.3.2(Debian 4.3.2-1.1)上不能为我编译
main(){
int unix;
}
Run Code Online (Sandbox Code Playgroud)
我检查了C关键字列表,"unix"不是其中之一.为什么我收到以下错误?
unix.c:2: error: expected identifier or ‘(’ before numeric constant
Run Code Online (Sandbox Code Playgroud)
任何人?
好的,我会咬人的.大众流行的答案为什么C预处理器将单词"linux"解释为常量"1"?问题提到
main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);}`
Run Code Online (Sandbox Code Playgroud)
打印
"unix",但原因与宏名称的拼写完全无关.
我阅读http://www.ioccc.org/1987/korn.hint,但我认为更多的细节将有助于不混淆这:)
https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html:
\n\n\n一些用户尝试使用它
\n-Wpedantic来检查程序是否严格符合 ISO C。他们很快发现它并没有达到他们想要的效果:它找到了一些非 ISO 实践,但不是所有\xe2\x80\x94,只有那些 ISO C需要诊断的实践,以及其他一些已添加诊断的实践。
有哪些未发现的非 ISO 实践示例-pedantic?
我前几天在stackoverflow上找到了这个片段(谢谢你):
#define PLATFORM 3
#define PASTER(x,y) x ## _ ## y
#define EVALUATOR(x,y) PASTER(x,y)
#define PLATFORMSPECIFIC(fun) EVALUATOR(fun, PLATFORM)
extern void PLATFORMSPECIFIC(somefunc)(char *x);
Run Code Online (Sandbox Code Playgroud)
用gcc -E编译,结果如下:
# 1 "xx.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "xx.c"
extern void somefunc_3(char *x);
Run Code Online (Sandbox Code Playgroud)
然而:
#define PLATFORM linux
#define PASTER(x,y) x ## _ ## y
#define EVALUATOR(x,y) PASTER(x,y)
#define PLATFORMSPECIFIC(fun) EVALUATOR(fun, PLATFORM)
extern void PLATFORMSPECIFIC(somefunc)(char *x);
Run Code Online (Sandbox Code Playgroud)
结果是:
# 1 "xx.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "xx.c"
extern void somefunc_1(char *x);
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能让它返回'somefunc_linux'?克朗似乎做对了,顺便说一句.
我正在使用QT Creator在Ubuntu上制作C++程序.我编写的程序编译得很好,直到我决定开始使用C++ 11而不是C++ 98(这是QT Creator中的默认设置).我使用自己的cmake文件,而不是qmake,所以为了做到这一点,我在我的网站中包含以下内容CMakeLists.txt file:
set(CMAKE_CXX_FLAGS "-std=c++0x")
Run Code Online (Sandbox Code Playgroud)
现在,我的部分代码有以下内容(不是我编写的):
#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
Run Code Online (Sandbox Code Playgroud)
转移到C++ 11后,我在该行收到错误error Unsupported Platform!.这是因为,从我所看到的,变量linux没有在任何地方定义,尽管__x86_64__定义了变量.
因此,我有两个问题:
1)为什么变量linux没有定义,即使我使用的是Linux?
2)如何告诉C++ 11忽略此错误?
谢谢.