Linkage Directives: extern "C"

U

usr2003

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.
 
M

Mike Wahler

usr2003 said:
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
 
K

Kevin Goodsell

usr2003 said:
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
 
U

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
 
U

usr2003

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top