Calling third party C library functions from C++

Discussion in 'C++' started by Dhillon, Dec 15, 2008.

  1. Dhillon

    Dhillon Guest

    Hello All,

    I have searched internet and also posts on this group. None of the
    suggestions have worked in my case.
    I am having linker issues when I try to use a C funtion in C++ code.


    Though I have declared that C function as extern "C" c_func(......)
    in my .cpp file.

    Also this c_func takes argument as (void*, void (cpp_func*)(int,
    void*, unsigned long), void*)

    Here cpp_func is a pointer to a function in my .cpp file(not a member
    function) where I am calling this c_func

    To avoid name mangling by C++ compiler for the function to which
    cpp_func points to I have also declared this

    functions as extren "C" cpp_function_pointed_to (....) in my .h file
    so that c_func is able to use this callback function which is defined
    in my C++ file.

    Please correct me where I am going wrong.

    Despite doing all this, When I try to build my project I fail at
    linker stage where linker complians about undefined funtion c_func
    referred from function_pointed_by defined in my .cpp file

    I am using CodeWarrior IDE for my development.

    Thanks in advance,
    MD
     
    Dhillon, Dec 15, 2008
    #1
    1. Advertising

  2. Dhillon

    Dhillon Guest

    On Dec 15, 9:19 am, Victor Bazarov <> wrote:
    > Dhillon wrote:
    > > I have searched internet and also posts on this group. None of the
    > > suggestions have worked in my case.
    > > I am having linker issues when I try to use a C funtion in C++ code.

    >
    > > Though I have declared that C function as  extern "C" c_func(......)
    > > in my .cpp file.

    >
    > > Also this c_func takes argument as (void*, void (cpp_func*)(int,
    > > void*, unsigned long), void*)

    >
    > This looks like a declaration of the arguments.  If that's so, then your
    > "cpp_func" has to be a part of the declaration (since it precedes the
    > asterisk), like a modifier or a specifier.  Is it?  If it isn't, do not
    > put it here.  Just say that your 'c_func' has the following declaration:
    >
    > extern "C" <return_value_type> c_func(void*, void (*)(int,void*,unsigned
    > long), void*);
    >
    > If that's so, then I suspect you might need to add something in front of
    > the asterisk in the parentheses like so:
    >
    >     ... c_func(void*, void (extern "C" *)(int,void* ...
    > //                         ^^^^^^^^^^
    >
    >
    >
    > > Here cpp_func is a pointer to a function in my .cpp file(not a member
    > > function) where I am calling this c_func

    >
    > Huh?
    >
    > > To avoid name mangling by C++ compiler for the function to which
    > > cpp_func points to I have also declared this

    >
    > > functions as extren "C"  cpp_function_pointed_to (....) in my .h file
    > > so that c_func is able to use this callback function which is defined
    > > in my C++ file.

    >
    > > Please correct me where I am going wrong.

    >
    > Read the FAQ 5.8 and follow its recommendations.
    >
    > > Despite doing all this, When I try to build my project I fail at
    > > linker stage where linker complians about undefined funtion c_func
    > > referred from function_pointed_by defined in my .cpp file

    >
    > > I am using CodeWarrior IDE for my development.

    >
    > That's only relevant if you need help with your IDE (which you can't
    > really get here, since this is a tool-neutral newsgroup).
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Hi,
    Thanks for your reply.
    I am using c/C== compilar from Codewarrior IDE version 4.2.7
    Searched the whole user guide but they have not specified the compilar
    version number being used.

    Following may explain more what I am trying to do.

    -----------------------------------------------------------------------
    my_cpp_file_1.h
    -----------------------------------------------------------------------

    extern "C" func_pointed_to (
    uint_32 node,
    uint_32 node_mask,
    void* data,
    void* data_mask
    );


    -----------------------------------------------------------------------
    my_cpp_file_1.cpp
    -----------------------------------------------------------------------
    #include my_cpp_file_1.h

    extern "C" void c_func(
    void* root,
    uint_32 (* cpp_func)(uint_32, uint_32, void*, void*),
    void* data
    );

    uint_3 func_pointed_to (
    uint_32 node,
    uint_32 node_mask,
    void* data,
    void* data_mask
    )
    {
    ......................
    ........................
    return false;

    } /* Endbody */

    uint_32 my_cpp_func(
    uint_32 node,
    uint_32 node_mask,
    void* data,
    void* data_mask
    )
    {
    ..........................
    ............................

    c_func(pointer_1, &func_pointed_to, pointer_2);
    .............................
    }
     
    Dhillon, Dec 15, 2008
    #2
    1. Advertising

  3. Dhillon

    James Kanze Guest

    On Dec 15, 3:19 pm, Victor Bazarov <> wrote:
    > Dhillon wrote:
    > > I have searched internet and also posts on this group. None
    > > of the suggestions have worked in my case.
    > > I am having linker issues when I try to use a C funtion in
    > > C++ code.


    > > Though I have declared that C function as extern "C" c_func(......)
    > > in my .cpp file.


    > > Also this c_func takes argument as (void*, void
    > > (cpp_func*)(int, void*, unsigned long), void*)


    > This looks like a declaration of the arguments. If that's so,
    > then your "cpp_func" has to be a part of the declaration
    > (since it precedes the asterisk), like a modifier or a
    > specifier. Is it? If it isn't, do not put it here. Just say
    > that your 'c_func' has the following declaration:


    > extern "C" <return_value_type> c_func(void*, void
    > (*)(int,void*,unsigned long), void*);


    > If that's so, then I suspect you might need to add something
    > in front of the asterisk in the parentheses like so:


    > ... c_func(void*, void (extern "C" *)(int,void* ...
    > // ^^^^^^^^^^


    The ``extern "C"'' is valid for the entire declaration,
    including the function pointer argument; ``extern "C"'' where
    you want to put it isn't legal.

    If you need to declare a pointer to a function argument as
    ``extern "C"'', without declaring the function ``extern "C"'',
    you have to use a typedef:

    extern "C" { typedef void (*PtrToCFnc)() ; }
    void f( PtrToCFnc ) ;

    > > Here cpp_func is a pointer to a function in my .cpp file(not
    > > a member function) where I am calling this c_func


    > Huh?


    He lost me too. A small example would doubtlessly clarify
    things.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Dec 16, 2008
    #3
  4. Dhillon

    Dhillon Guest

    On Dec 16, 5:15 am, James Kanze <> wrote:
    > On Dec 15, 3:19 pm, Victor Bazarov <> wrote:
    >
    >
    >
    >
    >
    > > Dhillon wrote:
    > > > I have searched internet and also posts on this group. None
    > > > of the suggestions have worked in my case.
    > > > I am having linker issues when I try to use a C funtion in
    > > > C++ code.
    > > > Though I have declared that C function as  extern "C" c_func(......)
    > > > in my .cpp file.
    > > > Also this c_func takes argument as (void*, void
    > > > (cpp_func*)(int, void*, unsigned long), void*)

    > > This looks like a declaration of the arguments.  If that's so,
    > > then your "cpp_func" has to be a part of the declaration
    > > (since it precedes the asterisk), like a modifier or a
    > > specifier.  Is it?  If it isn't, do not put it here.  Just say
    > > that your 'c_func' has the following declaration:
    > > extern "C" <return_value_type> c_func(void*, void
    > > (*)(int,void*,unsigned long), void*);
    > > If that's so, then I suspect you might need to add something
    > > in front of the asterisk in the parentheses like so:
    > >     ... c_func(void*, void (extern "C" *)(int,void* ...
    > > //                         ^^^^^^^^^^

    >
    > The ``extern "C"'' is valid for the entire declaration,
    > including the function pointer argument; ``extern "C"'' where
    > you want to put it isn't legal.
    >
    > If you need to declare a pointer to a function argument as
    > ``extern "C"'', without declaring the function ``extern "C"'',
    > you have to use a typedef:
    >
    >     extern "C" { typedef void (*PtrToCFnc)() ; }
    >     void f( PtrToCFnc ) ;
    >
    > > > Here cpp_func is a pointer to a function in my .cpp file(not
    > > > a member function) where I am calling this c_func

    > > Huh?

    >
    > He lost me too.  A small example would doubtlessly clarify
    > things.
    >
    > --
    > James Kanze (GABI Software)             email:
    > Conseils en informatique orientée objet/
    >                    Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34- Hide quoted text -
    >
    > - Show quoted text -


    Thanks for your comments. Victor's second post cleared all the doubts.
    I am able to link the code now.
    Appreciate your input.
     
    Dhillon, Dec 17, 2008
    #4
    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. Mikael Engdahl

    Strange error when calling third party assembly

    Mikael Engdahl, Aug 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    369
    Mikael Engdahl
    Aug 8, 2003
  2. cjones42

    Third Party Import Library?

    cjones42, Jun 25, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    331
    Mythran
    Jun 25, 2004
  3. Robert Birn
    Replies:
    3
    Views:
    804
    Robert Birn
    Oct 31, 2003
  4. sam
    Replies:
    3
    Views:
    401
    Abecedarian
    May 8, 2005
  5. Dave
    Replies:
    1
    Views:
    467
    Rapscallion
    May 16, 2005
Loading...

Share This Page