英文:
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应用程序中制作/导出/使用这个库?
这是我的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 "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
}
This simple library contains the Calculator
that sums numbers. Now I've a small C program that will use it:
calc.c:
#include "calcLib.h"
int main() {
printf("Hello World from C!\n");
printf("Calculator() -> %d\n", Calculator(10,5));
return 0;
}
Now I can compile the library and the C program with:
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
This will output an executable that works as expected:
> .\calc.exe
Hello World from C!
Calculator() -> 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"C:\Users\TCB13\shared\calcLib.a"
INCLUDEPATH += "C:\Users\TCB13\shared" # Shared folder includes the calcLib.h file
Then in my 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();
}
Compiling this results in the following errors:
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
。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论