Vala GUI and logic in C++

旧巷老猫 提交于 2019-11-29 00:14:53

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!