473,805 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5088
pa**********@at t.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**********@at t.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**********@at t.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**********@at t.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.l earn.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
3312
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 buffer into the character pointer. The code looks like the following: #include <stdio.h> #include <stdlib.h> #include "stdafx.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
3
4743
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 an error message saying "Cannot marshal parameter #2: Invalid managed/unmanaged type combination (this value type must be paired with Struct)" when calling "AceGetPinParams(iHandle,ref sd_pin)" function,here's my test code: private static...
0
2582
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 the Printer Properties form but I'm unable to write back the settings to my PrinterSettings It either crash with a null reference exception or my application freeze completly.
0
1070
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: TransparentBlt. But the call is failing (and the image is not drawn), with an error code of 126: "The specified module could not be found" (ERROR_MOD_NOT_FOUND).
2
3515
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 call is failing (and the image is not drawn), with an error code of 126: "The specified module could not be found" (ERROR_MOD_NOT_FOUND). (The bitmap is loaded from a separate file called "bmp.bmp", any bitmap renamed to that will do. I'm using...
1
1615
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 here is the header file: Code:
22
9264
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. I'm sure the problem is the way i'm using the sendmessage API, the return string and the lParam return 0....is anybody have a clue? any sendmessage api expert here? public static extern Int32 FindWindow(String lpClassName,String
9
2730
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 like this. ;;;; REGISTER THE VISION FACE RECOGNITION DLL ;;;;;;;;;;;;;;;;;;;;;;;;;;;; (fli:register-module "FaceRecog.dll") ; an interface function:
0
1054
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 progressing, I got some problems. Every time I quit from Python, I get a segmentation fault, although the main program runs well. I suspect it is because I wrapped std::vector objects in C struct and I did not release the memory properly. Below is the...
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10609
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10105
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9185
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5542
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.