C
Charles Nicholson
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;
}
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);
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
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;
}
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);
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