0

I have a program written in C and I need to use KDIS libraries which are written in C++. I compile my C program with automake&friends in KDevelop. How can I compile everything together?? Because I want to call some KDIS functions inside my C program.

Thank you in advance.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
dani
  • 341
  • 1
  • 7
  • 18

2 Answers2

6

If you need to call C++ functions which are not declated extern "C", then you have to do so from a C++ program yourself. You can create one single C++ file in your project which wraps all the library functions you need in extern "C" functions to be used by the rest of your project. You'll have to tell autotools that you're using both C and C++. The file extensions should be enough to decide which is which.

To give you an example, consider the following mymagic.cc creating bindings for some libmagic written in C++:

#include <libmagic/magic.hh>
extern "C" {
  int doMagic() {
    magic::Wizard w("foo", 42);
    magic::Result res = w.doMagic();
    return res.getResultCode();
  }
}

To the rest of your application, doMagic() would appear as just another C function. But the inside is C++, so it can use any C++ constructs you want. When you need to pass stuff from your library around, you should use pointers to opaque types. So in the header mymagic.h which is also used by your C code, you can write

struct magicValue;
int doMagic(void);
struct magicValue* createMagic(void);
void destroyMagic(struct magicValue*);

And in the mymagic.cc you'd then be more explicit:

struct magicValue {
    magic::value v;
    magicValue(magic::value val) : v(val) { }
};
magicValue* createMagic() {
    return new magicValue(magic::value("foo"));
}
void destroyMagic(magicValue*) {
    delete magicValue;
}
MvG
  • 57,380
  • 22
  • 148
  • 276
  • So I should create a main.c with extern "all functions in C++". But the parameters of that functions are C++?? – dani Aug 01 '12 at 08:57
  • 2
    @dani: If KDIS doesn't provide a C interface, then you need to invent one. It's up to you how to achieve that, but generally you will find yourself defining opaque pointer types in C to refer to the object types in C++, and writing a lot of functions of the form `ReturnType Foo_method(Foo *self, other args ...) { return self->method(other args...); }`. – Steve Jessop Aug 01 '12 at 09:08
  • @dani, I'm not sure I understand your comment. But I added some example, hope this clarifies things. Also have a look at the links SingerOfTheFall [provided](http://stackoverflow.com/a/11755623/1468366), they appear to have a lot more detail on how to not only call functions but pass data in various ways between C and C++. – MvG Aug 01 '12 at 09:19
  • @SteveJessop, feel free to edit the answer and include your example if you want to. – MvG Aug 01 '12 at 10:33
2

This link may help you understand how to mix C and C++ code in your application.

Also, look at this Stack Overflow question, I believe that's what you need.

Community
  • 1
  • 1
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105