473,395 Members | 1,403 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,395 software developers and data experts.

Help with Extern C

#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

Sep 12 '06 #1
7 5070
pa**********@att.net writes:
>#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.html

Sep 12 '06 #2
pa**********@att.net wrote:
#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?
Sep 12 '06 #3

Carlos Martinez wrote:
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

Sep 12 '06 #4
Carlos Martinez wrote:
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.

Sep 12 '06 #5
pa**********@att.net wrote:
#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.

Sep 12 '06 #6
On 12 Sep 2006 03:06:56 -0700, pa**********@att.net wrote in
comp.lang.c++:
#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.
Thank you very much for your help.

Paul Epstein
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 12 '06 #7

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

Sep 12 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
3
by: Webdiyer | last post by:
I want to integrate SecurID two-factor authentication system of the RSASecurity.inc into our asp.net application,but I get into trouble when trying to call the API functions of the ACE Agent,I got...
0
by: Benoit Courchesne | last post by:
Hi, I'm trying to build my custom Printer Dialog box. The following code is supposed to Pop up the printer properties form and edit the PrinterSettings. My problem is that I'm able to show...
0
by: Nick Jacobson | last post by:
Please help! The following code is used to draw a bitmap on a form with a color key (i.e. a color that's not drawn). For speed purposes, I'm trying to call the Win32 API function:...
2
by: Nick Jacobson | last post by:
The following code is used to draw a bitmap on a form with a color key (i.e. a color that's not drawn). For speed purposes, I'm trying to call the Win32 API function: TransparentBlt. But the...
1
by: llihp | last post by:
Hi all, I'm new to C#, so apologies if this is a dumb question. smile I'm trying to convert some code from C++ to C# so I can write some DLL files. Here is the original code in C++, firstly...
22
by: SQACSharp | last post by:
I'm trying to get the control name of an editbox in another window. The following code set the value "MyPassword" in the password EditBox but it fail to return the control name of the EditBox. ...
9
by: Ringo | last post by:
the LeafProject http://www.leafproject.org has a DLL for Face recognition. it is written in C++ but they interface to it from Lisp. I want to interface to it from C#. Their Lisp definitions looks...
0
by: RLC | last post by:
Hello I am new to python SWIG. Recently I wrote a small program trying to import collada files(using colladadom) into python so I could use python cgkit to render them. However, during the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.