【问题标题】:C++ dll in C programC程序中的C++ dll
【发布时间】:2009-04-11 15:03:49
【问题描述】:

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

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__

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

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

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

Error   1   error C2059: syntax error : 'string'    

它是关于头文件中的“extern“C””部分。

我已尝试将头文件更改为:

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

甚至当我将 & 更改为 * 时出现:

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

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

【问题讨论】:

  • 我找到了答案:错误 6 错误 LNK2019: 函数 _main main.obj 中引用的未解析的外部符号 _load_obj 因为我将库项目编译为静态库,然后编译为 ddl 库和意外的 .lib已被 dll 库创建的较小的替换...

标签: c++ c dll


【解决方案1】:

参数“GLuint &amp;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...
}

【讨论】:

    【解决方案2】:

    正如大卫指出的那样,C 没有引用。

    另外,取出extern "C"。 C 没有用,也不知道它。

    如果您需要共享标头,请执行以下操作:

    #ifdef __cplusplus
        extern "C" {
    #endif
    
    /* extern "C" stuff */
    
    #ifdef __cplusplus
        }
    #endif
    

    在 C 中,__cplusplus 不会被定义。

    【讨论】:

      猜你喜欢
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多