spin_eight
spin_eight

Reputation: 4025

How to export an array from a DLL?

I can`t export an array from a DLL. Here is my code:

" DLL header "

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

" DLL source"

 #define EXPORT
    #include "MySUMoperator.h"

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

    char ImplicitDLLName[] = "MySUMOperator";

" client main.cpp"

int main(int argc, char** argv)
{
    printf("%s", ImplicitDLLName);
}

When building I am getting from the linker this error:

Error   2   error LNK2001: unresolved external symbol _ImplicitDLLName  \Client\main.obj

// My purpose of exporting the array is to study export of different data structs from DLLs

How to cope with the error raised by linker and what rules are violated?

*UPDATE: * IDE Visual Studio 2010.
Client - is written on C++, also DLL is on C++

Upvotes: 3

Views: 3242

Answers (1)

WhozCraig
WhozCraig

Reputation: 66194

Assuming you're linking your import library correctly (and thats a big assumption), you're not declaring MYLIB correctly for importing symbols:

This:

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

Should be this:

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

Keep in mind we've little context to work with. It looks like you're trying to consume this from a C-compiled application, but without more info I can't be sure. If that is the case, then Q_DECL_IMPORT had better do the above or it still will not work. I'd start with a basic "C" linkage export and work your way up from there.


Sample EXPORTS.DLL

Exports.h

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

extern "C" EXPORTS_API char szExported[];

Exports.cpp

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

// This is an example of an exported variable
EXPORTS_API char szExported[] = "Exported from our DLL";

Sample EXPORTSCLIENT.EXE

ExportsClient.cpp

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

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

Output

Exported from our DLL

Upvotes: 6

Related Questions