如何编写确定其编译器的程序
How to write a program that determines its compiler
需要编写一个C程序。如果在 C 语言编译器中运行,则程序应打印"C"。如果在编译器C++中运行,则应打印"C++"。
不能使用预处理器指令。
在头脑中,仅用于将任何字符的大小与char
大小进行比较,例如:
sizeof(char)==sizeof('a')
以下是它的工作原理:
// C code:
#include <stdio.h>
int main()
{
printf("%s", (sizeof(char)==sizeof('a') ? "C++" : "C"));
return 0;
}
输出:C
// C++ code:
#include <stdio.h>
int main()
{
printf("%s", (sizeof(char)==sizeof('a') ? "C++" : "C"));
return 0;
}
输出:C++
那里,有更好的方法吗?
你可以检查__cplusplus
宏,看看你是否被编译为 c++。
#include <stdio.h>
int main()
{
printf("%sn",
#if __cplusplus
"C++"
#else
"C"
#endif
);
}
标准 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf包含有关 C 和 C++ 之间区别的附录
所以它包含您使用的字符与整数的差异,但也包括例如
更改:在C++中,类声明将类名引入作用域 声明并隐藏任何对象、函数或其他声明的位置 封闭作用域中的该名称。 在 C 语言中,结构标记名称的内部作用域声明从不隐藏 外部作用域中对象或函数的名称
示例:(来自标准)
int x [99];
void f () {
struct x { int a ; };
sizeof (x ); /∗ size of the array in C ∗/
/∗ size of the struct in C++ ∗/
}
gcc 在我的机器上给了哪个 396 和 g++ 4
相关文章:
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- 如何编译使用从不同编译器编译的库的应用程序?
- 如何使用CLion和MSVC编译器工具链编译wxWidgets Hello World应用程序而没有错误?
- 为什么我的程序无法在GDB在线编译器/调试器或Visual Studio C++ 2019中运行
- 是否可以将不同的编译器嵌入到我的应用程序中?
- C++使用较新的编译器构建应用程序,而无需重新构建库
- C++程序编译器在使用g++命令编译后自动运行exe文件
- 我正在调试这个C++程序.编译器不再显示语法错误,但存在隐藏的逻辑错误
- 编译 Fortran 和C++程序并从英特尔编译器链接
- CMake 和 Visual Studio 2015 - C 编译器 cl.exe 无法编译简单的测试程序
- C++编译器 g++.exe 无法编译简单的测试程序 - 确定 CXX 编译器是否工作失败
- JIT 编译器及其在 C++ 前面加快 .NET 中程序执行速度的好处
- 在线编译器 - 等待输入并自动馈送输入时拦截程序
- G :内部编译器错误:分割故障(程序CC1PLUS) - 我在哪里开始
- GCC编译器,为较低版本的GCC编译应用程序
- 在C++程序中使用 libcurl 会导致 Visual C++ 编译器发出错误
- 如何将我的控制台应用程序链接到使用 clang++ 作为编译器的 OSX 环境中的 /usr/local/lib 中存在
- 编译器将私人构造函数作为程序代码中的候选人提供
- Arm GNU编译器:通过多余的强制转换优化的三进制生成的程序集
- 用于独立于系统的程序分发的即时C++编译器