How can I resolve this link error in Visual Studio (LNK2005)?

心已入冬 提交于 2019-11-30 23:03:00

Check a few things:

  1. Are your header files guarded. I.e. do they have #ifndef guards.

  2. Are you defining (non-template) functions in headers without the inline keyword. That messes lots of stuff up.

  3. Are you trying to define templates in a .cpp file. All template definitions need to be in headers.

Post some code and exact error text please!

Your problem is that you're linking with two files providing the same symbol.

You haven't provided the real error message so we can't tell you exactly what the problem is but it's likely to be that you're linking with libraries from two different versions of Visual Studio.

There are also solutions available by searching the web (I assume you did this but just missed the articles in question :-) that suggest you can fix the problem by changing the project options from "Multi-threaded Debug(/MTd)" to "Multi-threaded Debug DLL (/MTD)" but I haven't looked in to this.

Please post the complete error so we can offer more targeted assistance.

Make sure the option you select for Runtime Libary linking is the same for every project and library. Project Properties -> Configuration Properties -> C/C++ -> Code Generation -> Runtime Library -> "Multi-threaded" / "Multi-threaded DLL" / ...

My issue was all of my C++ projects were "Multi-threaded" but I was referencing fortran modules that were "Multi-threaded DLL"

The MSDN article on LNK4098 has a very useful table: it tells you which libraries to manually add to the "Ignore specific library" list, depending on which CRT you're using. You need to pick a CRT (Multithreaded or not; static or DLL; debug or release), and then add the ignore libraries based on your choice.

The underlying cause is described in more detail in KB154753 ... libraries that a program will link with when built by using Visual C++

My interpretation of this is that in certain situations the algorithm that automatically picks which CRT libraries to link your code with will pick several conflicting libraries.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!