0

I am having problems when trying to use SWIG to access C++-code from my GO-code.

My simplistic setup is:

Simple.h:

#include <iostream>

class SimpleSwig
{
    public:
        void hello_world();
};

Simple.cpp:

#include "Simple.h"

void hello_world()
{
    std::cout << "hello world\n";
}

simple.i:

%module simple

%{
#include "Simple.h"
%}

%include Simple.h

main.go:

package main

import "fmt"
import "ctogoswig/swig"

func main() {
    fmt.Println("go ...")

    s := simple.NewSimpleSwig()
    s.Hello_world()

    fmt.Println("done")
}

Issuing

$ swig -go -c++ -intgosize 64 .\simple.swigcxx

gives me the new files simple.go and simple_wrap.cxx.

When I then issue

$ go install

I am getting:

enter image description here

A call to

$ go build -x

yields:

enter image description here

My Go environment variables are:

enter image description here

So, what am I missing?

The mentioned line 277 in the "undefined reference" exception is within the file simple_wrap.cxx:

enter image description here

Maybe also relevant is that within this file the following include is present a little before line 277:

enter image description here

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
marc wellman
  • 5,808
  • 5
  • 32
  • 59
  • 1
    You have an undefined reference to `SimpleSwig::hello_world()`. Could you please point out where that is defined? (Not `hello_world()`, but `SimpleSwig::hello_world()`.) – JaMiT May 03 '22 at 11:24
  • @JaMiT... of course, please see my update. – marc wellman May 03 '22 at 11:30
  • 1
    That's where you're using the symbol not where it's defined. You've delcared the method `SimpleSwig::hello_world()` but you have defined the global function `hello_world()` not `SimpleSwig::hello_world()` – Alan Birtles May 03 '22 at 11:32
  • @AlanBirtles believe it or not, but you nailed it - you gave me the right answer! My dear, unknown friend - you ended days of suffering for me! Thank YOU! – marc wellman May 03 '22 at 14:23
  • To avoid future suffering if you dont understand an error message then copy and paste it into google and you'll undoubtedly find a SO post where someone else has encountered the same error – Alan Birtles May 03 '22 at 14:29

0 Answers0