C程序中的C ++ dll [英] C++ dll in C program

查看:171
本文介绍了C程序中的C ++ dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to create a dll library from C++ code and use it in C program. I'd like to export only one function:

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

库中的头文件:

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

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

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

File .lib is added in VS project options (Linker/Input/Additional dependencies). .dll is in folder where .exe is. When I compile C project - error:

Error   1   error C2059: syntax error : 'string'    

它与头文件中的 extern C部分有关。

It is about part "extern "C" " in header file.

我试图将头文件更改为:

I've tried to change header file to:

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

然后

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    

中引用的外部符号_load_obj我不知道为什么它是错误的。正确添加了.lib .h和.dll。

I've no idea why it is wrong. .lib .h and .dll are properly added.

推荐答案

参数 GLuint& object_list 的意思是在此处传递对GLuint的引用。 C没有参考。

The parameter "GLuint &object_list" means "pass a reference to an GLuint here". C doesn't have references. 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程序中的C ++ dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆