如何处理C++;带有对C函数和类对象的外部调用的头文件 我试图编译一个包含C和C++文件的应用程序。有一个特别的标题,我面临的问题。这个文件(C++头文件)看起来会像这样: #ifndef TASK_H #define TASK_H #include "MyCCPObject.h" int foo1(int); int foo2(int); int fooObject(MyCCPObject myCppObject); // Function involves a Class "MyCCPObject" type #ifdef __cplusplus extern "C" { #endif int foo3(void); // Function called in a C file #ifdef __cplusplus } #endif #endif //TASK_H
我有一个函数如何处理C++;带有对C函数和类对象的外部调用的头文件 我试图编译一个包含C和C++文件的应用程序。有一个特别的标题,我面临的问题。这个文件(C++头文件)看起来会像这样: #ifndef TASK_H #define TASK_H #include "MyCCPObject.h" int foo1(int); int foo2(int); int fooObject(MyCCPObject myCppObject); // Function involves a Class "MyCCPObject" type #ifdef __cplusplus extern "C" { #endif int foo3(void); // Function called in a C file #ifdef __cplusplus } #endif #endif //TASK_H,c++,c,cross-language,C++,C,Cross Language,我有一个函数fooObject(),它有一个MyCCPObject类类型作为参数。另外,其中一个函数,foo3()将从C文件中调用。 当C编译器编译此头时,我得到以下错误:“错误:#20:标识符”类“未定义”。为了避免这种情况,我必须: 将fooObject()声明放在编译器保护中: 在头文件MyCCPObject.h的类声明中也放置编译器保护: 注意:在任何C文件中都不会调用MyCCPObject。 那么,当我有一个C++头文件时,最好的方法是: 函数将涉及一个类对象 对C文件的extern调
fooObject()
,它有一个MyCCPObject
类类型作为参数。另外,其中一个函数,foo3()
将从C文件中调用。
当C编译器编译此头时,我得到以下错误:“错误:#20:标识符”类“未定义”
。为了避免这种情况,我必须:
fooObject()
声明放在编译器保护中:MyCCPObject.h
的类声明中也放置编译器保护:MyCCPObject
。
那么,当我有一个C++头文件时,最好的方法是:
extern
调用#ifndef TASK_H
#define TASK_H
#ifdef __cplusplus
#include "MyCCPObject.h"
int foo1(int);
int foo2(int);
int fooObject(MyCCPObject myCppObject); // Function involves a Class "MyCCPObject" type
extern "C" {
#endif
int foo3(void); // Function called in a C file
#ifdef __cplusplus
}
#endif
#endif //TASK_H
选项2:如果要使fooObject
函数可从C访问,然后,可以更改MyCPOPbjult.h,以向C++提供完整的类信息,而只为C提供最小的<代码> TyPulfF。#ifdef __cplusplus
class MyCCPObject
{
public:
MyCCPObject(uint32_t val);
private:
uint32_t value;
};
#else
typedef struct MyCCPObject MyCCPObject;
#endif
和task.h to
#ifndef TASK_H
#define TASK_H
#include "MyCCPObject.h"
int foo1(int);
int foo2(int);
#ifdef __cplusplus
extern "C" {
#endif
int fooObject(MyCCPObject *myCppObject); // Function involves a Class "MyCCPObject" type
int foo3(void); // Function called in a C file
#ifdef __cplusplus
}
#endif
#endif //TASK_H
<>请注意,我需要更改C++的代码> FooObjult/Cuth>,以获取一个指向C对象的指针,因为C代码没有看到完整的类,也不知道对象的大小。他们也在考虑是否有可能这样做
< > > >选项1 :如果你只想让C编译器能够解析你的任务.h头文件,那么你就可以隐藏C++部分我的使用<代码>
#ifndef TASK_H
#define TASK_H
#ifdef __cplusplus
#include "MyCCPObject.h"
int foo1(int);
int foo2(int);
int fooObject(MyCCPObject myCppObject); // Function involves a Class "MyCCPObject" type
extern "C" {
#endif
int foo3(void); // Function called in a C file
#ifdef __cplusplus
}
#endif
#endif //TASK_H
选项2:如果要使fooObject
函数可从C访问,然后,可以更改MyCPOPbjult.h,以向C++提供完整的类信息,而只为C提供最小的<代码> TyPulfF。#ifdef __cplusplus
class MyCCPObject
{
public:
MyCCPObject(uint32_t val);
private:
uint32_t value;
};
#else
typedef struct MyCCPObject MyCCPObject;
#endif
和task.h to
#ifndef TASK_H
#define TASK_H
#include "MyCCPObject.h"
int foo1(int);
int foo2(int);
#ifdef __cplusplus
extern "C" {
#endif
int fooObject(MyCCPObject *myCppObject); // Function involves a Class "MyCCPObject" type
int foo3(void); // Function called in a C file
#ifdef __cplusplus
}
#endif
#endif //TASK_H
请注意,我需要更改<代码> FoObjs<代码>的签名,以获取一个指向该对象的指针,因为C代码没有看到完整的类,并且不知道对象的大小。
为C和C++代码使用单独的头。 将
foo3
声明(包括\uuu cplusplus
保护)移动到单独的标题中。我们称之为Foo3.h
您现在拥有以下文件:
-包含Task.h
和foo1
,foo2
的声明,包括foooobject
myccobject.h
-包含Foo3.h
Foo3
-包括Task.cpp
和Task.h
,并提供Foo3.h
、foo1
和foo2
的定义Foo3
-包括App.c
并使用Foo3.h
Foo3
构建C应用程序时,只添加
Foo3.h
和App.C
。这样,其他的头(包含C++代码)将不会被编译,因此不会产生任何错误。< P>使用C++和C++代码的单独标头。
将foo3
声明(包括\uuu cplusplus
保护)移动到单独的标题中。我们称之为Foo3.h
您现在拥有以下文件:
-包含Task.h
和foo1
,foo2
的声明,包括foooobject
myccobject.h
-包含Foo3.h
Foo3
-包括Task.cpp
和Task.h
,并提供Foo3.h
、foo1
和foo2
的定义Foo3
-包括App.c
并使用Foo3.h
Foo3
构建C应用程序时,只添加
Foo3.h
和App.C
。这样,其他的头(包含C++代码)将不会被编译,因此不会产生任何错误。什么问题,你想要达到什么?你能列出所有文件名的内容吗?好,当整个应用程序被编译时,C编译器会抛出关于“标识符”类的错误。“未定义´,因为由于´extern´调用,头文件包含在C文件中。为了避免这种情况,我使用了编译器保护。现在我不确定这是否是正确的方法。@Phalgun在我的例子中,task.h
是有问题的include文件。task.cpp
文件将使用编译后的文件,但有两种方法,如fooObject(MyCCPObject)
,它还将使用类型为MyCCPObject
的对象。接下来是C文件,一些App.C<