本文介绍了Vala GUI和C ++中的逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用SDL的绘图程序,用C ++编写。我想只在Vala创建一个图形界面,并使用它从一个程序调用函数(函数是准备使用,我只想从GUI调用它们)。我正在寻找解决方案作为VAPI,我在想使用GObject,但我不能接受两者。有没有人做过类似的事情,你能建议我解决我的问题吗?

解决方案

如果你想使用C ++代码Vala我们正确地准备他们。这是一个例子。



首先,你必须告诉 valac 编译器函数定义在其他地方。让我们使用 extern 指令。

  // ccodetest.vala 
extern void cpp_test_function );

void main(){
stdout.printf(This is Vala code\\\
);
cpp_test_function();然后,C ++中的函数与从C中派生的对象文件正确链接,因此,我们将它们声明为 externC

  // cpplibrary.cpp 
# include

using namespace std;

externCvoid cpp_test_function(){
cout< 这是一个C ++代码\\\
;
}

当我们准备好,我们可以编译Vala代码到C.我们得到 ccodetest.c



valac -C ccodetest.vala



现在我们可以使用 gcc 来编译目标文件。我们得到 ccodetest.o



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



File C ++编译如下。



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



最后我们链接这两个文件。



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



如下:

  $ ./ccode_test 
这是Vala代码
这是一个C ++代码


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?

解决方案

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

这篇关于Vala GUI和C ++中的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 05:11