c function calls from c++

W

wojtek.fedorko

Hi,
I have a 3rd party c library which has some functions I'd like to use
inside my c++ prorgam.
I have the source code for this library but would not like to touch it
if possible.

the c library has files that look something like this:

header.h:
extern void foo();
--------------------------------------

lib.c:

#include "header.h"

void foo(a,b)
int a; // - btw I didn't know you could 'type'
your variables like this in c...
int b;
{
//do stuff
}
-------------------------------------

-So the prototype in the header doesn't match that in the .c file in
terms of number of parameters.


I'm writing my program like this:

extern "C" {
#include "header.h"
}

int main(){
int a,b;
foo(a,b);
}

This does not compile. The compiler complains that foo is called with
wrong number of parameters.

If write I something like this like this instead:


extern "C" {
#include "header.h"
}

extern "C" {
void foo(int,int);
}

int main(){
int a,b;
foo(a,b);
}

compiler complains about multiple definitions of foo.

For now I worked around this by editing header.h like this:
extern void foo(int,int)
but I would really like to avoid this as I don't control the library
code...

thanks a lot,
Wojtek
 
V

Victor Bazarov

I have a 3rd party c library which has some functions I'd like to use
inside my c++ prorgam.
I have the source code for this library but would not like to touch it
if possible.

the c library has files that look something like this:

header.h:
extern void foo();

In C-speak it means a function that takes any arguments and returns
nothing.
--------------------------------------

lib.c:

#include "header.h"

void foo(a,b)
int a; // - btw I didn't know you could 'type'
your variables like this in c...

That's pre-standard way of declaring the arguments. See the early
editions of K&R.
int b;
{
//do stuff
}
-------------------------------------

-So the prototype in the header doesn't match that in the .c file in
terms of number of parameters.


I'm writing my program like this:

extern "C" {
#include "header.h"
}

int main(){
int a,b;
foo(a,b);
}

This does not compile. The compiler complains that foo is called with
wrong number of parameters.

Yep. It's a problem.
If write I something like this like this instead:


extern "C" {
#include "header.h"
}

extern "C" {
void foo(int,int);
}

int main(){
int a,b;
foo(a,b);
}

compiler complains about multiple definitions of foo.

It is completely within its rights to do so.
For now I worked around this by editing header.h like this:
extern void foo(int,int)
but I would really like to avoid this as I don't control the library
code...

You don't have to change 'header.h'. You just need to copy them
all into new headers and change those.

There is a very, very slim chance that you would _ever_ need to
do that again. If you find an updated version of the library,
it's more likely to contain something more suitable for re-use.

Another way of dealing with that is to have your own C library
which would serve as a proxy or a buffer for this one. Make it
with a properly formatted header. Let every function you expose
to your C++ code call the respective function in the library you
are wrapping. Name your library functions differently (a prefix
or a postfix would do).

V
 
K

Klaus Bahner

void foo(a,b)
int a; // - btw I didn't know you could 'type'
your variables like this in c...
int b;
{


Old K&R style.
//do stuff
}


IIRC legal according to K&R C.

For now I worked around this by editing header.h like this:
extern void foo(int,int)
but I would really like to avoid this as I don't control the library
code...

If you don't want to touch the source code, you have to invoke a C
compiler instead of a C++ compiler. If, for example, you were using
MSVC++ you have to use *.c instead of *.cpp file extension for your
library files. It then compiles without errors. I'm not totally sure
about it, but I don't know of any way to convince an ANSI C++ compiler
to eat that old style.

HTH
Klaus
 
W

wojtek.fedorko

Thanks for the responses.
The second option (compiling in c) won't work since I actually need c+
+ (the example prog I wrote here is an oversimplification- it would
compile in c but my real stuff needs c++)
The first option (writing my own header.h) is what I think I'll have
to do. It's kind of a pain because the actual code structure is much
more complicated - header.h is really included in a chain of headers
which contain a bunch of defines typedefs etc which are needed by my
prog - so essentially I'll have to duplicate all that include
structure but now with prototypes that have proper arguments - yuck.
And if there's an update to the library I'll have to redo this - yuck+
+.
again, thanks for the responses
Wojtek
 
V

Victor Bazarov

Thanks for the responses.
The second option (compiling in c) won't work since I actually need c+
+ (the example prog I wrote here is an oversimplification- it would
compile in c but my real stuff needs c++)
The first option (writing my own header.h) is what I think I'll have
to do. It's kind of a pain because the actual code structure is much
more complicated - header.h is really included in a chain of headers
which contain a bunch of defines typedefs etc which are needed by my
prog - so essentially I'll have to duplicate all that include
structure but now with prototypes that have proper arguments - yuck.
And if there's an update to the library I'll have to redo this - yuck+
+.
again, thanks for the responses

From what you shared with us, the library you are adapting is more than
15 years old. Are you sure there is nothing on the market (paid or
free of charge) that doesn't do what you need? Perhaps your time is
better spent looking for a currently supported solution and working on
interfacing with that instead...

V
 
M

Mumia W.

Thanks for the responses.
The second option (compiling in c) won't work since I actually need c+
+ (the example prog I wrote here is an oversimplification- it would
compile in c but my real stuff needs c++)
The first option (writing my own header.h) is what I think I'll have
to do. It's kind of a pain because the actual code structure is much
more complicated - header.h is really included in a chain of headers
which contain a bunch of defines typedefs etc which are needed by my
prog - so essentially I'll have to duplicate all that include
structure but now with prototypes that have proper arguments - yuck.
And if there's an update to the library I'll have to redo this - yuck+
+.
again, thanks for the responses
Wojtek

I've never used this program, but "protoize" might be able to help you.
Do an Internet search for it.
 
W

wojtek.fedorko

Hi,
The library I'm using was released in Nov 2006. gcc builds it with no
complaints (although I probably could make it complain using
apropriate options)
This protoize looks promising - I'll try this and post the results
thanks,
Wojtek
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top