Calling third party C library functions from C++

D

Dhillon

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
 
D

Dhillon

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* ...
//                         ^^^^^^^^^^






Read the FAQ 5.8 and follow its recommendations.



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

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);
.............................
}
 
J

James Kanze

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 ) ;

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

Dhillon

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 ) ;


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

--
James Kanze (GABI Software)             email:[email protected]
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.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top