Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2840
  • Last Modified:

warning c4273: Send : inconsistent dll linkage

__declspec(dllimport)   void Send(Msg* pMessage);

Please help me to get rid of this warning:
warning c4273: Send : inconsistent dll linkage
0
bachra04
Asked:
bachra04
  • 4
  • 4
  • 4
  • +1
3 Solutions
 
evilrixSenior Software Engineer (Avast)Commented:
This generally means the compiler has seen more than one declaration of a symbol, one with and one without the __declspec specifier. Check to make sure all declarations are consistent.
0
 
bachra04Author Commented:
looks like there is some inconsistency between the .cpp and .h :
I declared the __declspec ... in .h , should I do sthg in .cpp

>>messaging.cpp(148) : warning C4273: Send' : inconsistent dll linkage
>> messaging.h(37) : see previous definition of 'Send'
0
 
js-profiCommented:
dllimport is for the executable (dll or exe) which calls the function.

dllexport is for the dll taht implements and provides the function.

so if you implemented the Send or included a header which has the Send declared but without __declspec(dllimport) or with other specifier you get that warning.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
evilrixSenior Software Engineer (Avast)Commented:
>> I declared the __declspec ... in .h , should I do sthg in .cpp
Short answer, for importing -- no.

http://msdn.microsoft.com/en-us/library/y4h7bcy6.aspx

"The DLL interface refers to all items (functions and data) that are known to be exported by some program in the system; that is, all items that are declared as dllimport or dllexport. All declarations included in the DLL interface must specify either the dllimport or dllexport attribute. However, the definition must specify only the dllexport attribute. For example, the following function definition generates a compiler error:"
0
 
pgnatyukCommented:
Sorry, for interrupting.
In the project settings of the DLL, in the definition section you can see something like MY_PROJECT_NAME_EXPORTS added. In this project, in the main header file you see:

#ifdef MY_PROJECT_NAME_EXPORTS
#define MY_PROJECT_NAME_API __declspec(dllexport)
#else
#define MY_PROJECT_NAME_API __decspec(dllimport)
#endif

If you have all this in your DLL project, you may include this DLL main header file to your executable and do not declare these exported functions in the executable code.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> Sorry, for interrupting.
pgnatyuk, your comment and suggestions are always welcome :)
0
 
pgnatyukCommented:
:)
Thanks.
0
 
bachra04Author Commented:
Hi pgnatyuk,
That's what I did I included the .h and it compiles and executes correctly but I have to get rid of warnings.
0
 
js-profiCommented:
if you included exactly the code of pgnatyuk above you must define MY_PROJECT_NAME_EXPORTS
 (or whatever name you used) in the preprocessor macros of the dll project but not in the app project.

it doesn't help you from warnings doing so, if there are more implementations of declarations of Send function somewhere in your code. Search for Send whole word case sensitive in all project cpps and headers. it must occur only once in one shared header of your app and dll project.
0
 
bachra04Author Commented:
There are other Send functions but with either different signatures or namespaces
0
 
js-profiCommented:
an exported function should not have name conflict with other functions. moreover it better should be declared with extern "C" clause to prevent from C++ name handling.

extern "C"
{
    // choose function name which doesn't have name conflict
    MY_PROJECT_NAME_API  void SendToServer(MSG* pMsg);
}
0
 
pgnatyukCommented:
One more point - if you the DLL and EXE projects in one solution, you need to compile the DLL first. You can set dependency for EXE from the DLL.
It will be good if you will show the warnings here. So we can recommend something.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>>  moreover it better should be declared with extern "C" clause to prevent from C++ name handling.
Only really necessary if it's being imported with C linkage (you can also import with C++ linkage) -- but it does make it more portable since the ABI of C++ is, um, pretty non-standard so this is a reasonable suggestion :)

>> There are other Send functions but with either different signatures or namespaces
Note though that with C linkage you need to be careful about function overloading (because you can't do it, that's what the name-mangling in C++ is for). I'm not saying it will cause you an issue, just advising you be mindful of this.
0
 
bachra04Author Commented:
When I replaced __declspec(dllimport)
with  extern __declspec(dllexport)
all the warnings are gone, but I cannot use  extern __declspec(dllexport)  for member methods .
Any clues ?
0
 
js-profiCommented:
again, the dllimport is for the calling executable. the dllexport for the dll that exports the function. you can use a header like the one pgnatyuk provided to use the same header for both calling executable and serving dll. can you say whether your project is the dll exporting the Send function or is the application calling the Send function? if the second you must not define the macro MY_PROJECT_NAME_API as it would switch to dllexport. if the first you must define the MY_PROJECT_NAME_API in the preprocessor settings so taht the dllexport was applied.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now