Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Linkage Directives: extern "C"

Reply
Thread Tools

Linkage Directives: extern "C"

 
 
usr2003
Guest
Posts: n/a
 
      09-17-2003

I wrote the following test program to test the linkage directives
extern "C":



#include <stdio.h>



extern "C" {

void f();

void callingFWithParm();

}



void f()

{

printf("Function f()\n");

}



void callingFWithParm()

{

int i=0;

f(i);

}



int

main()

{

callingFWithParm();

}



I compiled with g++ on Red Hat Linux 9. But I got the following error:



"mix.cc: In function `void callingFWithParm()':

mix.cc:9: too many arguments to function `void f()'

mix.cc:16: at this point in file"



It's as if the linkage directive didn't work. If I take out the linkage
directive and compile with gcc, it works fine.



Could anybody explain this behavior? Any input would be greatly
appreciated.


--
Posted via http://dbforums.com
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      09-17-2003

usr2003 <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> I wrote the following test program to test the linkage directives
> extern "C":
>
>
>
> #include <stdio.h>
>
>
>
> extern "C" {
>
> void f();
>
> void callingFWithParm();
>
> }


Move the above line

>
>
>
> void f()
>
> {
>
> printf("Function f()\n");
>
> }


To here.

-Mike



 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-17-2003
usr2003 wrote:

> I wrote the following test program to test the linkage directives
> extern "C":
>
> #include <stdio.h>
>
> extern "C" {
>
> void f();


f takes no arguments.

>
> void callingFWithParm();
> }
>
> void f()
> {
> printf("Function f()\n");
> }
>
> void callingFWithParm()
> {
> int i=0;
> f(i);


You call f with an argument.

> }
>
> int
> main()
> {
> callingFWithParm();
> }
>
> I compiled with g++ on Red Hat Linux 9. But I got the following error:
>
> "mix.cc: In function `void callingFWithParm()':
>
> mix.cc:9: too many arguments to function `void f()'


This is exactly the error I would expect if I were to attempt to call a
function that takes 0 arguments and pass it 1 argument.

>
> mix.cc:16: at this point in file"
>
> It's as if the linkage directive didn't work. If I take out the linkage
> directive and compile with gcc, it works fine.


First, there's nothing here presenting any evidence that the linkage
directive did or did not work. Second, the error still exists when you
remove the linkage directive, so unless you've done something else
differently (such as compiled in C mode, in which case it may compile
but the behavior is undefined, IIRC) you should get the same error.

>
> Could anybody explain this behavior? Any input would be greatly
> appreciated.


extern "C" changes linkage only. You can't put C code inside the extern
"C" block - it's still C++. An empty parameter list still means "no
arguments", as it always does in C++. Having it inside extern "C" does
not give it the C semantics.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
usr2003
Guest
Posts: n/a
 
      09-19-2003

Thanks Kevin. Your explanation makes sense. I was trying to come up with
a simple example to see how the linkage directive works. I wanted to set
up a scenario where the program would only work correctly with the
extern "C" linkage directive; in another word, it wouldn't work without.
Obviously, my example didn't do what I intended to do.



I still don't know when to use the linkage directive. If I compile the c
code with a C compiler to get the .o file, then I don't need the linkage
directive (Well, at least in my example).



Any comment on when linkage directive should be used? Any example?



Thanks,

JF


--
Posted via http://dbforums.com
 
Reply With Quote
 
usr2003
Guest
Posts: n/a
 
      09-19-2003

My bad. It does require the extern "C" linkage directive when I try to
use the .o compiled by a C compile. Now it all makes sense to me.



Thanks again!



Originally posted by usr2003

> Thanks Kevin. Your explanation makes sense. I was trying to come up
> with a simple example to see how the linkage directive works. I wanted
> to set up a scenario where the program would only work correctly with
> the extern "C" linkage directive; in another word, it wouldn't work
> without. Obviously, my example didn't do what I intended to do.


>


> I still don't know when to use the linkage directive. If I compile the
> c code with a C compiler to get the .o file, then I don't need the
> linkage directive (Well, at least in my example).


>


> Any comment on when linkage directive should be used? Any example?


>


> Thanks,


JF


--
Posted via http://dbforums.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ linkage vs c linkage ramasubramanian.rahul@gmail.com C++ 1 09-12-2008 11:41 AM
Questions about extern "C" linkage directive mimi C++ 4 03-23-2007 05:14 AM
Static linkage and extern "C" Mark A. Gibbs C++ 10 10-21-2005 06:06 AM
why can't a template function have extern "C" linkage? Ian C++ 22 07-29-2005 03:33 AM
extern and linkage Steve Kobes C Programming 1 06-09-2004 05:15 PM



Advertisments