C程序中的C++ dll

mma*_*oka 3 c c++ dll

我想用C++代码创建一个dll库,并在C程序中使用它.我只想导出一个函数:

GLboolean load_obj (const char *filename, GLuint &object_list);
Run Code Online (Sandbox Code Playgroud)

来自库的头文件:

#ifndef __OBJ__H__
#define __OBJ__H__

#include <windows.h>  
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glu.h>
#include <GL/glut.h>

#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

extern "C" GLboolean load_obj (const char *filename, GLuint &object_list);

#endif // __3DS__H__
Run Code Online (Sandbox Code Playgroud)

在.cpp(在库项目中)函数也声明为:

extern "C" GLboolean load_obj (const char *filename, GLuint &object_list)
{
 code...
}
Run Code Online (Sandbox Code Playgroud)

文件.lib添加在VS项目选项(链接器/输入/附加依赖项)中..dll位于.exe所在的文件夹中.当我编译C项目时 - 错误:

Error   1   error C2059: syntax error : 'string'    
Run Code Online (Sandbox Code Playgroud)

它是关于头文件中的"extern"C""部分.

我试图将头文件更改为:

extern GLboolean load_obj (const char *filename, GLuint &object_list);
Run Code Online (Sandbox Code Playgroud)

然后

Error   1   error C2143: syntax error : missing ')' before '&'  
Error   2   error C2143: syntax error : missing '{' before '&'  
Error   3   error C2059: syntax error : '&' 
Error   4   error C2059: syntax error : ')' 
Run Code Online (Sandbox Code Playgroud)

甚至当我改变&为*出现了:

Error   6   error LNK2019: unresolved external symbol _load_obj referenced in function _main    main.obj    
Run Code Online (Sandbox Code Playgroud)

我不知道为什么这是错的..lib .h和.dll已正确添加.

Dav*_*itt 11

参数" GLuint &object_list"表示"在此处传递对GLuint的引用".C没有参考.请改用指针.

// declaration
extern "C" GLboolean load_obj (const char *filename, GLuint *object_list);

// definition
GLboolean load_obj (const char *filename, GLuint *object_list)
{
    code...
}
Run Code Online (Sandbox Code Playgroud)