views:

855

answers:

4

I am gettings these type of errors in a MFC VS6 project while linking the application:

msvcrt.lib(MSVCRT.dll) : error LNK2005: _atoi already defined in LIBC.lib(atox.obj)

I know what it means (a function exists in 2 different libraries); to solve it I should have to exclude one of the 2 libraries (msvcrt.lib or libc.lib).

But if I do this there are all kinds of unresolved external errors. So I would like to keep using both libraries.

Is there any way to tell the linker that I want to use the _atoi function in libc.lib and not in msvcrt.lib (or the other way around)?

Any help or direction would be great.

+11  A: 

This error certainly means that you're linking two pieces of codes that have been compiled using distinct runtime libraries. MSVCRT.dll is the dynamic version, while LIBC.lib is the static one. If you do this, all hell break loose. Try finding which parts of your code use which version, and sort this out.

fbonnet
Just had a similar problem, as I'm not much familiar with the unmanaged world it was a bit of a surprise to me that you may need different versions of a 3rd party library depending on which runtime libraries your code and that library use. I downloaded a 3rd party lib and tried to link it with a project in VC++ 2005. Turned out the library used and older version of the(a?) runtime library - LIBCP.LIB. Fortunately the 3rd party library had its source available so I was able to comple it with the VC2005 runtime library. Wonder how I would solve this without the source.
axk
+3  A: 

You have a runtime clash. Using multiple runtime libraries is generally a bad thing.

You can use /nodefaultlib:msvcrt (or /nodefaultlib:libc) in your linker options to exclude one or the other.

Actually, before resorting to that, check your project settings. If I recall correctly, libc is the single-threaded runtime in VS6, and msvcrt is the multi-threaded runtime. If you have multiple projects in your solution, make sure they're all using one or the other.

zpasternack
libc is single-threaded static. libcmt is multi-threaded static. msvcrt is a DLL and multi-threaded. libc is no longer available in current MSVC releases.
MSalters
A: 

There seems to be an option which you can use to ignore errors like this: in projectsettings > link > check 'Force file output'. This will generate the program even if there are linkerrors.

The Build output gives something like this:

msvcrt.lib(MSVCRT.dll) : warning LNK4006: _atoi already defined in LIBC.lib(atox.obj); second definition ignored

Of course you will need to use this option with care as it can generate an application which won't work in some cases, but here it probably doesn't do any harm (I hope).

Thank you for the other replies, but that didn't seem to be an option in my particular case.

Mantichora
A: 

Make sure that in properties->c/c++->Code Generation that your project and its various dependencies are using the DLL version of the Runtime Library.

This is to prevent dependency A and B both containing msvcrt.lib functions, which would cause you to get that error at link time.

0xC0DEFACE