Mail Archives: djgpp/1999/01/11/17:26:33
From: | Laurence Withers <lwithers AT lwithers DOT demon DOT co DOT uk>
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Re: Calling C++ functions from C functions: how?
|
Date: | Mon, 11 Jan 1999 19:40:58 +0000
|
Organization: | IP
|
Message-ID: | <yeLbzQAKPlm2EAXL@lwithers.demon.co.uk>
|
References: | <77boug$l58$1 AT samba DOT rahul DOT net>
|
NNTP-Posting-Host: | lwithers.demon.co.uk
|
X-NNTP-Posting-Host: | lwithers.demon.co.uk:194.222.80.1
|
X-Trace: | news.demon.co.uk 916093476 nnrp-11:10397 NO-IDENT lwithers.demon.co.uk:194.222.80.1
|
X-Complaints-To: | abuse AT demon DOT net
|
MIME-Version: | 1.0
|
X-Newsreader: | Turnpike (32) Version 4.00 <WCcCG$mmNL32sYSf46vBeC80CR>
|
Lines: | 80
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Reply-To: | djgpp AT delorie DOT com
|
--- Original Message ---
From: to comp.os.msdos.djgpp <axlq AT unicorn DOT us DOT com>
Time: Mon, 11 Jan 1999, 02:50:56
>I have a library that contains several C modules and two C++ modules.
>The C modules end in .c and the C++ modules end in .cc. I'm using RHIDE
>to build everything. No special compiler options.
>
>I also have a test program in C that calls these functions.
>
>When I attempt to link my program to the library, the linker complains
>that two functions called by the test program are undefined. These two
>functions happen to be the C++ ones in the library.
[snip]
As Eli has said, this occurs due to name mangling. One possible solution
to this is to provide C wrapper functions for anything in C++. For
example:
// obj.cc //////////////////////////////////////////////////////////////
// C++ function
int cplusplus_tester_function(int a, int b)
{
return (a + b);
}
//
// C wrapper
//
extern "C" {
int c_tester_function(int a, int b)
{
return cplusplus_tester_function(a, b);
}
}
// end of obj.cc ///////////////////////////////////////////////////////
// main.c //////////////////////////////////////////////////////////////
#include <stdio.h>
int c_tester_function(int, int);
int main(void)
{
int a = 2, b = 3;
printf("Adding %d and %d gives: %d\n", a, b,
c_tester_function(a, b));
return 0;
}
// end of main.c ///////////////////////////////////////////////////////
Now compile and run:
19:30:49.54-> gxx -o obj.o -c obj.cc
19:36:10.58-> ar rs obj.a obj.o
19:36:15.02-> gcc -o main.o -c main.c
19:36:59.62-> gcc -o test.exe main.o obj.a
19:37:07.48-> test
Adding 2 and 3 gives: 5
As you can see, the C wrapper function has to be surrounded by an
extern "C" { /* ... */ } clause.
This might seem inefficient, but if you compile with the
-finline-functions optimisation, I believe the call will be optimised
away (not sure though), effectively solving your problem.
Well, I hope this helps you. Bye for now,
--
Laurence Withers, mailto:lwithers AT lwithers DOT demon DOT co DOT uk
Integrated Peripherals Operating System Project Leader || OPES Project
Projects' homepage is at: http://www.lwithers.demon.co.uk/ Leader
- Raw text -