multiple definition error when linking C++ dynamic library and Go with SWIG

橋本紘希 <[email protected]>
Newsgroups gmane.comp.programming.swig
Message-ID <CAAif--oQZSYz0ZMUvnaEiVMnVYvH_y6Q1SMPE5=4R8L4fba-eg@mail.gmail.com>
I would like to know the procedure to link the 3rd party dynamic
library, written in C++, and Go source code.
I have only C++ header files and shared library files(*.hh and *.so).
I do not have C++ source files(*.cc).

For a test purpose, I set up 3 repositories.

(1)https://github.com/hsmtkk/swigo_cc
The C++ program.

(2)https://github.com/hsmtkk/swigo_bridge
The program to bridge C++ and Go with SWIG.

(3)https://github.com/hsmtkk/swigo_user
The Go program.

For the codes in (2) and (3), C++ source files(*.cc) are not available.

This is my swigcxx file, included in the (2) repository.
https://github.com/hsmtkk/swigo_bridge/blob/main/example.swigcxx
```
%module example
%{
#include "example.hh"
%}
%include "example.hh"
```

This is my command to generate wrapper files in the (2) repository.
https://github.com/hsmtkk/swigo_bridge/blob/main/Makefile
```
swig -go -cgo -c++ -intgosize 64 -use-shlib -soname libexample.so
example.swigcxx
```

When I try to build the (3) program, I get this error.
```
$ go build
# github.com/hsmtkk/swigo_bridge
example.swigcxx:5: Error: Unable to find 'example.hh'
```

When I include the header file manually, I get another error.
```
$ env CGO_CXXFLAGS="-I`pwd`" go build
# github.com/hsmtkk/swigo_bridge
/tmp/go-build130079447/b002/_x005.o: In function
`_wrap_Swig_free_example_fe57d0301a1c3822':
example_wrap.cxx:(.text+0x83): multiple definition of
`_wrap_Swig_free_example_fe57d0301a1c3822'
/tmp/go-build130079447/b002/_x004.o:example_wrap.cxx:(.text+0x83):
first defined here
/tmp/go-build130079447/b002/_x005.o: In function
`_wrap_Swig_malloc_example_fe57d0301a1c3822':
example_wrap.cxx:(.text+0xad): multiple definition of
`_wrap_Swig_malloc_example_fe57d0301a1c3822'
/tmp/go-build130079447/b002/_x004.o:example_wrap.cxx:(.text+0xad):
first defined here
/tmp/go-build130079447/b002/_x005.o: In function
`_wrap_new_Example_example_fe57d0301a1c3822':
example_wrap.cxx:(.text+0xe4): multiple definition of
`_wrap_new_Example_example_fe57d0301a1c3822'
/tmp/go-build130079447/b002/_x004.o:example_wrap.cxx:(.text+0xe4):
first defined here
/tmp/go-build130079447/b002/_x005.o: In function
`_wrap_Example_GetNumber_example_fe57d0301a1c3822':
example_wrap.cxx:(.text+0x14d): multiple definition of
`_wrap_Example_GetNumber_example_fe57d0301a1c3822'
/tmp/go-build130079447/b002/_x004.o:example_wrap.cxx:(.text+0x14d):
first defined here
/tmp/go-build130079447/b002/_x005.o: In function
`_wrap_delete_Example_example_fe57d0301a1c3822':
example_wrap.cxx:(.text+0x187): multiple definition of
`_wrap_delete_Example_example_fe57d0301a1c3822'
/tmp/go-build130079447/b002/_x004.o:example_wrap.cxx:(.text+0x187):
first defined here
/tmp/go-build130079447/b002/_x004.o: In function
`_wrap_new_Example_example_fe57d0301a1c3822':
example_wrap.cxx:(.text+0x118): undefined reference to `Example::Example(int)'
/tmp/go-build130079447/b002/_x004.o: In function
`_wrap_Example_GetNumber_example_fe57d0301a1c3822':
example_wrap.cxx:(.text+0x171): undefined reference to `Example::GetNumber()'
/tmp/go-build130079447/b002/_x005.o: In function
`_wrap_new_Example_example_fe57d0301a1c3822':
example_wrap.cxx:(.text+0x118): undefined reference to `Example::Example(int)'
/tmp/go-build130079447/b002/_x005.o: In function
`_wrap_Example_GetNumber_example_fe57d0301a1c3822':
example_wrap.cxx:(.text+0x171): undefined reference to `Example::GetNumber()'
collect2: error: ld returned 1 exit status
```

How can I solve the issue above?

I would appreciate your help.
Best regards,

[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.