根据我的要求。假设我有以下文件
abc.h //used for C file
int new; // All C++ keywords, new use as a variable
char class; // All C++ keywords, class use as a variable
Actual_task();
abc.c //C file
main()
{
...//Body of file where they have used new and class variable
new++; //for an example
class = 'C';
actual_task();//One function is getting called here
}我有一个.cpp文件,它需要文件abc.h必须是included才能使用actual_task()
CPPfile.cpp
extern "C"{
#include "abc.h"
}然后它抛出像class一样的errors,而new不能像变量一样使用。
那么如何在cpp文件中使用C头文件呢?
发布于 2012-12-13 19:57:38
您不能将使用C++关键字的C头文件用于C++之外的其他目的。
正确的解决方案是更改头文件,使其不再使用C++关键字。
如果将C++关键字用于全局变量( C++代码未使用的变量)或函数参数名称,则可以使用如下结构:
#define new new_
#define class class_
extern "C" {
#include "abc.h"
}
#undef class
#undef new发布于 2012-12-13 20:18:52
更改头文件,使变量/函数的名称不再是C++中的保留字。
如果有人说你不能,那就告诉他们你被要求合并来自两种不同编程语言的源代码,这两种编程语言在这种意义上是不兼容的。这是一个已知的问题,有一个已知的解决方案。
https://stackoverflow.com/questions/13859163
复制相似问题