I referred this somewhat similar question before asking this, but unable to solve my problem
I am looking at an old application with many solutions. The problem is happening in one of the solutions (say S). Here is the situation:
- A project (say P1) inside S has all C/C++ files and needs to call a C# function
- Since P1 also contains .c files, I can't use
/clr
option with that - If I compile the .c files in P1 as .cpp files then it generates lots of errors, I don't intend to change the source in that legacy .c file
- So I created another project (say P2) with
/clr
enabled and created one header file for function declaration and a .cpp file for the function definition; The C# call is made under it; P2 compiles fine - Note that P1 is a .dll and P2 is created as a static library;
- P2 is mentioned under the P1's "Framework and refernces"
and a warning:
warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
Now with all these, I get 3 linker errors in P1:
error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@ABV0@@Z) already defined in libcmtd.lib(typinfo.obj)
error LNK2005: "private: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info@@AAEAAV0@ABV0@@Z) already defined in libcmtd.lib(typinfo.obj)
error LNK1169: one or more multiply defined symbols found
This error is available at many online forums including this website. But somehow I am not able to fix it after trying those options (I am new to .NET framework).
Important point is that, even if I remove the C# code from P2 then also the same error appears.
What is the correct way to fix it?
Update:
P2 just contains 1 header file with function declaration and 1 source file with function definition which is a 1 line call to the C# method; e.g.
void Class::foo () { // A static function inside Class
std::string x = marshal_as<std::string>(C#_function);
// ...
}
P2 is newly added to compile with /clr
(Removing P2 makes the solution compile fine).
I am compiling both P1 and P2 with /MD[d]
options. And the above error is thrown by P1.
If I make P2 from static library (.lib) to dynamic linked library (.dll), then the above errors goes away. And the new linker error comes for the foo
itself for undefined reference:
error LNK2019: unresolved external symbol "public: void __cdecl Class::foo()" referred in function { some function of P1 }