Windows error LNK1179
I compiled an exe on windows VS 2005, a c++ program that link to other libs of mine. It compiled and suddenly it doesn't with an error of LNK1170 COMDAT "
I can't understand what happened and开发者_StackOverflow中文版 how to solve the problem.
Thanks
There is many issues concerning this bug including:
- using of unnamed stuff (struct, class, namespace)
- instantiation of template parameter
- incremental linking
- ...
I experienced the bug in situation like this:
template < class InputIterator >
Id findOrInsertSomething( const std::string & name, InputIterator begin, InputIterator end )
{
Id out;
{
static std::string sql( calculateQueryA() );
if (findSomething( sql, name, out ))
return out;
}
static std::string sql( calculateQueryB() );
out = insertSomehing( sql, name, begin, end );
return out;
}
When used in code, link error LNK1179 occurred. Problem is simple. Even without /H switch, maximum length of names can be 2047 characters. When using template stuff, usually very long names are created. In the example there are 2 static variables with the same name. They will be named like:
sql?something-like-function-signature?something-like-block-signature
what can be easily longer then 2047 characters. Because something-like-block-signature
is at the end, it will be behind 2047 boundary, making effectively both sql
variables the same for the linker.
Solution is easy:
- using different names
- hide optimization in function (will not be calculated for each version of the template)
This is copied from http://msdn.microsoft.com/en-us/library/aa234469(VS.60).aspx
Linker Tools Error LNK1179
Visual Studio 6.0
invalid or corrupt file: duplicate comdat comdat
An object module contained two or more COMDATs with the same name.
One possible cause is if you use the /H option with /Gy. The /H option limits the length of external names, and the /Gy option packages functions in COMDATs.
For example, if you compile the following with /Gy and /H8, you will get error LNK1179 since the object module will contain two COMDATs of the same name (function1 and function2 are unique at nine characters):
void function1(void);
void function2(void);
void main(void) {
function1();
function2();
}
void function1(void) {}
void function2(void) {}
Anyway, check the official documentation about it:
http://msdn.microsoft.com/en-us/library/cddbs9aw%28VS.80%29.aspx
http://msdn.microsoft.com/en-us/library/aa234469%28VS.60%29.aspx
And if you check codeguru, a guy found another way to solve a problem similar to yours:
Project->Settings->C++ tab, Debug cathegory: Inline function expansion: change from 'None' to 'Only _inline'.
I started again the IDE and also reboot the machine and it didn't help. I managed to solve it after I clean and comiple again (not recompile option) 2 libs of mine. I don't understand why it helped but it succeeded to lnik the project.
Experienced this problem while making a bind to a member function:
std::function<void(Appender<Contact>&)> f = std::bind(&Manager::AppendContacts, this, std::placeholders::_1);
And then passing it somewhere:
gBuf.append(f);
The error was:
LNK1179: invalid or corrupt file: duplicate COMDAT (...)
The member function seems to have the exact same signature as f
(or is perhaps exceeding the max limit the linker checks)
If I instead write it inline, without the intermediate f
, there is no error.
gBuf.append(std::bind(&Manager::AppendContacts, this, std::placeholders::_1));
I don't pretend to understand what is happening here, but perhaps I can save someone a couple of hours figuring this out like I had to...
精彩评论