如何处理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
    的类声明中也放置编译器保护:
  • 注意:在任何C文件中都不会调用
    MyCCPObject
    。 那么,当我有一个C++头文件时,最好的方法是:
  • 函数将涉及一个类对象
  • 对C文件的
    extern
    调用
  • 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。 TyPulfF确保C在没有编写代码>类< <代码>或<代码>结构> /代码>之前只知道类名<代码> MycCopbBase<代码>。

    #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。 TyPulfF确保C在没有编写代码>类< <代码>或<代码>结构> /代码>之前只知道类名<代码> MycCopbBase<代码>。

    #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
    从构建系统(make,cmake等)中,在构建C++库时,添加文件<代码>任务> h <代码>,<代码> foo.H./Cuth>,<代码>任务> CPP < /C> >(和其他与代码有关的文件> MycCopbjtub>代码>


    构建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
    从构建系统(make,cmake等)中,在构建C++库时,添加文件<代码>任务> h <代码>,<代码> foo.H./Cuth>,<代码>任务> CPP < /C> >(和其他与代码有关的文件> MycCopbjtub>代码>


    构建C应用程序时,只添加
    Foo3.h
    App.C
    。这样,其他的头(包含C++代码)将不会被编译,因此不会产生任何错误。

    什么问题,你想要达到什么?你能列出所有文件名的内容吗?好,当整个应用程序被编译时,C编译器会抛出关于“标识符”类的错误。“未定义´,因为由于´extern´调用,头文件包含在C文件中。为了避免这种情况,我使用了编译器保护。现在我不确定这是否是正确的方法。@Phalgun在我的例子中,
    task.h
    是有问题的include文件。
    task.cpp
    文件将使用编译后的文件,但有两种方法,如
    fooObject(MyCCPObject)
    ,它还将使用类型为
    MyCCPObject
    的对象。接下来是C文件,一些
    App.C<