简体   繁体   English

使用SWIG和外部C库来构建问题

[英]Build issue with go using SWIG and an external C library

I am trying to create a golang program which will use an external C library. 我正在尝试创建一个将使用外部C库的golang程序。 Before doing complex stuff, I wanted to test the use of SWIG in a small foo example. 在做复杂的事情之前,我想在一个小的foo例子中测试SWIG的使用。 I also want to be able to use the "go get" syntax without having to manually run swig. 我还希望能够使用“go get”语法,而无需手动运行swig。 And to take into account that the external C library shall have been built before and can be located anywhere on the machine. 并且考虑到外部C库之前已经构建并且可以位于机器上的任何位置。

I am using golang 1.12.5 and swig 3.0.12 我正在使用golang 1.12.5和swig 3.0.12

I have created the following structure: 我创建了以下结构:

.
|-- libfoo
|   |-- foo.c
|   |-- foo.h
|   `-- libfoo.so
`-- src
    |-- example_test.go
    |-- lib.go
    `-- libfoo.swig

In the libfoo folder I have a simple shared library. 在libfoo文件夹中,我有一个简单的共享库。 The files contain: foo.c: 这些文件包含:foo.c:

#include "foo.h"

int foo(int c){
    return c+1;
};

foo.h foo.h中

int foo(int c);

That I've compiled it with: 我编译它:

gcc -o ./libfoo.so -fPIC -shared ./foo.c

Then the golang files: libfoo.swig 然后是golang文件:libfoo.swig

%module libfoo

%{
  extern int foo(int a);
%}

extern int foo(int a);

lib.go lib.go

package libfoo

/*
#cgo LDFLAGS: -L../libfoo -lfoo
*/

import "C"

And finally example_test.go 最后是example_test.go

package libfoo

import (
    "testing"
    "fmt"
)

func TestFoo(*testing.T) {
    i := Foo(1)
    fmt.Printf("%v\n",i)
}

When I try to build with "go build -x" It seems that the cgo directives located in the "lib.go" file are not taken into account as I do not see the flag appears in the output. 当我尝试用“go build -x”构建时,似乎没有考虑位于“lib.go”文件中的cgo指令,因为我没有看到输出中出现该标志。 Here is the output 这是输出

06aa7e308d6:/mnt/data/src# go build -x
WORK=/tmp/go-build551116655
mkdir -p $WORK/b001/
swig -version
cd $WORK
/opt/go/pkg/tool/linux_amd64/compile -o ./b001/_go_.o -trimpath ./b001 -p main -complete -goversion go1.12.5 -D _$WORK -c=4 ./swig_intsize.go
cd /mnt/data/src
swig -go -cgo -intgosize 64 -module libfoo -o $WORK/b001/libfoo_wrap.c -outdir $WORK/b001/ libfoo.swig
CGO_LDFLAGS='"-g" "-O2"' /opt/go/pkg/tool/linux_amd64/cgo -objdir $WORK/b001/ -importpath _/mnt/data/src -- -I $WORK/b001/ -g -O2 ./lib.go $WORK/b001/_libfoo_swig.go
cd $WORK
gcc -fno-caret-diagnostics -c -x c - || true
gcc -Qunused-arguments -c -x c - || true
gcc -fdebug-prefix-map=a=b -c -x c - || true
gcc -gno-record-gcc-switches -c -x c - || true
cd $WORK/b001
TERM='dumb' gcc -I /mnt/data/src -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_x001.o -c _cgo_export.c
TERM='dumb' gcc -I /mnt/data/src -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_x002.o -c lib.cgo2.c
TERM='dumb' gcc -I /mnt/data/src -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_x003.o -c _libfoo_swig.cgo2.c
TERM='dumb' gcc -I /mnt/data/src -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_x004.o -c libfoo_wrap.c
TERM='dumb' gcc -I /mnt/data/src -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_cgo_main.o -c _cgo_main.c
cd /mnt/data/src
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -o $WORK/b001/_cgo_.o $WORK/b001/_cgo_main.o $WORK/b001/_x001.o $WORK/b001/_x002.o $WORK/b001/_x003.o $WORK/b001/_x004.o -g -O2
# _/mnt/data/src
/tmp/go-build551116655/b001/_x004.o: In function `_wrap_foo_libfoo_0f106433f15c8754':
/tmp/go-build/libfoo_wrap.c:255: undefined reference to `foo'
collect2: error: ld returned 1 exit status

If I set the CGO_LDFLAGS using an environment variable, it works. 如果我使用环境变量设置CGO_LDFLAGS,它可以工作。 However I want to be able to just use "go get" without having those variables before. 但是我希望能够在没有这些变量的情况下使用“go get”。

I don't understand why go does not take my #cgo directive into account. 我不明白为什么不考虑我的#cgo指令。 Did I miss something ? 我错过了什么 ?

Someone on the golang github repository has solved my issue. golang github存储库中的个人已经解决了我的问题。

The solution was to remove the newline between the import "C" directive and the comments containing the #cgo directives. 解决方案是删除导入“C”指令和包含#cgo指令的注释之间的换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM