4

I have a static var that calls to a constructor in a library in C++, something like this:

class ClassToBuild {
 public:
  ClassToBuild() {
    // Some cool stuff happens here...
  }
};
static ClassToBuild classToBuild;

The problem is that when this file is included in C++, the constructor is being called before the main function, but when I include this libraries from Go, the constructor is not being called. This are third part libraries that I can't modify, and the ClassToBuild is not exposed in the header, so I can't call it from outside.

How can I do to execute the constructor?

Thanks!

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
  • Are you using swig? cgo only understands C, not C++. – JimB Nov 24 '15 at 21:21
  • I'm using cgo, but I made a wrapper for the libraries using extern "C" { ... } in order to reference the C++ code – Alonso Vidales Miguélez Nov 24 '15 at 23:05
  • 1
    I'm not sure of the details around C++ and `extern "C"`, but I would first try to make a simple C program that calls the library in the same manner to establish if it actually has anything to do with Go. Maybe try swig, which may do something automatically which you're missing (the go build tool automatically uses swig files). – JimB Nov 24 '15 at 23:19

0 Answers0