By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,241 Members | 2,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,241 IT Pros & Developers. It's quick & easy.

LNK2005 from a C++/CLI dll linking against a non-CLR C++ static library

P: n/a
Hello all-

I have some static C++ libraries that I wrote in VS2003 but which
upgraded fine when i went to VS2005 Pro. In them i overload the global
versions of operators new, new[], delete, and delete[]. I also use the
STL and parts of boost (shared_ptr<> and weak_ptr<>) pretty heavily.
They have dependencies on various windows libs (dbghelp, winsock, etc...).

These static libraries have no common runtime support at all and use the
Multi-Byte character set.

I'm trying to expose the functionality provided by my libs up to .NET by
way of a C++/CLI DLL that links against them and exposes a new public
ref class in the generated assembly. This new CLR class will wrap and
broker their functionality to various C# apps.

I'm running into nasty linker errors, so I wrote a tiny little test case
that reproduces the failures I'm getting. I'd really appreciate it if
anyone would mind taking the time to look over them. The smallest way I
could repro it was to create a VS2005 solution with 2 projects. The
first is a C++ static library containing the dummy implementation and
the second is a C++/CLR executable that uses the static library. Note
that the problem occurs even if the C++/CLR project is a DLL; it doesn't
seem related in any obvious way.

The non-CLR static library is contained in UnmanagedTest.h and
UnmanagedTest.cpp as follows.

Unmanaged.h:
-----------------------------------------------------
#pragma once
#include <vector>

class Test
{
public:
Test();
~Test();

int* x;
std::vector<int> y;
};
-----------------------------------------------------

Unmanaged.cpp:
-----------------------------------------------------
#include "UnmanagedTest.h"
#include <cstdlib>

void* operator new(size_t size)
{
return std::malloc(size);
}

void operator delete(void* mem)
{
std::free(mem);
}

Test::Test()
{
x = new int[32];
for (int i = 0; i < 32; ++i)
x[i] = i;
}

Test::~Test()
{
delete[] x;
}
-----------------------------------------------------

The .NET C++/CLI executable has the default AssemblyInfo.cpp and one
..cpp file.

CppCliTest.cpp:
-----------------------------------------------------
#pragma unmanaged
#include "UnmanagedCppLib/UnmanagedTest.h"
#pragma managed

using namespace System;

int main()
{
Test* test = new Test;

for (int i = 0; i < 32; ++i)
Console::WriteLine(test->x[i]);

delete test;

return 0;
}
-----------------------------------------------------

The linker errors i'm getting are as follows:

-----------------------------------------------------
UnmanagedCppLib.lib(UnmanagedTest.obj) : error LNK2005: "void __cdecl
operator delete(void *)" (??3@YAXPAX@Z) already defined in
MSVCRT.lib(MSVCR80.dll)

D:\source\CppCliTest\Release\CppCliTest.exe : fatal error LNK1169: one
or more multiply defined symbols found
-----------------------------------------------------

My UnmanagedCppLib is set to use the Debug Multithreaded DLL CRT in
debug config and the Multithreaded DLL CRT in release. Interestingly,
this all compiles cleanly, links cleanly, and runs as expected in Debug
configuration. These link errors are only in Release build.

Here's where it gets a little crazy- I've found two separate ways to
make this work in Release configuration:

1. remove the overloads of global operators new and delete from
UnmanagedTest.cpp.

2. remove all references to std::vector from UnmanagedTest.h and
UnmanagedTest.cpp

Making either of these changes gets rid of the link errors.

I figure I must be doing something wrong here, but after paring it down
this far I can't figure out what to try next. Any time i see MSVCRT.lib
conflicts I immediately think that i'm using the wrong version of the
CRT but i only have one static lib (it uses MT DLL) and one C++/CLI DLL
(it has no choice but to use MT DLL).

Any advice or suggestions would be greatly appreciated.

Thanks in advance,
Charles Nicholson
Feb 25 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Charles Nicholson wrote:
Hello all-

I have some static C++ libraries that I wrote in VS2003 but which
upgraded fine when i went to VS2005 Pro. In them i overload the global
versions of operators new, new[], delete, and delete[]. I also use the
STL and parts of boost (shared_ptr<> and weak_ptr<>) pretty heavily.
They have dependencies on various windows libs (dbghelp, winsock, etc...).

These static libraries have no common runtime support at all and use the
Multi-Byte character set.

I'm trying to expose the functionality provided by my libs up to .NET by
way of a C++/CLI DLL that links against them and exposes a new public
ref class in the generated assembly. This new CLR class will wrap and
broker their functionality to various C# apps.

I'm running into nasty linker errors, so I wrote a tiny little test case
that reproduces the failures I'm getting. I'd really appreciate it if
anyone would mind taking the time to look over them. The smallest way I
could repro it was to create a VS2005 solution with 2 projects. The
first is a C++ static library containing the dummy implementation and
the second is a C++/CLR executable that uses the static library. Note
that the problem occurs even if the C++/CLR project is a DLL; it doesn't
seem related in any obvious way.

The linker errors i'm getting are as follows:

-----------------------------------------------------
UnmanagedCppLib.lib(UnmanagedTest.obj) : error LNK2005: "void __cdecl
operator delete(void *)" (??3@YAXPAX@Z) already defined in
MSVCRT.lib(MSVCR80.dll)

D:\source\CppCliTest\Release\CppCliTest.exe : fatal error LNK1169: one
or more multiply defined symbols found
-----------------------------------------------------


I tossed up a very small repro case i described in the original post at
http://cnicholson.net/CppCliTest.zip if any brave and kind souls feel
like taking a stab at it. (note, the filename is case-sensitive). This
is for VS2005 only.

It really feels like i'm doing something obviously wrong, but nothing's
jumping out at me.

Thanks in advance for any suggestions, corrections, ideas, etc...

Regards,
Charles Nicholson
Feb 27 '06 #2

P: n/a
Charles Nicholson wrote:
I have some static C++ libraries that I wrote in VS2003 but which
upgraded fine when i went to VS2005 Pro. In them i overload the
global versions of operators new, new[], delete, and delete[]. I also
use the STL and parts of boost (shared_ptr<> and weak_ptr<>) pretty
heavily. They have dependencies on various windows libs (dbghelp,
winsock, etc...).

These static libraries have no common runtime support at all and use
the Multi-Byte character set.

I'm trying to expose the functionality provided by my libs up to .NET
by way of a C++/CLI DLL that links against them and exposes a new
public ref class in the generated assembly. This new CLR class will
wrap and broker their functionality to various C# apps.
The linker errors i'm getting are as follows:

-----------------------------------------------------
UnmanagedCppLib.lib(UnmanagedTest.obj) : error LNK2005: "void __cdecl
operator delete(void *)" (??3@YAXPAX@Z) already defined in
MSVCRT.lib(MSVCR80.dll)

D:\source\CppCliTest\Release\CppCliTest.exe : fatal error LNK1169: one
or more multiply defined symbols found
-----------------------------------------------------


I tossed up a very small repro case i described in the original post at
http://cnicholson.net/CppCliTest.zip if any brave and kind souls feel
like taking a stab at it. (note, the filename is case-sensitive). This
is for VS2005 only.


I should also note that if I compile the static library with CLR support
that everything works fine. However, I'd like to use them the way they
were intended; as non-CLR libs. There are a fair amount of them and I'm
not crazy about introducing a new build config...

regards,
charles nicholson
Feb 28 '06 #3

P: n/a
I tried to reproduce your problem, but your solution copiles and links fine
on my machine.

According to you description, your poblems probably exist because you do not
use the right CRT in one of your libs. Linking with /clr object files
requires you to compile all your object files with the multithreaded DLL
variant of the CRT and to use only libs compiled with the multithreaded DLL
variant of the CRT.

Marcus

"Charles Nicholson" <ne**@cnicholson.net> wrote in message
news:eO*************@TK2MSFTNGP12.phx.gbl...
Charles Nicholson wrote:
Hello all-

I have some static C++ libraries that I wrote in VS2003 but which
upgraded fine when i went to VS2005 Pro. In them i overload the global
versions of operators new, new[], delete, and delete[]. I also use the
STL and parts of boost (shared_ptr<> and weak_ptr<>) pretty heavily. They
have dependencies on various windows libs (dbghelp, winsock, etc...).

These static libraries have no common runtime support at all and use the
Multi-Byte character set.

I'm trying to expose the functionality provided by my libs up to .NET by
way of a C++/CLI DLL that links against them and exposes a new public ref
class in the generated assembly. This new CLR class will wrap and broker
their functionality to various C# apps.

I'm running into nasty linker errors, so I wrote a tiny little test case
that reproduces the failures I'm getting. I'd really appreciate it if
anyone would mind taking the time to look over them. The smallest way I
could repro it was to create a VS2005 solution with 2 projects. The
first is a C++ static library containing the dummy implementation and the
second is a C++/CLR executable that uses the static library. Note that
the problem occurs even if the C++/CLR project is a DLL; it doesn't seem
related in any obvious way.

The linker errors i'm getting are as follows:

-----------------------------------------------------
UnmanagedCppLib.lib(UnmanagedTest.obj) : error LNK2005: "void __cdecl
operator delete(void *)" (??3@YAXPAX@Z) already defined in
MSVCRT.lib(MSVCR80.dll)

D:\source\CppCliTest\Release\CppCliTest.exe : fatal error LNK1169: one or
more multiply defined symbols found
-----------------------------------------------------


I tossed up a very small repro case i described in the original post at
http://cnicholson.net/CppCliTest.zip if any brave and kind souls feel like
taking a stab at it. (note, the filename is case-sensitive). This is for
VS2005 only.

It really feels like i'm doing something obviously wrong, but nothing's
jumping out at me.

Thanks in advance for any suggestions, corrections, ideas, etc...

Regards,
Charles Nicholson

Feb 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.