Note that there are some explanatory texts on larger screens.

plurals
  1. POVC++ Calling a function of /clr project from a function of non /clr project within a solution
    primarykey
    data
    text
    <p>I referred <a href="https://stackoverflow.com/questions/3994073/calling-a-function-from-a-win32-lib-project-with-clr-from-a-project-that-is-a">this somewhat similar question</a> before asking this, but unable to solve my problem</p> <p>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:</p> <ul> <li>A project (say P1) inside S has all C/C++ files and needs to call a C# function</li> <li>Since P1 also contains .c files, I can't use <code>/clr</code> option with that</li> <li>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</li> <li>So I created another project (say P2) with <code>/clr</code> 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</li> <li>Note that P1 is a .dll and P2 is created as a static library;</li> <li>P2 is mentioned under the P1's "Framework and refernces"</li> </ul> <p>and a warning:</p> <blockquote> <p>warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library</p> </blockquote> <p>Now with all these, I get 3 linker errors in P1:</p> <blockquote> <p>error LNK2005: "private: __thiscall type_info::type_info(class type_info const &amp;)" (??0type_info@@AAE@ABV0@@Z) already defined in libcmtd.lib(typinfo.obj)</p> <p>error LNK2005: "private: class type_info &amp; __thiscall type_info::operator=(class type_info const &amp;)" (??4type_info@@AAEAAV0@ABV0@@Z) already defined in libcmtd.lib(typinfo.obj)</p> <p>error LNK1169: one or more multiply defined symbols found</p> </blockquote> <p>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).<br> Important point is that, even if I remove the C# code from P2 then also the same error appears.</p> <p>What is the correct way to fix it? </p> <p><strong>Update</strong>:</p> <p>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. </p> <pre><code>void Class::foo () { // A static function inside Class std::string x = marshal_as&lt;std::string&gt;(C#_function); // ... } </code></pre> <p>P2 is newly added to compile with <code>/clr</code> (Removing P2 makes the solution compile fine).<br> I am compiling both P1 and P2 with <code>/MD[d]</code> options. And the above error is thrown by P1.</p> <p>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 <code>foo</code> itself for undefined reference:</p> <blockquote> <p>error LNK2019: unresolved external symbol "public: void __cdecl Class::foo()" referred in function { some function of P1 }</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload