Sign in to follow this  
jonbell

DLL Linkage warning

Recommended Posts

In my MFC I have overridden printf like this : in my header : extern "C" int printf(const char* szStr, ...); then in my cpp : extern "C" int printf(const char* szStr, ...) { } The code behaves as i would expect except that I get the following warning for every file that includes the header : warning C4273: 'printf' : inconsistent dll linkage How do i need to specify this to stop this warning?

Share this post


Link to post
Share on other sites
it happens because you have different linkage for your version of printf. The original function is defined

extern "C" _CRTIMP int _cdecl printf( const char*, ... );

If your using a dll runtime library _CRTIMP is defined as __declspec dllimport). I was under the impression you can't override a C function, but I've been messing around in VC 6 and its letting me do it. However, I haven't been able to resolve this issue because if I make the function signature to be as follows

extern "C" __declspec(dllexport) int _cdecl printf( const char*, ... );

It still complains to me. Since I've edited this post and my previous one about 100 times in 10 minutes, I'm just going to leave the rest of the explaining up to the professionals =)

Share this post


Link to post
Share on other sites
I'm not sure, but it may simply be that you are including your headers in the wrong order. Ex:

#include "MyPrintf.h"
#include <cstdio>


Then the standard header's definition will override yours. Clearly its linking against yours if it's working as you expect. In this case switching the order of the headers (or simply not including the standard header) might work.

Share this post


Link to post
Share on other sites
well I dont know much about the nature of the linking problem but a quick fix would just be to call the function something else and use macros to have all the other printf's reference your version. This seems like an ideal solution unless you are trying to do something tricky like have external libraries use your version. Anyway... good luck on it.

Share this post


Link to post
Share on other sites
My ocde compiles to a lib which is then called by another project so macro's are no good. Surely there is a way to get rid of this warning. Anyone?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this