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

Importing a type library into unmanaged code with /clr switch - linker errors

P: n/a
I have a VS2005 C++ MFC project which #imports a type library. The
goal is to introduce some managed code eventually, but for starters I
just need to set the /clr compiler option and build the project.

The type library is imported like so:

#import <LtipClient.tlbno_namespace named_guids

With the /clr switch on, I got lots of LNK2028 errors, so I added the
following directive to the top of each cpp source file

#pragma unmanaged

and I now get lots of LNK2001 and LNK2019 linker errors (see below)

I thought that unmanaged code should be able to use the COM objects as
normal? If I remove the /clr switch the project compiles just fine..

3>CommonView.obj : error LNK2019: unresolved external symbol "public:
long __thiscall ILTIPDataProvider::DispatchMessage(struct ILTIPMessage
*)" (?DispatchMessage@ILTIPDataProvider@@QAEJPAUILTIPM essage@@@Z)
referenced in function "public: void __thiscall
CCommonView::FeedHistory(void)" (?FeedHistory@CCommonView@@QAEXXZ)
3>Penelope.obj : error LNK2001: unresolved external symbol "public:
long __thiscall ILTIPDataProvider::DispatchMessage(struct ILTIPMessage
*)" (?DispatchMessage@ILTIPDataProvider@@QAEJPAUILTIPM essage@@@Z)

Apr 27 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
>I have a VS2005 C++ MFC project which #imports a type library. The
goal is to introduce some managed code eventually, but for starters I
just need to set the /clr compiler option and build the project.

The type library is imported like so:

#import <LtipClient.tlbno_namespace named_guids

With the /clr switch on, I got lots of LNK2028 errors, so I added the
following directive to the top of each cpp source file

#pragma unmanaged
Hi,

The best practise with /clr is to only define it for individual files. No
for whole projects. This can lead to a multitude of problems.
You also should not use #pragma unmanaged. source files should be wholly
compiled either managed or unmanaged, but not mixed. Doing so can lead to
CRT initialization problems.

Kind regards,
Bruno van Dooren MVP - VC++
http://msmvps.com/blogs/vanDooren
br**********************@hotmail.com

Apr 28 '07 #2

P: n/a
>
Hi,

The best practise with /clr is to only define it for individual files. No
for whole projects. This can lead to a multitude of problems.
You also should not use #pragma unmanaged. source files should be wholly
compiled either managed or unmanaged, but not mixed. Doing so can lead to
CRT initialization problems.

Kind regards,
Bruno van Dooren MVP - VC++
http://msmvps.com/blogs/vanDooren
bruno_nos_pam_van_doo...@hotmail.com
Thanks,

I made some progress by just enabling /clr on the module (cpp file)
only, but when I add managed code into the header file like:

#include <afxwinforms.h>

using namespace System;
using namespace System::Windows::Forms;

I get the compiler error:

3>C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include
\afxwinforms.h(19) : fatal error C1189: #error : MFC WinForms support
requires /clr (doesn't support oldSyntax)

How do I enable /clr for the header as well as the cpp without
applying it to the whole project?

Many thanks,

Duncan
Apr 28 '07 #3

P: n/a
On Apr 28, 4:16 pm, Duncan Smith <DSmith1...@googlemail.comwrote:
Hi,
The best practise with /clr is to only define it for individual files. No
for whole projects. This can lead to a multitude of problems.
You also should not use #pragma unmanaged. source files should be wholly
compiled either managed or unmanaged, but not mixed. Doing so can lead to
CRT initialization problems.
Kind regards,
Bruno van Dooren MVP - VC++
http://msmvps.com/blogs/vanDooren
bruno_nos_pam_van_doo...@hotmail.com

Thanks,

I made some progress by just enabling /clr on the module (cpp file)
only, but when I add managed code into the header file like:

#include <afxwinforms.h>

using namespace System;
using namespace System::Windows::Forms;

I get the compiler error:

3>C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include
\afxwinforms.h(19) : fatal error C1189: #error : MFC WinForms support
requires /clr (doesn't support oldSyntax)

How do I enable /clr for the header as well as the cpp without
applying it to the whole project?

Many thanks,

Duncan
Answer is to use #ifdef _MANAGED in the headers.

Apr 29 '07 #4

P: n/a

"Duncan Smith" <DS********@googlemail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
On Apr 28, 4:16 pm, Duncan Smith <DSmith1...@googlemail.comwrote:
Hi,
The best practise with /clr is to only define it for individual files.
No
for whole projects. This can lead to a multitude of problems.
You also should not use #pragma unmanaged. source files should be
wholly
compiled either managed or unmanaged, but not mixed. Doing so can lead
to
CRT initialization problems.
Kind regards,
Bruno van Dooren MVP - VC++
http://msmvps.com/blogs/vanDooren
bruno_nos_pam_van_doo...@hotmail.com

Thanks,

I made some progress by just enabling /clr on the module (cpp file)
only, but when I add managed code into the header file like:

#include <afxwinforms.h>

using namespace System;
using namespace System::Windows::Forms;

I get the compiler error:

3>C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include
\afxwinforms.h(19) : fatal error C1189: #error : MFC WinForms support
requires /clr (doesn't support oldSyntax)

How do I enable /clr for the header as well as the cpp without
applying it to the whole project?

Many thanks,

Duncan

Answer is to use #ifdef _MANAGED in the headers.
If I understand your problem correctly, you are trying to include all your
headers in one place to use as a precompiler header (stdafx.h perhaps)? If
that is the case, then #ifdef _MANAGED isn't going to help. Precompiled
headers shouldn't be shared between modules compiled with significantly
different options, such as with and without /clr. You could create two
precompiled headers, or turn them off for half the program (or off
entirely).
May 7 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.