472,989 Members | 3,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

C runtime library woes in Managed C++

When creating a Managed C++ DLL using the Visual Studio 7.1 Solution
Explorer (by selecting Add New Project and then either choosing Class
Library (.NET) or Windows Control Library (.NET)), the IDE sets the
/MT(d) compiler switch (statically linked multithreaded C runtime
library) by default. This is fine with me, as it relieves me from having
to redistribute MSVCR71(D).DLL with my application.

However, as soon as a single runtime library function is used in my
DLL's code, the linker complains:

error LNK2019: unresolved external symbol _main referenced in
function _mainCRTStartup

main? In a DLL? Sure enough, the error goes away when I change /MT(d) to
/MD(d), but then I have to redistribute MSVCR71(D).DLL and risk all
kinds of version troubles.

I must say that information on this issue both on MSDN and the entire
Internet is rather poor. There is no indication that DLLs *require*
/MD(d) (my understanding is that the D refers to the runtime library,
not the client assembly, being a DLL), yet building them with /MT(d)
doesn't work. But why does the IDE set the latter option by default? Do
I really have to add a dummy main() function to my DLL when using
/MT(d)? Or have I missed something?
--
Gerhard Menzl

Humans may reply by replacing the obviously faked part of my e-mail
address with "kapsch".
Nov 17 '05 #1
5 3024
Gerhard,

The "The Managed C++ Wrapper Library" chapter of this paper
http://www.codeguru.com/Cpp/Cpp/cpp_...cle.php/c6867/
contains instructions that you may find useful. Give it a try and let me
know if that solves your problem.

--
Nikola Dudar
Visual C++; // Program Manager
This posting is provided "AS IS" with no warranties, and confers no rights.

"Gerhard Menzl" <ge***********@spambucket.net> wrote in message
news:41********@news.kapsch.co.at...
When creating a Managed C++ DLL using the Visual Studio 7.1 Solution
Explorer (by selecting Add New Project and then either choosing Class
Library (.NET) or Windows Control Library (.NET)), the IDE sets the /MT(d)
compiler switch (statically linked multithreaded C runtime library) by
default. This is fine with me, as it relieves me from having to
redistribute MSVCR71(D).DLL with my application.

However, as soon as a single runtime library function is used in my DLL's
code, the linker complains:

error LNK2019: unresolved external symbol _main referenced in
function _mainCRTStartup

main? In a DLL? Sure enough, the error goes away when I change /MT(d) to
/MD(d), but then I have to redistribute MSVCR71(D).DLL and risk all kinds
of version troubles.

I must say that information on this issue both on MSDN and the entire
Internet is rather poor. There is no indication that DLLs *require* /MD(d)
(my understanding is that the D refers to the runtime library, not the
client assembly, being a DLL), yet building them with /MT(d) doesn't work.
But why does the IDE set the latter option by default? Do I really have to
add a dummy main() function to my DLL when using /MT(d)? Or have I missed
something?
--
Gerhard Menzl

Humans may reply by replacing the obviously faked part of my e-mail
address with "kapsch".

Nov 17 '05 #2
Nikola Dudar [MSFT] wrote:
The "The Managed C++ Wrapper Library" chapter of this paper
http://www.codeguru.com/Cpp/Cpp/cpp_...cle.php/c6867/
contains instructions that you may find useful. Give it a try and let me
know if that solves your problem.


Thank you very much for the link. This article, and the article
"Converting Managed Extensions for C++ Projects from Pure Intermediate
Language to Mixed Mode" in the Visual C++ documentation which I
discovered after browsing the former, shed some light on the matter, but
they do not really provide a satisfactory answer. The compiler switches
/MT(d), /MD(d) etc. aren't even mentioned; it simply says "use
msvcrt.lib" (which is implicit with /MD), and it doesn't mention the
debug version.

The mentioned articles also suggest adding __DllMainCRTStartup@12 to the
Force Symbol References property, but the linker complains about the
unresolved external symbol _main. How does that fit together?

In the meantime, I have - hopefully - solved my problem by compiling all
assemblies using /MD(d) and deploying mscvr71(d).dll and msvcp71(d).dll.

Why is this important piece of information so cleverly hidden? Why is
there no mixed mode/pure MSIL switch to be found in a project's
properties or in the New Project dialog? There must be millions of lines
of C and C++ legacy code out there that people want to integrate into
..NET applications. This is not an obscure corner case.

--
Gerhard Menzl

Humans may reply by replacing the obviously faked part of my e-mail
address with "kapsch".
Nov 17 '05 #3
Gerhard,

Managed DLL built with /MT in VS2003 is subject to loader deadlock issue.
The linker throws these error messages to ensure that users are aware of the
potential deadlock. The paper, I have sent a link to in my previous post,
is a short summary of this paper http://support.microsoft.com/?id=814472,
which goes into deep details of this problem, proposes workarounds and has
links to other useful papers.

FYI, in VS2005 Beta1, the CRT design has been changed to overcome the loader
deadlock issue. The CRT provides two initialization routines - native (main,
dllmain) entry points (as they were before), in which managed code should
not be called, and managed module constructor, which properly initialize
managed binaries. This ensures that no calls to managed code are done from
inside native initialization routines, when OS loader lock is held. There is
also a better diagnostic which helps users to identify the problem. See
http://msdn.microsoft.com/visualc/de...debgb1ldlk.asp
for a detailed summary.

Also in Beta1 user can only link managed binaries with /MD[d] mostly because
of the same reasons. That is why I would say that you should build your app
with /MD. That not only helps to overcome the loader deadlock but also makes
your code ready for VS2005. However, it is always up you as an owner of the
app to know what design is better. :-)

Thanks,
--

Nikola Dudar
Visual C++; // Program Manager
This posting is provided "AS IS" with no warranties, and confers no rights.

"Gerhard Menzl" <ge***********@spambucket.net> wrote in message
news:41********@news.kapsch.co.at...
Nikola Dudar [MSFT] wrote:
The "The Managed C++ Wrapper Library" chapter of this paper
http://www.codeguru.com/Cpp/Cpp/cpp_...cle.php/c6867/
contains instructions that you may find useful. Give it a try and let me
know if that solves your problem.


Thank you very much for the link. This article, and the article
"Converting Managed Extensions for C++ Projects from Pure Intermediate
Language to Mixed Mode" in the Visual C++ documentation which I discovered
after browsing the former, shed some light on the matter, but they do not
really provide a satisfactory answer. The compiler switches /MT(d), /MD(d)
etc. aren't even mentioned; it simply says "use msvcrt.lib" (which is
implicit with /MD), and it doesn't mention the debug version.

The mentioned articles also suggest adding __DllMainCRTStartup@12 to the
Force Symbol References property, but the linker complains about the
unresolved external symbol _main. How does that fit together?

In the meantime, I have - hopefully - solved my problem by compiling all
assemblies using /MD(d) and deploying mscvr71(d).dll and msvcp71(d).dll.

Why is this important piece of information so cleverly hidden? Why is
there no mixed mode/pure MSIL switch to be found in a project's properties
or in the New Project dialog? There must be millions of lines of C and C++
legacy code out there that people want to integrate into .NET
applications. This is not an obscure corner case.

--
Gerhard Menzl

Humans may reply by replacing the obviously faked part of my e-mail
address with "kapsch".

Nov 17 '05 #4
Nikola Dudar [MSFT] wrote:
Managed DLL built with /MT in VS2003 is subject to loader deadlock issue.
The linker throws these error messages to ensure that users are aware of the
potential deadlock. The paper, I have sent a link to in my previous post,
is a short summary of this paper http://support.microsoft.com/?id=814472,
which goes into deep details of this problem, proposes workarounds and has
links to other useful papers.
<gulp> How subtle.
Also in Beta1 user can only link managed binaries with /MD[d] mostly because
of the same reasons. That is why I would say that you should build your app
with /MD. That not only helps to overcome the loader deadlock but also makes
your code ready for VS2005. However, it is always up you as an owner of the
app to know what design is better. :-)


Does that mean that using /MD spares me from the contortions described
in the article cited above?

--
Gerhard Menzl

Humans may reply by replacing the obviously faked part of my e-mail
address with "kapsch".
Nov 17 '05 #5
Yes, inVS2003 it is easier to build DLL when using /MD.
--
Nikola Dudar
Visual C++; // Program Manager
This posting is provided "AS IS" with no warranties, and confers no rights.

"Gerhard Menzl" <ge***********@spambucket.net> wrote in message
news:41********@news.kapsch.co.at...
Nikola Dudar [MSFT] wrote:
Managed DLL built with /MT in VS2003 is subject to loader deadlock issue.
The linker throws these error messages to ensure that users are aware of
the potential deadlock. The paper, I have sent a link to in my previous
post, is a short summary of this paper
http://support.microsoft.com/?id=814472, which goes into deep details of
this problem, proposes workarounds and has links to other useful papers.


<gulp> How subtle.
Also in Beta1 user can only link managed binaries with /MD[d] mostly
because of the same reasons. That is why I would say that you should
build your app with /MD. That not only helps to overcome the loader
deadlock but also makes your code ready for VS2005. However, it is always
up you as an owner of the app to know what design is better. :-)


Does that mean that using /MD spares me from the contortions described in
the article cited above?

--
Gerhard Menzl

Humans may reply by replacing the obviously faked part of my e-mail
address with "kapsch".

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jbmeeh | last post by:
I have an application in which I need to expose runtime moving and resizing of controls. Most of the functionality that I need is exposed by the CRectTracker class in the C++ library. Are there any...
7
by: wmkew | last post by:
Hello everyone I'm encountering a R6002 Runtime error and several bugs when trying to generate a simple Managed C++ application with .NET 2003. The main problem seems to arise from linking with...
0
by: Eric Sassaman [MS] | last post by:
Online Chat: Visual C++ 2005 Library and Runtime Enhancements The next release of Visual C++ includes many new and enhanced libraries which improve security, support managed code and integrate...
12
by: Markus Ewald | last post by:
I'm just experimenting around with the VisualC++ 2005 Express Edition Beta and found some strange behavior for which I can't seem to find a workaround. What I've done is set up two static library...
3
by: James Radke | last post by:
Hello, I have an asp.net application (using vb.net codebehind), that is calling some older c++ dlls. These dlls require the use of the c++ Runtime which is in the windows/System32 directories. ...
12
by: Herby | last post by:
Iv compiled my current C++ project as \clr as i want to start putting in some specific C++\CLI code to serialize my MFC objects to .NET equivalents. The program crashes on startup, something to do...
1
by: Pascalus | last post by:
Hi there! I have a problem with the delete operator to destroy a (char*). It used to work since today. I don't know what I may have changed in the project and/or the solution (probably in the...
3
by: Jonathan Wilson | last post by:
I have the following: 1.A C++ dll (with no managed code whatsoever) compiled with visual C++ 2005 that links to libcmt.lib as its runtime library. and 2.A piece of binary code (also compiled with...
0
by: creo | last post by:
OS: Windows XP (32 bit) NET: .Net 2.0 runtime DevEnv: VS 2008 C++ and C# I am attempting to use an unmanaged C++ library to load a managed C# assembly and hand off control. I have the following...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.