Qt: 从Golang导出的共享C库

huangapple go评论95阅读模式
英文:

Qt: Shared C Library Exported From Golang

问题

我正在为您翻译以下内容:

我正在使用Golang导出可以在C中使用的共享库进行一些测试,并得出以下结果:

calcLib.go:

package main
import "C"

//export Calculator
func Calculator(x int, y int) int {
	return x+y;
}

func main() {
	// Need a main function to make CGO compile package as C shared library
}

这个简单的库包含了一个名为Calculator的函数,用于求和。现在我有一个小的C程序将使用它:

calc.c:

#include <stdio.h> 
#include "calcLib.h"

int main() {     
    printf("Hello World from C!\n");     
    printf("Calculator() -> %d\n", Calculator(10,5));
    return 0;
}

现在我可以使用以下命令编译库和C程序:

C:\Users\TCB13\Desktop
> go build -buildmode c-shared -o calcLib.a calcLib.go
C:\Users\TCB13\Desktop
> gcc -o calc.exe calc.c calcLib.a

这将输出一个正常工作的可执行文件:

> .\calc.exe
Hello World from C!
Calculator() -> 15

现在,我该如何在Qt项目中使用这个共享库?

我尝试在我的.pro文件中添加以下内容:

LIBS += -L"C:\Users\TCB13\shared\calcLib.a"
INCLUDEPATH += "C:\Users\TCB13\shared"  # Shared folder includes the calcLib.h file

然后在我的main.cpp中:

#include "testapp.h"

#include <QApplication>
#include <QtDebug>
#include "calcLib.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TestApp w;
    w.show();

    qDebug() << Calculator(10,5);

    return a.exec();
}

编译这个项目会产生以下错误:

Qt: 从Golang导出的共享C库

我该如何正确地在Qt应用程序中制作/导出/使用这个库?

这是我的calcLib.h文件供参考:https://pastebin.com/k3sKYiti

谢谢。

英文:

I was doing some tests with Golang exporting shared libraries that can be used in C and came up with the following:

calcLib.go:

package main
import &quot;C&quot;

//export Calculator
func Calculator(x int, y int) int {
	return x+y;
}

func main() {
	// Need a main function to make CGO compile package as C shared library
}

This simple library contains the Calculator that sums numbers. Now I've a small C program that will use it:

calc.c:

#include &quot;calcLib.h&quot;

int main() {     
    printf(&quot;Hello World from C!\n&quot;);     
    printf(&quot;Calculator() -&gt; %d\n&quot;, Calculator(10,5));
    return 0;
}

Now I can compile the library and the C program with:

C:\Users\TCB13\Desktop
&gt; go build -buildmode c-shared -o calcLib.a calcLib.go
C:\Users\TCB13\Desktop
&gt; gcc -o calc.exe calc.c calcLib.a

This will output an executable that works as expected:

&gt; .\calc.exe
Hello World from C!
Calculator() -&gt; 15

Now, how can I use this shared library in a Qt project?

I tried to add the following to my .pro file:

LIBS += -L&quot;C:\Users\TCB13\shared\calcLib.a&quot;
INCLUDEPATH += &quot;C:\Users\TCB13\shared&quot;  # Shared folder includes the calcLib.h file

Then in my main.cpp:

#include &quot;testapp.h&quot;

#include &lt;QApplication&gt;
#include &lt;QtDebug&gt;
#include &quot;calcLib.h&quot;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TestApp w;
    w.show();

    qDebug() &lt;&lt; Calculator(10,5);

    return a.exec();
}

Compiling this results in the following errors:

Qt: 从Golang导出的共享C库

How can I properly make/export/use this library in a Qt Application?

Here is my calcLib.h file for reference: https://pastebin.com/k3sKYiti

Thank you.

答案1

得分: 1

在qmake项目文件中,LIBS变量使用与gcc链接器相同的命令行选项,-L用于指定库的搜索路径,-l用于指定库的名称(不包括lib前缀和.a.dll.so后缀,我相信带有前缀的版本也可以工作)。

因此,应该写成LIBS += -L$${PWD}\shared -lCalc

qmake-variable-reference

英文:

In qmake project files LIBS variable uses same cli keys as gcc linker -L to specify searchpath for libraries and -l for library name (without lib prefix and without .a or .dll or .so suffix, I believe version with prefix should also work).

So it should be LIBS += -L$${PWD}\shared -lCalc

qmake-variable-reference

huangapple
  • 本文由 发表于 2021年12月2日 07:56:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/70192715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定