Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2004

Tip: Looking for answers? Try searching our database.

extern "C++" ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bonj - 16 Oct 2004 21:15 GMT
I can use
extern "C" ...
to link a C variable or runction into a C++ file, but what if I want to link
a C++ variable into a C file? I get errors trying to use extern "C++" and it
seems like it is looking for a variable with an additional underscore at the
beginning, is the linker.
Any ideas?
Cheers
Jeff Partch [MVP] - 16 Oct 2004 21:40 GMT
> I can use
> extern "C" ...
[quoted text clipped - 4 lines]
> Any ideas?
> Cheers

I don't think the "C" compiler supports anything but extern, so your only
option is to extern "C" your C++ variable. The SDK headers conditionally
#define EXTERN_C, so you can...

// SomeCommon.h
EXTERN_C INT g_nValue;

// SomeCpp.cpp
#include "Some.h"
EXTERN_C INT g_nValue = 0;

// SomeC.c
#include "Some.h"

VOID _cdecl SomeFunc(VOID)
{
   INT n = g_nValue;
}

Signature

Jeff Partch [VC++ MVP]

Victor Bazarov - 16 Oct 2004 22:39 GMT
"Bonj" <benjtaylor at hotpop d0t com> wrote...
>I can use
> extern "C" ...
[quoted text clipped - 3 lines]
> underscore at the beginning, is the linker.
> Any ideas?

You cannot link a file compiled with a C++ compiler to another compiled
with a C compiler, using a C linker.  That's likely not going to work.
What you need is

(a) Declare the symbol in the C++ file as 'extern "C"' when compiling
(b) Link both together using a C++ linker

The reason is simple: the C language is older and when the object code
and linking specifications were developed, there was no C++, so most of
the C compilers/linkers still follow those quarter-a-century old guide-
lines.  Besides, there is a bit of that "I've been here before you, I
am better than you, and I am not going to give you the second thought"
attitude coming from the C camp.  No big deal, C++ programmers just work
around all that.

V
Bonj - 16 Oct 2004 23:26 GMT
> Besides, there is a bit of that "I've been here before you, I
> am better than you, and I am not going to give you the second thought"
> attitude coming from the C camp.  No big deal, C++ programmers just work
> around all that.

Well that's a shame that they do, but at the end of the day you know that
ultimately he who knows when to use a C file and when to use a C++ file will
build the best program.

> V
Bonj - 17 Oct 2004 13:52 GMT
You shouldn't see C programmers as rivals, if they've got a bee in their
bonnet then it's probably only because they don't even know how to use C++
they've been using C for so long. Although I think there's a rapidly
declining number of people to who this can be attributed.
At  the end of the day, the two should compliment each other.

>> Besides, there is a bit of that "I've been here before you, I
>> am better than you, and I am not going to give you the second thought"
[quoted text clipped - 6 lines]
>
>> V

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.