Vala GUI and logic in C++
Asked Answered
N

1

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?

Novelette answered 2/6, 2013 at 17:14 Comment(8)
What makes you think you can't bind a GObject-based API with a VAPI? Most of the available VAPIs do just that...Carboxylase
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.Novelette
Could you post the error here?Petes
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.Carboxylase
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 statuskNovelette
That error is something you would likely see if you were using C++ linkage. Make sure you're using extern "C" when appropriate. en.wikipedia.org/wiki/…Carboxylase
Thank you, @nemequ. Using extern "C" in C++ code is what I was looking for, the problem is solved.Novelette
It looks like valabind might be of use here.Clipfed
N
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
Novelette answered 6/6, 2013 at 2:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.