1

Golang has a lot of useful libraries but I need to support my C++ application and can't re-write it in Golang (it is too large).

So I'm trying to use Go's libraries ($GOPATH\pkg\path\to\package\*.a files) from my C++ application by static linking.

First of all I make simple library (Golang):

    package math_core
    func Sum(a, b int) int {
        return a + b
    }

And built this code (in root folder for this project) via following commands:

    SET GOOS=windows
    SET GOARCH=386
    go build
    go install

(as result I got libmath.core.a library)

After it I make simple application which will use this library (C++):

    #include <iostream>
    extern int Sum(int a, int b);
    int main() {
        std::cout << Sum(5, 7) << std::endl;
        return 0;
    }

I found this articles in the web:

but it doesn't help me.

I use following SOFTWARE:

  • Windows 10 x64
  • Eclipse IDE for C/C++ Developers (Neon.3 Release 4.6.3)
  • GCC version 4.8.1 20130324 (prerelease) (rubenvb-4.8-stdthread)
  • Gogland 1.0 EAP (Build #GO-173.3415.23)
  • JRE 1.8.0_152-release-1024-b6 amd64
  • Go 1.9.2 windows/amd64

Content of Z:\\MyProjects\\Math.Console\\ folder:

Files structure

I trying to statically link libmath.core.a library from my math.console application using following commands:

    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

But I got this OUTPUT:

`Z:\MyProjects\Math.Console/libmath.core.a: could not read symbols: Archive has no index; run ranlib to add one

Also I tried to extract object files (*.o) from library libmath.core.a via

    ar -xo libmath.core.a

and I got following files:

    __.PKGDEF
    _go_.o
    main.o

When I trying to see it contents via nm I got this:

    nm: __.PKGDEF: File format not recognized
    nm: _go_.o: File format not recognized

And for main.o file from libmath.core.a library I got this:

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

But I don't understand how can I use Golang's library from C++ project.

Thank you for any help.

V. Panchenko
  • 774
  • 1
  • 10
  • 32
  • "Also I tried to extract object files (*.o) from library libmath.core.a and I got ... main.o" The `main.o` file is produced from your C++ source. The only way it would be in the library archive is you had put it in there yourself. – davmac Oct 27 '17 at 12:23
  • 1
    Also, I highly doubt that Go uses C++-compatible name mangling. In general it seems [impossible to directly call Go from C](https://stackoverflow.com/questions/1713214/how-to-use-c-in-go), so it won't be possible in C++ either. – davmac Oct 27 '17 at 12:26

1 Answers1

0

I believe that you need to have your exported function in the main package.

Also I think that you need to have an empty main function in that file.

func main() {}

soluwalana
  • 68
  • 7