Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > c function calls from c++

Reply
Thread Tools

c function calls from c++

 
 
wojtek.fedorko@gmail.com
Guest
Posts: n/a
 
      04-23-2007
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-23-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Klaus Bahner
Guest
Posts: n/a
 
      04-23-2007
>
> 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
> }
> -------------------------------------
>
> -So the prototype in the header doesn't match that in the .c file in
> terms of number of parameters.



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

 
Reply With Quote
 
wojtek.fedorko@gmail.com
Guest
Posts: n/a
 
      04-24-2007
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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-24-2007
(E-Mail Removed) wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      04-25-2007
On 04/24/2007 09:57 AM, (E-Mail Removed) wrote:
> 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.

 
Reply With Quote
 
wojtek.fedorko@gmail.com
Guest
Posts: n/a
 
      04-26-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
This function has an onClick event that calls a function that calls This function Bob Javascript 5 10-24-2006 04:11 PM
ods calls business object then method calls ta with output params andy6 ASP .Net 2 06-09-2006 01:54 AM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
MoVoIP - FREE MOBILE Inetrnet Phone Calls - FREE Internet Phone Calls ubifone VOIP 0 07-29-2005 04:31 PM
Sequence of constructor calls and destructor calls. Honne Gowda A C++ 2 10-31-2003 09:31 AM



Advertisments