Help with Extern C

P

pauldepstein

#include <iostream>

using namespace std;



int main()
{

extern "C" int f(int, int);

cout << f(2,3);
cin.get();
}

int f(int x, int y)
{return (x* 234);}

Using Dev C++ with Windows XP, the above code would not compile. The
error messages were: syntax error before string constant and f
undeclared.

This puzzles me somewhat as it seems quite a faithful rendering of the
extern C section of the C++ primer 4th edition.

What am I missing?

It is correct that this use of extern C is completely redundant because
the code has no features that are unique to C or unique to C++.
However, that doesn't seem a likely reason for the compiler to
complain.

The whole reason I got into this extern C business is that I wanted to
use the C facility of suspending type-checking for function parameters
using the (...) notation.

However, I couldn't get this to work. If someone can help me with
this, I would be grateful. C++ primer seems to imply that c++ already
supports this ellipsis notation but I didn't find it straightforward.

Thank you very much for your help.

Paul Epstein
 
C

Carlos Martinez

#include <iostream>

using namespace std;



int main()
{

extern "C" int f(int, int);

cout << f(2,3);
cin.get();
}

int f(int x, int y)
{return (x* 234);}

Using Dev C++ with Windows XP, the above code would not compile. The
error messages were: syntax error before string constant and f
undeclared.

This puzzles me somewhat as it seems quite a faithful rendering of the
extern C section of the C++ primer 4th edition.

What am I missing?

It is correct that this use of extern C is completely redundant because
the code has no features that are unique to C or unique to C++.
However, that doesn't seem a likely reason for the compiler to
complain.

The whole reason I got into this extern C business is that I wanted to
use the C facility of suspending type-checking for function parameters
using the (...) notation.

However, I couldn't get this to work. If someone can help me with
this, I would be grateful. C++ primer seems to imply that c++ already
supports this ellipsis notation but I didn't find it straightforward.

Thank you very much for your help.

Paul Epstein

Why do you put f inside main definition?
 
T

Tim Love

#include <iostream>
using namespace std;


int main()
{
extern "C" int f(int, int);
cout << f(2,3);
cin.get();
}
int f(int x, int y)
{return (x* 234);}
Using Dev C++ with Windows XP, the above code would not compile.
I think that if you put the code for f in a separate file and compile
it with a C compiler, you'll have more luck.
It is correct that this use of extern C is completely redundant because
the code has no features that are unique to C or unique to C++.
The issue is more to do with name-mangling. See the "C++ calling C"
section of
http://www-h.eng.cam.ac.uk/help/tpl/languages/mixinglanguages.html
 
P

pauldepstein

Carlos said:
Why do you put f inside main definition?

Carlos, I don't understand your question at all.

Clearly, my posted code doesn't work.

However, without using extern "C" it works very well.

When you say that I "put" f inside the main definition, what do you
mean by "put"? I was declaring the function f.

Paul Epstein
 
B

Bart

Carlos said:
Why do you put f inside main definition?

It's perfectly legal to declare function prototypes locally, although
it's less common than simply declaring them at the global scope.

Regards,
Bart.
 
B

Bart

#include <iostream>
using namespace std;
int main()
{
extern "C" int f(int, int);
cout << f(2,3);
cin.get();
}

int f(int x, int y)
{return (x* 234);}

Using Dev C++ with Windows XP, the above code would not compile. The
error messages were: syntax error before string constant and f
undeclared.

This puzzles me somewhat as it seems quite a faithful rendering of the
extern C section of the C++ primer 4th edition.

What am I missing?

Write it as:

extern "C"
{
int f(int, int);
}

at the global scope.

Regards,
Bart.
 
J

Jack Klein

#include <iostream>

using namespace std;



int main()
{

extern "C" int f(int, int);

cout << f(2,3);
cin.get();
}

int f(int x, int y)
{return (x* 234);}

Using Dev C++ with Windows XP, the above code would not compile. The
error messages were: syntax error before string constant and f
undeclared.

This puzzles me somewhat as it seems quite a faithful rendering of the
extern C section of the C++ primer 4th edition.

What am I missing?

What you are missing is the fact that your prototype and your function
definition do not match. Your prototype inside main is for a file
named 'f' with C linkage, your definition after main is for a file
named 'f' with C++ linkage, because you did not put extern "C" around
the definition. These are the declarations of two different
overloaded functions with the same name.

C++ allows for a set of overloaded functions to include exactly one
with C linkage, although it can have any number with C++ linkage.

So your code in main() is trying to call a function described by the
declaration in scope at the time, not another overloaded function of
the same name.

This is no different than if you removed the extern "C" from the
declaration inside main, but changed the function definition to:

int f (double x, double y);

The call would still fail, because you are not calling the f() that
you actually defined, but a different overloaded function with the
same name, that you have not supplied.
It is correct that this use of extern C is completely redundant because
the code has no features that are unique to C or unique to C++.
However, that doesn't seem a likely reason for the compiler to
complain.

I am not sure, and I am too lazy to look up, whether C++ actually
allows two overloaded functions to have exactly the same argument list
just because one of them has C linkage. I don't think so, as the call
would be ambiguous, but I might be wrong.
The whole reason I got into this extern C business is that I wanted to
use the C facility of suspending type-checking for function parameters
using the (...) notation.

The ellipsis for functions for variable arguments lists works EXACTLY
the same in C++ as it does in C. The limitation is that you can only
pass POD (Plain Old Data) types as the variable arguments.
However, I couldn't get this to work. If someone can help me with
this, I would be grateful. C++ primer seems to imply that c++ already
supports this ellipsis notation but I didn't find it straightforward.

If what you are really trying to do is to create and call a variadic
function in C++, then post the code you can't get to work for doing
that, and ask for help.
 
P

pauldepstein

Jack Klein wrote:
....
What you are missing is the fact that your prototype and your function
definition do not match. Your prototype inside main is for a file
named 'f' with C linkage, your definition after main is for a file
named 'f' with C++ linkage, because you did not put extern "C" around
the definition. These are the declarations of two different
overloaded functions with the same name.
....

Thanks. So it seems I should have used extern "C" twice and I
understand why. However, I couldn't get this to work. I made various
attempts to write extern "C" during the function definition and always
got a compilation error or linker error.

Could you be a bit more precise as to where to put the 2nd extern "C"
to get it to work (or direct me to a website which explains it.)

Thanks,

Paul Epstein
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top