473,480 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ code with C-style interface for a library to be used in C++ and C?

Hi!

I have a question about building and then using libraries containing
C++ code.

Let's say I have some C++ code and a .cpp file with 1 function that
uses some other C++ code / classes etc... Also, any possible exception
is handled within the function itself.

In code:

MyModule.h
----------
#ifndef MYMODULE_H
#define MYMODULE_H

extern int Test(float inParam1,float inParam2,float* outResult);

#endif // #ifndef MYMODULE_H

MyModule.cpp
------------
#include "MyModule.h"
#include "MyClasses.h" // contains MyClassA and MyClassB

int Test(float inParam1,float inParam2,float* outResult)
{
int theResult = 0;
try
{
MyClassA a;
a.Setup(inParam1);
MyClassB b;
b.Setup(inParam2);
*outResult = a.Process(b);
}
catch (...)
{
theResult = -1;
}
return theResult;
}

Now, I would like to build a library that is callable from C++ AND
from C that exposes the functionality of that Test function (I just
gave an example with 1 single function, but in practice there are
more).

Currently, when I build the library, it is compiled using the C++
compiler (of course, since I really use C++ classes and so on), and I
can use the library from a C++ program (.cpp file with main), as
should...

But I can't seem to find out how to make that function accessible to a
C program (.c file with main). I get an error like "unresolved
external symbol _Test", and I know that it probably has something to
do with the name mangling in C++ being different from C...
Considering the fact that the *interface* of the library does not
contain any C++ specific things, and handles all possible exceptions
internally, it should be possible to use it in C too, right?

Can someone please explain me how to do that?
Thanks in advance!

Koen

PS
If someone wants the test code, I can post/send them on request.
Jul 19 '05 #1
5 3351
"Koen" <no@ssppaamm.com> wrote in message
news:bk**********@gaudi2.UGent.be...
Hi!

I have a question about building and then using libraries containing
C++ code.

Let's say I have some C++ code and a .cpp file with 1 function that
uses some other C++ code / classes etc... Also, any possible exception is handled within the function itself.

In code:

MyModule.h
----------
#ifndef MYMODULE_H
#define MYMODULE_H

extern int Test(float inParam1,float inParam2,float* outResult);

#endif // #ifndef MYMODULE_H


OK. Seems like all I needed to do was this:

#ifndef MYMODULE_H
#define MYMODULE_H

#ifdef __cplusplus
extern "C" {
#endif

int Test(float inParam1,float inParam2,float* outResult);

#ifdef __cplusplus
}
#endif

#endif // #ifndef MYMODULE_H
Only thing I'm not sure of anymore is whether I should still keep the
"extern" in front of my function (so that also in the C case there is
an "extern"):

extern int Test(float inParam1,float inParam2,float* outResult);

Does that still have any use?

Koen
Jul 19 '05 #2
Koen escribió:
But I can't seem to find out how to make that function accessible to a
C program (.c file with main). I get an error like "unresolved
external symbol _Test", and I know that it probably has something to
do with the name mangling in C++ being different from C...


Declare your function as extern "C".

Regards.
Jul 19 '05 #3

"Koen" <no@ssppaamm.com> wrote in message
news:bk**********@gaudi2.UGent.be...
"Koen" <no@ssppaamm.com> wrote in message
news:bk**********@gaudi2.UGent.be...
Hi!

I have a question about building and then using libraries containing
C++ code.

Let's say I have some C++ code and a .cpp file with 1 function that
uses some other C++ code / classes etc... Also, any possible exception
is handled within the function itself.

In code:

MyModule.h
----------
#ifndef MYMODULE_H
#define MYMODULE_H

extern int Test(float inParam1,float inParam2,float* outResult);

#endif // #ifndef MYMODULE_H


OK. Seems like all I needed to do was this:

#ifndef MYMODULE_H
#define MYMODULE_H

#ifdef __cplusplus
extern "C" {
#endif

int Test(float inParam1,float inParam2,float* outResult);

#ifdef __cplusplus
}
#endif

#endif // #ifndef MYMODULE_H
Only thing I'm not sure of anymore is whether I should still keep the
"extern" in front of my function (so that also in the C case there is
an "extern"):

extern int Test(float inParam1,float inParam2,float* outResult);


In both C and C++, functions are 'extern' by default,
and need not be qualified as such.

The use of 'extern' with 'extern "C"' has a special
purpose meaning, used for the interlanguage interface.

Does that still have any use?


No, it never did. :-)

-Mike
Jul 19 '05 #4
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:fh*****************@newsread3.news.pas.earthl ink.net...
In both C and C++, functions are 'extern' by default,
and need not be qualified as such.

The use of 'extern' with 'extern "C"' has a special
purpose meaning, used for the interlanguage interface.

Does that still have any use?


No, it never did. :-)


OK. Thanks for the info!
Koen
Jul 19 '05 #5
On Fri, 19 Sep 2003 15:49:33 +0200, "Koen" <no@ssppaamm.com> wrote in
comp.lang.c++:
Hi!

I have a question about building and then using libraries containing
C++ code.

Let's say I have some C++ code and a .cpp file with 1 function that
uses some other C++ code / classes etc... Also, any possible exception
is handled within the function itself.

In code:

MyModule.h
----------
#ifndef MYMODULE_H
#define MYMODULE_H

extern int Test(float inParam1,float inParam2,float* outResult);

#endif // #ifndef MYMODULE_H

MyModule.cpp
------------
#include "MyModule.h"
#include "MyClasses.h" // contains MyClassA and MyClassB

int Test(float inParam1,float inParam2,float* outResult)
{
int theResult = 0;
try
{
MyClassA a;
a.Setup(inParam1);
MyClassB b;
b.Setup(inParam2);
*outResult = a.Process(b);
}
catch (...)
{
theResult = -1;
}
return theResult;
}

Now, I would like to build a library that is callable from C++ AND
from C that exposes the functionality of that Test function (I just
gave an example with 1 single function, but in practice there are
more).

Currently, when I build the library, it is compiled using the C++
compiler (of course, since I really use C++ classes and so on), and I
can use the library from a C++ program (.cpp file with main), as
should...

But I can't seem to find out how to make that function accessible to a
C program (.c file with main). I get an error like "unresolved
external symbol _Test", and I know that it probably has something to
do with the name mangling in C++ being different from C...
Considering the fact that the *interface* of the library does not
contain any C++ specific things, and handles all possible exceptions
internally, it should be possible to use it in C too, right?

Can someone please explain me how to do that?
Thanks in advance!

Koen

PS
If someone wants the test code, I can post/send them on request.


You probably can't use C++ code that does things like throwing and
catching exceptions from inside a C program.

While this is implementation specific, it is often necessary on many
platforms for a program containing mixed C and C++ object modules to
have the start-up and main() in C++. It is quite possible that the C
environment created by the C compiler for a C executable will not have
appropriate support for C++ only features such as exceptions.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #6

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

Similar topics

699
33295
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
9
2009
by: Dan Perl | last post by:
Is there a mechanism or an idiom for adding code for debugging so that it can easily be removed in the production code? I am thinking of something similar to the C/C++ preprocessor statements with...
1
2865
by: Brian Beck | last post by:
Hi. I'm having some problems with code based directly on the following httplib documentation code: http://www.zvon.org/other/python/doc21/lib/httplib-examples.html I've included the code and...
5
1887
by: Sky Fly | last post by:
Hi, I know that when an .NET exe is run, the CLR loads the exe (along with dependent assemblies), compiles them to native code then runs the code. Assuming the assemblies are loaded from a...
16
3082
by: Dario de Judicibus | last post by:
I'm getting crazy. Look at this code: #include <string.h> #include <stdio.h> #include <iostream.h> using namespace std ; char ini_code = {0xFF, 0xFE} ; char line_sep = {0x20, 0x28} ;
5
4033
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each...
37
5912
by: Alan Silver | last post by:
Hello, Newbie here, so please forgive what is probably a basic question ... I see a lot of discussion about "code behind", which if I have understood correctly, means that the script code goes...
171
7577
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
4
2087
by: KenFehling | last post by:
Hello. I am wondering if there exists a piece of software that takes multiple .js files that are nicely indented and commented and create one big tightly packed .js file. I'm hoping the one file...
6
3111
by: Fuzzyman | last post by:
Hello all, I'm trying to extract the code object from a function, and exec it without explicitly passing parameters. The code object 'knows' it expects to receive paramaters. It's 'arg_count'...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7043
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,...
1
6737
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6921
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...
0
5336
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,...
1
4776
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...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
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...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.