473,395 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1780
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sergei | last post by:
I ran into this problem. I needed to create an entry for access to a library of functions that are "extern C", and I just can't find the right syntax, if it exists at all ( I am using MSVC...
1
by: terrencel | last post by:
I was told to look at some old C code that was ported to C++. One of the file is like: ========================================= CPPClass* someCPPVar = NULL; extern "C" {
111
by: JKop | last post by:
Okay here we go, I feel it's about time people conversed about the bullshit aspects of C++ (including the bullshit stuff brought forward from C). I'll begin with a few of my own grievances: 1)...
8
by: Generic Usenet Account | last post by:
Our C++ program was linked with some legacy C functions. We had used the extern "C" declaration, and everything was working fine. Then someone thought that it would be better to have a consistent...
1
by: Phlip | last post by:
Language Lawyers: Compare this: extern "C" int Maine() { ... bool runTests(); return !runTests(); }
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
12
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
4
by: kk_oop | last post by:
Hi. I need to write a C++ callback function and register it with a C program. I've read that this can be done if the callback function is a static method. I've also read that I should use a...
4
by: mimi | last post by:
The programmer indicates to the compiler that a function is written in a different programming language using a linkage directives.It is intuitive that extern "SomeLanguage" is used to declare...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.