I want to use golang call c++ dll with swig on windows. (gc compiler, on Linux was successful.) But there have some problems. Here is the sample.

//sampel.h
int compute(int a, int b);

//sample.cpp
#include <iostream>
#include "sample.h"

int compute(int a, int b){
    int temp = (a+b)*(a-b);
    return temp;
}

//sample.i
%module sample

%inline %{
    #include "sample.h"
%}
int compute(int a,int b);

Now, I use this cmd to generate the wrap files:

swig -c++ -go -soname sample.dll -intgosize 64 sample.i

Then create an empty dll project in VS, add sample.h too Header Files, add sample.cpp and sample_wrap.cxx to Source Files, add sample.i to the project.

Build Solution, generates sample.dll

Use cmd as below to generate sample.a:

go tool 6g sample.go
go tool 6c -I C:\Go\pkg\windows_amd64 sample_gc.c
go tool pack grc sample.a sample.6 sample_gc.6

Next, install sample.a(to avoid some issue), then run the test.go:

package main

import (
    "fmt"
    "sample"
)

func main() {
    fmt.Println(sample.Compute(3, 4))
}

The problem is here, when I run test.go, got the error:

adddynlib: unsupported binary format

How do I fix the problem(dll and test.go is in the same directory)? Thanks! If you need extra information that I missed, just ask.

Alex

有帮助吗?

解决方案

According to SWIG Tutorial and SWIG Comparability, it works just with 32 bit versions of Windows:

SWIG also works perfectly well under all known 32 bit versions of Windows including 95/98/NT/2000/XP.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top