DLL Linkage warning

Started by
4 comments, last by jonbell 18 years, 1 month ago
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?
Advertisement
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 =)
moe.ron
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.
Its not the order of the headers. Has anyone solved this problem?
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.
ASCII stupid question, get a stupid ANSI
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?

This topic is closed to new replies.

Advertisement