ohxuxa
ohxuxa

Reputation: 25

Unable to export functions from C++ DLL

I need to create a C++ MFC DLL using Visual Studio 2008. To do this I created a DLL MFC Win32 Console project in Visual Studio and made a function that creates an object and uses its method to print a string to the screen. Kind of like this:

__declspec(dllexport) void Foo(void);

where:

    void Foo(void){
    print* obj = new print;
    obj->testPrint();
    return;
}

void print::testPrint(void){
    printf("Bar\n");
    return;
}

It compiles successfully and generates the DLL, but when I try to use it I always get: "error LNK2019: unresolved external symbol "void __cdecl Foo(void)"

I tried using Dependency Walker and it doesn't show any function up in the DLL. Am I doing something wrong? I've searched a lot and still got no conclusions from what may be happening.

Upvotes: 1

Views: 2695

Answers (3)

snowdude
snowdude

Reputation: 3874

Sounds like you may not have included the header file which specifies the export in any cpp file. Remember that the cpp files are the only ones actually compiled. So make sure you include the header with the __declspec(dllexport) void Foo(void); in at least one cpp file. Also make sure that your project Linker->Input settings don't have a 'Module Definition File' (def) file specified.

Don't bother trying to link to the DLL until dependency walker shows that something is actually exported.

Upvotes: 1

Ajay
Ajay

Reputation: 18411

You need to export the symbol explicitly from CPP file also:

__declspec(dllexport)
void Foo(void)
{ 
    print* obj = new print; 
    obj->testPrint(); 
    return; 
} 

The specification in header is merely for the client (EXE), and a hint for the linker from the server (DLL).

Upvotes: 0

TheMathemagician
TheMathemagician

Reputation: 958

Windows defaults to cdecl so you need to make explicit that it's exported declspec in the caller - or export it from the DLL as cdecl instead.

Upvotes: 0

Related Questions