c function calls from c++

Discussion in 'C++' started by wojtek.fedorko@gmail.com, Apr 23, 2007.

  1. Guest

    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
    , Apr 23, 2007
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Apr 23, 2007
    #2
    1. Advertising

  3. Klaus Bahner Guest

    >
    > 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
    Klaus Bahner, Apr 23, 2007
    #3
  4. Guest

    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
    , Apr 24, 2007
    #4
  5. 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
    Victor Bazarov, Apr 24, 2007
    #5
  6. Mumia W. Guest

    On 04/24/2007 09:57 AM, 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.
    Mumia W., Apr 25, 2007
    #6
  7. Guest

    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
    , Apr 26, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Honne Gowda A
    Replies:
    2
    Views:
    872
    Karl Heinz Buchegger
    Oct 31, 2003
  2. andy6
    Replies:
    2
    Views:
    756
    andy6 via DotNetMonster.com
    Jun 9, 2006
  3. Replies:
    2
    Views:
    914
    Bengt Richter
    Aug 1, 2005
  4. Richard Tobin
    Replies:
    24
    Views:
    784
  5. Bob
    Replies:
    5
    Views:
    259
Loading...

Share This Page