By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,509 Members | 1,288 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,509 IT Pros & Developers. It's quick & easy.

Linkage Directives: extern "C"

P: n/a

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
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

usr2003 <me*********@dbforums.com> wrote in message
news:33****************@dbforums.com...

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

Jul 19 '05 #2

P: n/a
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.

Jul 19 '05 #3

P: n/a

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
Jul 19 '05 #4

P: n/a

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
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.