Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling third party C library functions from C++

Reply
Thread Tools

Calling third party C library functions from C++

 
 
Dhillon
Guest
Posts: n/a
 
      12-15-2008
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
 
Reply With Quote
 
 
 
 
Dhillon
Guest
Posts: n/a
 
      12-15-2008
On Dec 15, 9:19*am, Victor Bazarov <(E-Mail Removed)> 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);
.............................
}

 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      12-16-2008
On Dec 15, 3:19 pm, Victor Bazarov <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
Dhillon
Guest
Posts: n/a
 
      12-17-2008
On Dec 16, 5:15*am, James Kanze <(E-Mail Removed)> wrote:
> On Dec 15, 3:19 pm, Victor Bazarov <(E-Mail Removed)> 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:(E-Mail Removed)
> 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.
 
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
can't find third-party C++ library Dave C++ 1 05-16-2005 08:15 AM
Third party C++ library offer system API? sam C++ 3 05-08-2005 03:39 PM
Third Party Import Library? cjones42 ASP .Net 1 06-25-2004 05:39 PM
Problems calling a third-party DLL using a thread Robert Birn Java 3 10-31-2003 02:15 PM
Strange error when calling third party assembly Mikael Engdahl ASP .Net 0 08-08-2003 12:02 AM



Advertisments