Re: call c++ function from c




Marcin Wolcendorf <wolcendo@xxxxxxxxxxxxxx> wrote in message
news:f9ho4k$lce$1@xxxxxxxxxxxxxxxxxxxxxx
falk.von.roetel@xxxxxx wrote:

I have the problem to call a c++ funtion in a C project. I have a
header file from the cpp file. In the header file the function is
decleared "int GetHostIPAddresses(ini i);". I inlcude the header file
in the c file and call the function GetHostIPAddresses(1);. But the
linker doesn't found the function. Can someone give me a hint what is
wrong.

Well, other explaind why it doesn't work pretty well, so I'll skip this
part.
If you want to use C++ function in C and you don't want to change C++
code, add a wrapper layer. Decalre function in C++ that calls your
intended function- it should look something like this:

foo() - your C++ function.

In .cpp file:
extern "C" foo_for_C()
{ return foo()}

In .h file:
#ifdef __cplusplus
extern "C" {
#endif
foo_for_c();

#ifdef __cplusplus
}
#endif

Well, looky there, an actual correct answer in this newsgroup. This
question has come up several times here, and certain people have once
again chosen to not only be presumably off-topic but just completely
wrong as well. They were affirmatively corrected several times
before, but apparently they are uneducable.

And I'm quite sure, quite worthless as programmers or for any
type of positive contribution to the human race. They are the garbage
that lie to get a job, then dog it for as long as they can with a constant
stream of excuses, lies, abuse, and obnoxious behavior in lieu of
actually ever writing any type of useful software.

In other words, the epitomy of how most employers are forced
to view "software engineers" due to long painful experience...

---
William Ernest Reid



.



Relevant Pages

  • Common Environment Object Model: getting the path to the cpp file a certain class is implemented in
    ... Say we have a CodeClass object and call AddFunction() on it. ... the call will end up with the function declaration ... in the header file for that class and the implementation in the cpp file. ...
    (microsoft.public.vsnet.general)
  • Re: Novice - Advice required
    ... > a seperate .cpp file (or header file?) and then whenever I want to ... int XYX::doTask ...
    (microsoft.public.vc.language)
  • Re: C++ header file
    ... Why not just have 1 CPP file and include the CPP file instead of ... In Java, there is no header file, each object is .java file. ... Separation of declaration and definition is very practical thing. ... It is sufficient to see only function's declaration in order to successfully compile a code that calls it. ...
    (microsoft.public.vc.language)
  • Re: Importing a type library into unmanaged code with /clr switch - linker errors
    ... I made some progress by just enabling /clr on the module (cpp file) ... but when I add managed code into the header file like: ... I get the compiler error: ...
    (microsoft.public.dotnet.languages.vc)