Golang有很多有用的库,但是我需要支持我的C++应用程序,不能用Golang重写它(它太大了)。
因此,我试图通过静态链接从我的C++应用程序中使用Go的库(C++文件)。
首先,我创建了一个简单的库(Golang):
package math_core
func Sum(a, b int) int {
return a + b
}通过以下命令构建了此代码(在此项目的根文件夹中):
SET GOOS=windows
SET GOARCH=386
go build
go install(结果我得到了libmath.core.a库)
之后,我创建了一个简单的应用程序,它将使用这个库(C++):
#include <iostream>
extern int Sum(int a, int b);
int main() {
std::cout << Sum(5, 7) << std::endl;
return 0;
}我在网上找到了的文章:
但这帮不了我。
我使用以下软件
Z:\\MyProjects\\Math.Console\\文件夹的内容:

我试图使用以下命令将libmath.core.a库与math.console应用程序静态链接
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "..\\main.cpp"
g++ "-LZ:\\MyProjects\\Math.Console" -o Math.Console.exe main.o -lmath.core但是我得到了这个输出
`Z:\MyProjects\Math.Console/libmath.core.a: could not read symbols: Archive has no index; run ranlib to add one此外,我还试图从库libmath.core.a中提取libmath.core.a
ar -xo libmath.core.a我得到了以下文件:
__.PKGDEF
_go_.o
main.o当我试图通过nm查看它的内容时,我得到了以下内容:
nm: __.PKGDEF: File format not recognized
nm: _go_.o: File format not recognized对于来自main.o库的libmath.core.a文件,我得到了以下内容:
00000000 b .bss
00000000 d .ctors
00000000 d .data
00000000 N .debug_abbrev
00000000 N .debug_aranges
00000000 N .debug_info
00000000 N .debug_line
00000000 N .debug_macro
00000000 N .debug_str
00000000 r .eh_frame
00000000 r .rdata$zzz
00000000 t .text
U ___main
00000058 t ___tcf_0
00000097 t __GLOBAL__sub_I_main
U __Z3Sumii
0000006a t __Z41__static_initialization_and_destruction_0ii
U __ZNSolsEi
U __ZNSolsEPFRSoS_E
U __ZNSt8ios_base4InitC1Ev
U __ZNSt8ios_base4InitD1Ev
U __ZSt4cout
U __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
00000000 b __ZStL8__ioinit
U _atexit
00000000 T _main但我不明白如何使用C++项目中的Golang库。
谢谢你的帮助。
发布于 2018-01-15 18:25:45
我认为您需要将导出的函数放在main包中。
另外,我认为您需要在该文件中有一个空的主函数。
func main() {}
https://stackoverflow.com/questions/46973463
复制相似问题