如何避免“已定义的错误” 在 C++
我在链接应用程序时在 MFC VS6 项目中遇到这些类型的错误:
msvcrt.lib(MSVCRT.dll) : error LNK2005: _atoi already defined in LIBC.lib(atox.obj)
我知道这意味着什么(一个函数存在于 2 个不同的库中); 要解决这个问题,我应该排除 2 个库之一(msvcrt.lib
或 libc.lib
)。
但如果我这样做,就会出现各种未解决的外部错误。 所以我想继续使用这两个库。
有什么方法可以告诉链接器我想在 libc.lib
中使用 _atoi
函数,而不是在 msvcrt.lib
(或者其他方式)?
任何帮助或指导都会很棒。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此错误肯定意味着您正在链接使用不同运行时库编译的两段代码。 MSVCRT.dll 是动态版本,而 LIBC.lib 是静态版本。 如果你这样做,一切都会崩溃。 尝试找出代码的哪些部分使用哪个版本,然后解决这个问题。
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.
您遇到了运行时冲突。 使用多个运行时库通常是一件坏事。
您可以在链接器选项中使用 /nodefaultlib:msvcrt (或 /nodefaultlib:libc)来排除其中之一。
实际上,在采取这种方法之前,请检查您的项目设置。 如果我没记错的话,libc 是 VS6 中的单线程运行时,而 msvcrt 是多线程运行时。 如果您的解决方案中有多个项目,请确保它们都使用其中之一。
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.
似乎有一个选项可以用来忽略这样的错误:在projectsettings >中 链接> 检查“强制文件输出”。 即使存在链接错误,这也会生成程序。
构建输出给出如下内容:
msvcrt.lib(MSVCRT.dll) : warning LNK4006: _atoi 已在 LIBC.lib(atox.obj) 中定义; 第二个定义被忽略
当然,您需要小心使用此选项,因为它可能会生成在某些情况下无法工作的应用程序,但在这里它可能不会造成任何损害(我希望)。
感谢您的其他答复,但这似乎不是我的具体情况的选择。
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.