473,395 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
3 5072
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Florian Liefers | last post by:
"Hello World\n", i have the following problem: One of my headerfiles for a lib is including <vector>. When i compile the lib, everything is done well. In my application another file is...
5
by: J | last post by:
Hi everyone, I started embedding python into a 3D graphics App and I came across this linking problem. SO.lib(ScnGlobal.obj) : error LNK2001: unresolved external symbol...
1
by: dullboy | last post by:
I have a non-MFC project which generated some linker errors "uuid.lib(ieguids.obj) : error LNK2005: _IID_Ixxx already defined in xxx.obj. _IID_Ixxx is an user-defined ID and xxx.obj is a user obj....
3
by: Jon | last post by:
Hi! I have a problem that have followed me for a long time. When linking, sometimes this link error below arises. My solution is always to remove all files in the project, throw away all...
3
by: Taran | last post by:
Hi all, I have this config.h file which has all the declarations for the vars being used in the application. There are no compilation errors but link errors for all the vars declared in this...
1
by: sethuganesh | last post by:
HI, i have ported vc++ 6.0 code to visual studio 2005. During batch build in debug mode i din't get any error.But if i build the same in release mode i am getting the following error. ...
1
by: jemai_linda | last post by:
Hi, When i build my project i have the following linking errors: nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) déjà défini(e) dans...
2
by: ppuniversal | last post by:
hello, my program snippet: /*****************************/ /******runThread Function*****/ /*to accept other client connections*/ /*****************************/ DWORD WINAPI...
6
by: fcvcnet | last post by:
Hi all, I defined a class, as fellows: // Segment.h #pragma once #include "MyPoint.h" enum TLSC {PARALLEL, INTERSECT, COINSIDE,INTERSECTATDIASTOLE} twolinesolutioncases;
9
by: dewi | last post by:
Dear All, I have several problem about VC++. I succeed to convert Simulink MATLAB to C code using Real-Time Workshop. I am trying to compile a C code using Visual C++ and found the error. Can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.