extern "C++" ?

  • Thread starter Thread starter Bonj
  • Start date Start date
B

Bonj

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
 
Bonj said:
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;
}
 
Bonj said:
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
 
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.
 
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.
 
Back
Top