简体   繁体   English

C程序中的C ++ dll

[英]C++ dll in C program

I'd like to create a dll library from C++ code and use it in C program. 我想用C ++代码创建一个dll库,并在C程序中使用它。 I'd like to export only one function: 我只想导出一个函数:

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

Header file from library: 来自库的头文件:

#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__

in .cpp (in library project) function is also declared as: 在.cpp(在库项目中)函数也声明为:

extern "C" GLboolean load_obj (const char *filename, GLuint &object_list)
{
 code...
}

File .lib is added in VS project options (Linker/Input/Additional dependencies). 文件.lib添加在VS项目选项(链接器/输入/附加依赖项)中。 .dll is in folder where .exe is. .dll位于.exe所在的文件夹中。 When I compile C project - error: 当我编译C项目时 - 错误:

Error   1   error C2059: syntax error : 'string'    

It is about part "extern "C" " in header file. 它是关于头文件中的“extern”C“”部分。

I've tried to change header file to: 我试图将头文件更改为:

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

then 然后

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 : ')' 

and even when I changed & to * appeared: 甚至当我改变为*出现了:

Error   6   error LNK2019: unresolved external symbol _load_obj referenced in function _main    main.obj    

I've no idea why it is wrong. 我不知道为什么这是错的。 .lib .h and .dll are properly added. .lib .h和.dll已正确添加。

The parameter " GLuint &object_list " means "pass a reference to an GLuint here". 参数“ GLuint &object_list ”表示“在此处传递对GLuint的引用”。 C doesn't have references. C没有参考。 Use a pointer instead. 请改用指针。

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

// definition
GLboolean load_obj (const char *filename, GLuint *object_list)
{
    code...
}

C has no references, as David pointed out. 大卫指出,C没有参考。

In addition, take out extern "C". 另外,取出extern“C”。 C does not have a use for nor know about it. C没有用途也不了解它。

If you need to share the header, do something like: 如果您需要共享标题,请执行以下操作:

#ifdef __cplusplus
    extern "C" {
#endif

/* extern "C" stuff */

#ifdef __cplusplus
    }
#endif

In C, __cplusplus won't be defined. 在C中,不会定义__cplusplus。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM