9

I have a drawing program that uses SDL, written in C++. I would like to create a graphical interface only in Vala and use it to call functions from a program (functions are ready to use and I only want to call them from the GUI). I was looking for solutions as VAPI, and I was thinking of using GObject, but I can not embrace both. Has anyone done similar things and can you suggest me a solution to my problem?

Ocelpers
  • 319
  • 2
  • 9
  • What makes you think you can't bind a GObject-based API with a VAPI? *Most* of the available VAPIs do just that... – nemequ Jun 02 '13 at 22:07
  • I tried to compile Vala code to C and then create an object file. I created a object file from C++ code. Then I tried to link two files with `-lglib` and `-lgobject` in g++. Unfortunately, linking object from Vala code with function written in C++ returns an error. – Ocelpers Jun 03 '13 at 16:30
  • Could you post the error here? – MrEricSir Jun 04 '13 at 00:48
  • 1
    You can't link Vala code directly to C++, but creating a C wrapper for C++ code is generally quite trivial. You just have to be mindful about using C linkage for the symbols you want to expose to C/Vala. If you want an example take a look at the leveldb source code. Specifically, `include/leveldb/c.h` and `db/c.cc`. – nemequ Jun 04 '13 at 06:41
  • This is the error out: `ccodetest.o: In function '_vala_main':` `ccodetest.c:(.text+0x2f): undefined reference to 'cpp_test_function'` `collect2: ld returned 1 exit statusk` – Ocelpers Jun 04 '13 at 07:17
  • 1
    That error is something you would likely see if you were using C++ linkage. Make sure you're using extern "C" when appropriate. http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B#Linking_C_and_C.2B.2B_code – nemequ Jun 04 '13 at 09:01
  • Thank you, @nemequ. Using extern "C" in C++ code is what I was looking for, the problem is solved. – Ocelpers Jun 06 '13 at 02:08
  • It looks like [valabind](https://github.com/radare/valabind) might be of use here. – user Nov 24 '14 at 13:56

1 Answers1

23

If you want to use the C++ code in Vala we prepare them properly. Here's an example.

First you have to tell the valac compiler that the function is defined somewhere else. Let's use the extern directive.

// ccodetest.vala
extern void cpp_test_function ();

void main () {
    stdout.printf ("This is Vala code\n");
    cpp_test_function ();
}

Then the functions in C++ are properly linked with the object files derived from C, we declare them as extern "C".

// cpplibrary.cpp
# include

using namespace std;

extern "C" void cpp_test_function () {
    cout << "This is a C + + code\n";
}

When we are so ready, we can compile Vala code to C. We get ccodetest.c.

valac -C ccodetest.vala

Now we can use gcc to compile the object file. We get ccodetest.o.

gcc-o ccodetest.o ccodetest.c-c-I /usr/include/glib-2.0/ -I /usr/include/glib-2.0/glib/ -I /usr/lib/glib-2.0/include/

File C++ compile as follows.

g++ -o cpplibrary.cpp.o cpplibrary.cpp -c

At the end we linking both files.

g++ -o ccode_test ccodetest.o cpplibrary.cpp.o -L /usr/lib/ -lglib-2.0 -lgobject-2.0

The program works as follows:

$ ./ccode_test
This is Vala code
This is a C++ code
Ocelpers
  • 319
  • 2
  • 9