如何从 DLL 导出数组?

spi*_*ght 2 c++ arrays dll dllexport

我无法从 DLL 导出数组。这是我的代码:

“DLL头”

#ifdef EXPORT
    #define MYLIB __declspec(dllexport)
#else 
    #define MYLIB
#endif


extern "C" {
/* Allows to use file both with Visual studio and with Qt*/
#ifdef __cplusplus
    MYLIB double MySum(double num1, double num2);
    extern MYLIB char ImplicitDLLName[]; 
#else
    extern Q_DECL_IMPORT char ImplicitDLLName[]; 
    Q_DECL_IMPORT double MySum(double num1, double num2);
#endif
}
Run Code Online (Sandbox Code Playgroud)

“DLL源”

 #define EXPORT
    #include "MySUMoperator.h"

    double MySum(double num1, double num2)
    {
        return num1 + num2;
    }

    char ImplicitDLLName[] = "MySUMOperator";
Run Code Online (Sandbox Code Playgroud)

“客户端main.cpp”

int main(int argc, char** argv)
{
    printf("%s", ImplicitDLLName);
}
Run Code Online (Sandbox Code Playgroud)

构建时,我从链接器收到此错误:

Error   2   error LNK2001: unresolved external symbol _ImplicitDLLName  \Client\main.obj
Run Code Online (Sandbox Code Playgroud)

// 我导出数组的目的是研究从DLL中导出不同的数据结构

如何处理链接器引发的错误以及违反了哪些规则?

*更新:* IDE Visual Studio 2010。
客户端 - 用 C++ 编写,DLL 也用 C++ 编写

Who*_*aig 5

假设您正确链接导入库(这是一个很大的假设),则您没有正确声明 MYLIB 来导入符号:

这:

#ifdef EXPORT
    #define MYLIB __declspec(dllexport)
#else 
    #define MYLIB
#endif
Run Code Online (Sandbox Code Playgroud)

应该是这样的:

#ifdef EXPORT
    #define MYLIB __declspec(dllexport)
#else 
    #define MYLIB __declspec(dllimport)
#endif
Run Code Online (Sandbox Code Playgroud)

请记住,我们几乎没有什么背景可以使用。看起来您正在尝试从 C 编译的应用程序中使用它,但如果没有更多信息,我无法确定。如果是这种情况,那么 Q_DECL_IMPORT 最好执行上述操作,否则仍然不起作用。我将从基本的“C”链接导出开始,然后从那里开始工作。


EXPORTS.DLL 示例

出口.h

#ifdef EXPORTS_EXPORTS
#define EXPORTS_API __declspec(dllexport)
#else
#define EXPORTS_API __declspec(dllimport)
#endif

extern "C" EXPORTS_API char szExported[];
Run Code Online (Sandbox Code Playgroud)

导出.cpp

#include "stdafx.h"
#include "Exports.h"

// This is an example of an exported variable
EXPORTS_API char szExported[] = "Exported from our DLL";
Run Code Online (Sandbox Code Playgroud)

示例 EXPORTSCLIENT.EXE

导出客户端.cpp

#include "stdafx.h"
#include <iostream>
#include "Exports.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    cout << szExported << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出

Exported from our DLL
Run Code Online (Sandbox Code Playgroud)