[英]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 程序将使用它:
计算.c:
#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
这将 output 一个按预期工作的可执行文件:
> .\calc.exe
Hello World from C!
Calculator() -> 15
现在,如何在 Qt 项目中使用这个共享库?
我尝试将以下内容添加到 my.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 应用程序中正确制作/导出/使用此库?
这是我的 calcLib.h 文件供参考: https://pastebin.com/k3sKYiti
谢谢你。
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 )。
所以应该是LIBS += -L$${PWD}\shared -lCalc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.