473,322 Members | 1,409 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.

Linkage Directives: extern "C"


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
4 3222

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

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

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

Similar topics

1
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
6
by: Otto Wyss | last post by:
I've the following function declaration: wxTree GetLastChild (const wxTree& item, long& cookie) const; I'd like to make the cookie parameter optional, i.e. "long& cookie = ....", without...
2
by: Eric Osman | last post by:
Hi, I'm looking for a javascript function that will convert input such as this: <CLUB Code=" into this: &lt;CLUB Code=&quot;
4
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following...
5
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot;...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
10
by: lovecreatesbeauty | last post by:
Is parameter type conversion required for the 2nd argument on printf("%p", (void *)&i); ? But one would never call memcpy like: memcpy((void *)pi, (void *)pj, sizeof *pj); /*memcpy((void *)pi,...
13
by: Ragnar | last post by:
Hi, 2 issues left with my tidy-work: 1) Tidy transforms a "&amp;" in the source-xml into a "&" in the tidied version. My XML-Importer cannot handle it 2) in a long <title>-string a wrap is...
2
by: sophia | last post by:
why scanf("%d. %d. %d",&d,&m,&y); is NOT reading the i/p 22 4 1972 correctly, whereas it is reading the i/p 22. 4. 1972 correctly ?. Is it because of the dot in the format string of scanf ?
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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
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.