相关疑难解决方法(0)

如何编写可在C++程序中使用的C头文件?

可能重复:
如何检查(通过预处理器)C源文件是否正在编译为C++代码

我正在尝试找到一个标准的宏来测试头文件是编译为C还是C++.这样做的目的是标题可以包含在C或C++代码中,并且必须根据具体情况略有不同.特别:

在C中,我需要这个代码:

extern size_t insert (const char*);
Run Code Online (Sandbox Code Playgroud)

在C++中,我需要这个代码:

extern "C" size_t insert (const char*);
Run Code Online (Sandbox Code Playgroud)

另外,有没有办法避免在标题中的每个声明周围放置#ifdef?

c c++ macros

13
推荐指数
3
解决办法
870
查看次数

使用C预处理器确定编译环境

我正在构建一个应用程序,它包含一个用C语言编写的Windows驱动程序和一个用C++ 编写的用户模式.它们都使用共享头文件来定义几个宏,常量,枚举等.在C++版本中,我想在命名空间中包含所有内容,这是C编译器不支持的功能.是否有某些变量我可以检查用作Visual Studio的预处理程序指令,如下例所示?

#ifdef USING_CPLUSPLUS
namespace mynamespace{
#endif

    struct mystruct{
       ...
    };


#ifdef USING_CPLUSPLUS
}
#endif
Run Code Online (Sandbox Code Playgroud)

c c++ visual-studio c-preprocessor

2
推荐指数
2
解决办法
583
查看次数

#ifdef标志告诉gcc和g ++编译器之间的区别?

gcc将C程序编译为C语言,将C++程序编译为C++,因此需要在C++中使用"extern"C"语句.然而,g ++将C程序编译为C++,将C++程序编译为C++,因此要求'extern"C"'声明不得在C++中使用.

无需任何输入即可检查标准标志

 g++ -E -dM - </dev/null 
 gcc -E -dM - </dev/null 
Run Code Online (Sandbox Code Playgroud)

给出相同的结果.

__GNUG__       is equivalent to testing (__GNUC__ && __cplusplus)
Run Code Online (Sandbox Code Playgroud)

这也无法在C++源代码清单中区分gcc和g ++ .

什么是魔术

#ifndef __MAGIC_G++_INCLUDE_FLAG_BUT_NOT_GCC__ 
Run Code Online (Sandbox Code Playgroud)

包裹extern"C"{声明?谢谢.

编辑:我意识到这是一个相当深奥的案例.测试需要在编译器风格上进行,而不是在源代码风格上进行.在这两种情况下,测试都在C++源代码清单中执行(这就是为什么我提到GNUG是不合适的).澄清一下,测试main.cpp:

#include <iostream>
int 
main( int argc, char **argv )
{
#ifdef __cplusplus  <<<< FIX THIS ONE
  std::cout << "I was compiled using g++" << std::endl;
#else
  std::cout << "I was compiled using gcc" << std::endl;
#endif 
}
Run Code Online (Sandbox Code Playgroud)

用任何一个编译时

 g++ …
Run Code Online (Sandbox Code Playgroud)

c c++ gcc compilation g++

2
推荐指数
1
解决办法
1949
查看次数

标签 统计

c ×3

c++ ×3

c-preprocessor ×1

compilation ×1

g++ ×1

gcc ×1

macros ×1

visual-studio ×1