Inconsistent dll linkage??

P

Peter Steele

I've created an unmanaged C++ DLL and when I compile it I get numerous
errors of the form

xyz.cpp(nnn): warning C4273: '<somefunction>' : inconsistent dll linkage

I have other DLLs that have been created using the same basic pattern. I
using the approach where the following code appears in the .h file:

#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif

Each of the functions in the .h file are prefixed with this macro:

MYDLL_API unsigned int myfunc();

The DLL project defines the MYDLL_EXPORTS symbol so that the specified
functions are exported. Conversely, any app that includes this .h file will
not have the macro defined and will use the dllimport directive.

So, this is my basic approach to creating my DLLs. In a couple of cases I'm
getting these warning messages. I'm obviously missing something obvious but
I can't figure it out. The DLLs appear to be working properly but I want to
get rid of these warnings. Any ideas?
 
A

Antti Keskinen

Hi !

This error happens when the linker is unsure what it should do with a
symbol. To export/import it or not. You should make sure that at the
beginning of the header file, you have a comment #pragma once written. This
makes sure that each function declaration is done only once, no matter how
many compilation units (.cpp files) include the header.

For example, this will cause the error
__declspec(dllexport) int MyFunc();
__declspec(dllimport) int MyFunc(); // Inconsistent DLL linkage

-Antti Keskinen
 
P

Peter Steele

This doesn't seem to be the cause in this case. I get these warnings when I
compile the DLL itself, not when I link it with other apps. The DLL of
course only includes its own .h file one time so there is no chance two
declspec directuves are being compiled, or at least I don't see how...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top