正确使用C语言中的C++函数

Sca*_*olo 3 c c++ struct

我今天早上一直在尝试为OpenGL设置一个对象加载器,并且已经取得了一些进展,但是现在我被困在看起来是让我的设置工作的最后一步.我的主要OpenGL程序是用来编写的c (shift.c),我的对象加载器是用来编写的C++ (loader.cpp).我还loader.h基于这个SO线程为这两个编写了链接器.

我认为我在翻译中做错了,因为到目前为止我所做的一切都没有正常工作.我的文件如下.

loader.h

#ifndef LOADER_H
#define LOADER_H

#ifdef _cplusplus
class Model_OBJ
{
  public:
    Model_OBJ();
    float* Model_OBJ::calculateNormal(float* coord1,float* coord2,float* coord3 );
    int Model_OBJ::Load(char *filename);  // Loads the model
    void Model_OBJ::Draw();         // Draws the model on the screen
    void Model_OBJ::Release();        // Release the model

    float* normals;             // Stores the normals
    float* Faces_Triangles;         // Stores the triangles
    float* vertexBuffer;          // Stores the points which make the object
    long TotalConnectedPoints;        // Stores the total number of connected verteces
    long TotalConnectedTriangles;     // Stores the total number of connected triangles

};
#endif /* __cplusplus */

#ifdef _cplusplus
extern "C" {
#endif
    struct model_obj;

    float* calculateNormal( float *coord1, float *coord2, float *coord3 );
    int Load(char* filename);
    void Release();
    void Draw();

    float* normals;             // Stores the normals
    float* Faces_Triangles;         // Stores the triangles
    float* vertexBuffer;          // Stores the points which make the object
    long TotalConnectedPoints;        // Stores the total number of connected verteces
    long TotalConnectedTriangles;     // Stores the total number of connected triangles

#ifdef _cplusplus
}
#endif

#endif /* LOADER_H */
Run Code Online (Sandbox Code Playgroud)

loader.cpp

/*
 *
 * Demonstrates how to load and display an Wavefront OBJ file.
 * Using triangles and normals as static object. No texture mapping.
 * http://talkera.org/opengl/
 *
 * OBJ files must be triangulated!!!
 * Non triangulated objects wont work!
 * You can use Blender to triangulate
 *
 */

#include "loader.h"
// Rest of my includes

class Model_OBJ
{
  public:
    Model_OBJ();
    float* calculateNormal(float* coord1,float* coord2,float* coord3 );
    int Load(char *filename);  // Loads the model
    void Draw();         // Draws the model on the screen
    void Release();        // Release the model

    float* normals;             // Stores the normals
    float* Faces_Triangles;         // Stores the triangles
    float* vertexBuffer;          // Stores the points which make the object
    long TotalConnectedPoints;        // Stores the total number of connected verteces
    long TotalConnectedTriangles;     // Stores the total number of connected triangles

};

#define POINTS_PER_VERTEX 3
#define TOTAL_FLOATS_IN_TRIANGLE 9
using namespace std;

Model_OBJ::Model_OBJ(){...}

float* Model_OBJ::calculateNormal( float *coord1, float *coord2, float *coord3 ){...}  
int Model_OBJ::Load(char* filename){...}
void Model_OBJ::Release(){...}
void Model_OBJ::Draw(){...}
Run Code Online (Sandbox Code Playgroud)

在我的shift.c档案中.我一直试图做类似以下的事情.但还没有成功.

#include "loader.h"
// Up in variable declarions
struct model_obj *obj1;

int main()
{
  obj1.Load("file.obj");
}
Run Code Online (Sandbox Code Playgroud)

Moo*_*uck 5

使用与您类似的C++结构,该标头的C部分将需要进行重大更改.通常,围绕C++类的C包装器看起来更像这样.(你也必须class改为struct.)

#ifdef _cplusplus
extern "C" {
#endif
struct Model_OBJ;

Model_OBJ* Model_OBJ_new(void);
void Model_OBJ_delete(Model_OBJ*self); 

float* Model_OBJ_calculateNormal(Model_OBJ*self,float*coord1,float*coord2,float*coord3);
int Model_OBJ_Load(Model_OBJ*self,char*filename); // Loads the model
void Model_OBJ_Draw(Model_OBJ*self); // Draws the model on the screen

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

然后在C++文件中,您还必须实现这些功能.

Model_OBJ* Model_OBJ_new(void) {return new Model_OBJ();}
void Model_OBJ_delete(Model_OBJ*self) {delete self}


float* Model_OBJ_calculateNormal(Model_OBJ*self,float*coord1,float*coord2,float*coord3)
{return self->calculateNormal(coord1,coord2,coord3);}
int Model_OBJ_Load(Model_OBJ*self,char*filename)
{return self->Load(filename);}
void Model_OBJ_Draw(Model_OBJ*self)
{return self->Draw();}
Run Code Online (Sandbox Code Playgroud)

然后C代码可以使用它:

int main() {
    Model_OBJ* obj1 = Model_OBJ_new();
    Model_OBJ_Load(obj1, "file.obj");
    Model_OBJ_delete(obj1);
}
Run Code Online (Sandbox Code Playgroud)