1

I am trying to make CGO link my C++ static library with GO code. I have written a C-wrapper header and c-source file with extern "C" calls like this

#ifdef __cplusplus
extern "C" {
#endif

and have added -stdc++ to the LDFLAGS in my .go file

// #cgo CXXFLAGS: -std=c++14 -I.  -flto
// #cgo LDFLAGS: -lstdc++ -L. -llibforgo
// #include "Wrapper.h"
import "C"

but the linker does not seem to find c++ standard calls ans is full of errors like

 undefined reference to `operator new(unsigned long)'
 undefined reference to `operator delete(void*, unsigned long)'

Here are related go environment variables

CC="gcc"
CXX="g++"

What I am doing wrong? How to tell cgo to use g++ instead of gcc? Maybe I should recompile my static c++ library with some additional options?

pppery
  • 3,731
  • 22
  • 33
  • 46
  • See [How to use C++ in Go](https://stackoverflow.com/q/1713214/1256452) (check for the more recent answers!). – torek May 22 '22 at 09:05
  • Thank you, I have read this again, but I don't find the answer. It seems like there is an example for linking dynamic library, but not static. Or if I am missing the answer to my case, could you point me please? Thank you again, Yuliana – Yuliana Zigangirova May 22 '22 at 11:40
  • I have found a strange solution, but it worked. Put a dummy .cc file in the same directory. https://groups.google.com/g/golang-nuts/c/WXPBp6WTm-s – Yuliana Zigangirova May 22 '22 at 14:24
  • The linker is run only at the end of the compilation phase (by default anyway); the empty `.cc` file is making the Go build link against libstdc++ but you can put that in your link flags directly. – torek May 22 '22 at 18:16
  • I did it - as I wrote in my initial post and you can see in my example - but that did not help. So something is obviously wrong there with the cgo functioning. – Yuliana Zigangirova May 23 '22 at 10:10
  • 1
    I'm not sure what's in `libforgo` but probably `-lstdc++` must be listed last. – torek May 23 '22 at 18:28

0 Answers0