How to avoid an "already defined error" in C ++

I collect these types of errors in the MFC VS6 project when linking the application:

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

I know what this means (function exists in 2 different libraries); To solve it, I had to exclude one of the two libraries ( msvcrt.lib or libc.lib ).

But if I do, there are all kinds of unresolved external errors. Therefore, I would like to use both libraries.

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

Any help or direction would be wonderful.

+6
c ++ linker visual-studio-6
source share
3 answers

There seems to be an option that you can use to ignore such errors: in projectsettings> link> check 'Force file output'. This will create the program, even if there are links.

The build result gives something like this:

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

Of course, you will need to use this parameter with caution, since it can generate an application that will not work in some cases, but here it probably does no harm (hopefully).

Thanks for the other answers, but that doesn't seem like an option in my particular case.

+1
source share

This error certainly means that you are linking two pieces of code that have been compiled using different runtime libraries. MSVCRT.dll is the dynamic version, and LIBC.lib is static. If you do this, all hell will break. Try to find which parts of your code use this version and sort it.

+11
source share

You have a run-time collision. Using multiple runtime libraries is usually bad.

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

Actually, before resorting to this, check the settings of your project. If I remember correctly, libc is a single-threaded runtime in VS6, and msvcrt is a multi-threaded runtime. If there are several projects in your solution, make sure that they all use one or the other.

+4
source share

All Articles