473,322 Members | 1,408 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,322 software developers and data experts.

using c++ features in extern "C"

Hi Everyone,

I have the following code,

void f(int &)
{
printf("in f...\n");
}

#ifdef __cplusplus
extern "C"
{
#endif

int main()
{
int i = 5;
int &j = i;
f(i);
return(0);
}

#ifdef __cplusplus
}
#endif

and it compiles and works fine with vc++ 6.0. Does the compiler ignore
the c linkage?
Dec 6 '07 #1
7 1745
Rahul wrote:
Hi Everyone,

I have the following code,

void f(int &)
{
printf("in f...\n");
}

#ifdef __cplusplus
extern "C"
{
#endif

int main()
{
int i = 5;
int &j = i;
f(i);
return(0);
}

#ifdef __cplusplus
}
#endif

and it compiles and works fine with vc++ 6.0. Does the compiler ignore
the c linkage?
There was a proposal recently in comp.lang.c++.moderated to treat
all VC++ v6 related queries as off-topic. The reason was that VC++
v6 is so non-standard, that you can't infer anything from the fact
that some code "works" when produced by VC++ v6. Not that you would
use a single compiler to see whether some construct is legal, but v6
of VC++ is just awful.

Now, your program is ill-formed because you changed the declaration
of the 'main' function. Do not do that. Use some other function
to fiddle with linkage, etc. I'll answer your question pretending
that your function is not 'main', but 'mian' or 'aimn' or 'mina' or
some such, and you just mistyped it.

You're not precluded from using C++ constructs in a C++ program.
No buts about it. When you add C linkage to a function, the
function becomes callable from C (and that's the intent of the
linkage) or you can declare a function as coming from a C program
(library) and call it from your C++ program. The body of your
C++ function can contain any C++ stuff you wish to put there.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 6 '07 #2
On Dec 6, 10:08 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Rahul wrote:
Hi Everyone,
I have the following code,
void f(int &)
{
printf("in f...\n");
}
#ifdef __cplusplus
extern "C"
{
#endif
int main()
{
int i = 5;
int &j = i;
f(i);
return(0);
}
#ifdef __cplusplus
}
#endif
and it compiles and works fine with vc++ 6.0. Does the compiler ignore
the c linkage?

There was a proposal recently in comp.lang.c++.moderated to treat
all VC++ v6 related queries as off-topic. The reason was that VC++
v6 is so non-standard, that you can't infer anything from the fact
that some code "works" when produced by VC++ v6. Not that you would
use a single compiler to see whether some construct is legal, but v6
of VC++ is just awful.

Now, your program is ill-formed because you changed the declaration
of the 'main' function. Do not do that. Use some other function
to fiddle with linkage, etc. I'll answer your question pretending
that your function is not 'main', but 'mian' or 'aimn' or 'mina' or
some such, and you just mistyped it.

You're not precluded from using C++ constructs in a C++ program.
No buts about it. When you add C linkage to a function, the
function becomes callable from C (and that's the intent of the
linkage) or you can declare a function as coming from a C program
(library) and call it from your C++ program. The body of your
C++ function can contain any C++ stuff you wish to put there.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Further to this, recently we came up with a project where c (extern
"C") code calls c++ function.
The c++ function accepts a pointer to an int and returns an int.

There was a suggestion to change the prototype of c++ function to
accept a reference. In that case, is it possible for the c code
to call c++ function accepting a reference?
Dec 6 '07 #3
Rahul wrote:
[..], recently we came up with a project where c (extern
"C") code calls c++ function.
The c++ function accepts a pointer to an int and returns an int.

There was a suggestion to change the prototype of c++ function to
accept a reference. In that case, is it possible for the c code
to call c++ function accepting a reference?
No. C has no references. You'd have to write a wrapper.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 6 '07 #4
Rahul wrote:
I have the following code,

void f(int &)
{
printf("in f...\n");
}

#ifdef __cplusplus
extern "C"
{
#endif

int main()
{
int i = 5;
int &j = i;
f(i);
return(0);
}

#ifdef __cplusplus
}
#endif

and it compiles and works fine with vc++ 6.0. Does the compiler ignore
the c linkage?
Why shouldn't it work? There's absolutely no problems with using "C++
features" inside the function with C linkage. There's no reason there
should be any problems. Why?

The only thing language-specific linkage affects is... well... linkage,
which is roughly limited to a set of certain _external_ properties of
the function, which in the end basically boil down to the formal
requirement that only one function with any given name can have C
linkage (i.e. you can't have several overloaded functions with C linkage).

Of course, it also might make no sense to declare as 'extern "C"'
function that rely on "C++ features" in their parameter declarations
(classes, references etc.), yet it might have some limited usability, so
the language doesn't prohibit such declarations.

--
Best regards,
Andrey Tarasevich
Dec 6 '07 #5
On Dec 6, 5:25 pm, Rahul <sam_...@yahoo.co.inwrote:
I have the following code,
void f(int &)
{
printf("in f...\n");
}
#ifdef __cplusplus
extern "C"
{
#endif
int main()
{
int i = 5;
int &j = i;
f(i);
return(0);
}
#ifdef __cplusplus
}
#endif
and it compiles and works fine with vc++ 6.0. Does the
compiler ignore the c linkage?
Sort of. Main (or rather ::main) is a very, very special
function, which follows different rules than any other function.
Typically, for example, it will be generated as if it were an
extern "C" function, even without the user requesting it. It is
treated specially regardless of where it occurs. The standard
specifically forbids declaring it static or inline, and I
suspect that the standard would have also forbid the above, had
anyone thought of it (because it's simpler to ban it, and
there's no good reason to allow it). But since they didn't ban
it, the above is perfectly legal code. And in it, main remains
main, to be treated specially. (Note, for example, that
declaring `extern "C"' doesn't give you the right to call it
recursively, just because you are allowed to call it recursively
in C.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 7 '07 #6
On Dec 6, 6:08 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
There was a proposal recently in comp.lang.c++.moderated to treat
all VC++ v6 related queries as off-topic.
I pretty much disagree with the current moderation trends in
comp.lang.c++.moderated---to the point of abandoning the group
completely (although I was one of the founders). But I don't
think they've gotten to the point of censuring because of the
version of the compiler you use.

If someone is trying to implement something using some of the
latest template techniques, of course, I would certainly
recommend that they upgrade the compiler. But not all
organizations insist on obfuscating their code to that degree,
and not all organizations can afford the upgrade. (You might be
able to upgrade without paying a penny to Microsoft, but it
still isn't free. No more than upgrading g++ or any other
compiler is free.)
The reason was that VC++ v6 is so non-standard, that you can't
infer anything from the fact that some code "works" when
produced by VC++ v6.
Nor with any other compiler, for that matter. I suppose you've
heard of undefined behavior.
Not that you would use a single compiler to see whether some
construct is legal, but v6 of VC++ is just awful.
Not awful: old. It's not the same thing. (VC++ 6.0 was one of
the better compilers around when it first appeared. G++ didn't
catch up until five or six years later.)

If you want to check legality using a compiler, of course, about
the only one which is any use at all for that would be Comeau.
Checking with several compilers can also be useful.
Now, your program is ill-formed because you changed the
declaration of the 'main' function.
I just checked the standard, and *IT* says that the code is
legal. FWIW: Sun CC 5.8 and g++ 4.1.0 agree as well, as does
VC++ 8. I'll admit that I didn't expect that, but of course,
::main is treated specially---Sun CC and g++, at least, always
treat it as if it were `extern "C"' (and I didn't check the
generated code this time, but in the past, more than one
compiler I've used generated a lot of extra code in the
function).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 7 '07 #7
On Dec 7, 2:56 pm, "Alf P. Steinbach" <al...@start.nowrote:
* James Kanze:
On Dec 6, 6:08 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
There was a proposal recently in comp.lang.c++.moderated to treat
all VC++ v6 related queries as off-topic.
I pretty much disagree with the current moderation trends in
comp.lang.c++.moderated---to the point of abandoning the group
completely (although I was one of the founders). But I don't
think they've gotten to the point of censuring because of the
version of the compiler you use.
It would be nice if you could be more clear about that
disagreement,
I thought we'd been through that before. It's not something
concrete that I can say: change that, and I'd be happy. It's
just a general feeling---I don't feel comfortable with the group
any more.

My first reaction was somewhat bitter: I am, after all, one of
the founders of the group, and had been one of the most
important contributors for the longest time. But when I
consider the issue objectively, it's normal that the group and
moderation policy evolve---and I've doubtlessly evolved as
well---and there's nothing surprising that my relationship with
the group evolve as well. If you look at the group, I don't
think that any of the original founders are particularly active
in it today, although in some cases, this is more for
professional reasons than any discomfort with the group.
and e.g. help the active moderators by explaining the
potential for improvement on the moderator's mailing list
instead of referring obliquely to it in [clc++].
I think my discomfort is well known. It should be rather
apparent, at least, from my participation, which went from the
most frequent poster to never posting rather abruptly.
The proposal Victor referred to was, IIRC, by Francis
Glassborow.
I'm not sure but I think he was also one of the founders of
the group.
Nope. He actually signed on rather late.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 9 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Shea Martin | last post by:
I am trying to use a system call which takes a function ptr. My compiler won't compile the code if I give the system_call a ptr to a class member function, A::func(). To combat this, I created an...
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(); }
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
11
by: Daniele Benegiamo | last post by:
Is the following program standard-compliant? I've compiled it with some compilers and no errors or warnings are produced, but this is not a demonstration. The "incriminated" part is the...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.