469,648 Members | 1,137 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

extern "C++" ?

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
Nov 17 '05 #1
4 1690
"Bonj" <benjtaylor at hotpop d0t com> wrote in message
news:Og**************@TK2MSFTNGP12.phx.gbl...
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


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;
}

--
Jeff Partch [VC++ MVP]
Nov 17 '05 #2
"Bonj" <benjtaylor at hotpop d0t com> wrote...
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?


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
Nov 17 '05 #3
> 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

Nov 17 '05 #4
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.

"Bonj" <benjtaylor at hotpop d0t com> wrote in message
news:ez**************@TK2MSFTNGP14.phx.gbl...
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


Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

111 posts views Thread by JKop | last post: by
8 posts views Thread by Generic Usenet Account | last post: by
1 post views Thread by Phlip | last post: by
10 posts views Thread by Mark A. Gibbs | last post: by
12 posts views Thread by G Patel | last post: by
19 posts views Thread by ccwork | last post: by
4 posts views Thread by kk_oop | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.