golang新版本对于cgo支持的一个坑

文件test.h如下

#ifndef _TEST_H
#define _TEST_H

#ifdef __cplusplus
extern "C" {
#endif

extern void myTest();

#ifdef __cplusplus
}
#endif

#endif

文件test.cc如下

#include "_cgo_export.h"

extern "C" {

void myTest() {
	test();
}

}

文件test.go如下

package test

/*
#include "test.h"
*/
import "C"

import ()

func Test() {
	C.myTest()
}

//export test
func test() {
	println("hello")
}

如果执行go build -x则会有如下结果

WORK=/tmp/go-build845372545
mkdir -p $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/
mkdir -p $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/
cd /home/pirate/programs/goclipse/workspace/v8/src/test
/home/pirate/programs/go/pkg/tool/linux_amd64/cgo -objdir $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/ -- -I $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/ test.go
/home/pirate/programs/go/pkg/tool/linux_amd64/6c -F -V -w -I $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/ -I /home/pirate/programs/go/pkg/linux_amd64 -o $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/_cgo_defun.6 -D GOOS_linux -D GOARCH_amd64 $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/_cgo_defun.c
gcc -I . -g -O2 -fPIC -m64 -pthread -print-libgcc-file-name
gcc -I . -g -O2 -fPIC -m64 -pthread -I $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/ -o $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/_cgo_main.o -c $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/_cgo_main.c
gcc -I . -g -O2 -fPIC -m64 -pthread -I $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/ -o $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/_cgo_export.o -c $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/_cgo_export.c
gcc -I . -g -O2 -fPIC -m64 -pthread -I $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/ -o $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/test.cgo2.o -c $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/test.cgo2.c
gcc -I . -g -O2 -fPIC -m64 -pthread -I $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/ -o $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/test.o -c ./test.c
g++ -I . -g -O2 -fPIC -m64 -pthread -I $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/ -o $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/test.cc.o -c ./test.cc
g++ -I . -g -O2 -fPIC -m64 -pthread -o $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/_cgo_.o $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/_cgo_main.o $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/_cgo_export.o $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/test.cgo2.o $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/test.o $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/test.cc.o
# _/home/pirate/programs/goclipse/workspace/v8/src/test
/tmp/go-build845372545/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/test.cc.o: In function `myTest':
./test.cc:6: undefined reference to `test()'
collect2: error: ld returned 1 exit status

这里可以看到是test.cc是用g++来编译的,但是test函数的声明是在_cgo_export.c里,声明是在_cgo_export.h里,g++编译的时候肯定会找不到这个函数,但是只需要一个小修改就可以解决问题,即把include "_cgo_export.h"移到extern "C"里去,最终如下

extern "C" {

#include "_cgo_export.h"
void myTest() {
	test();
}

}


你可能感兴趣的:(golang新版本对于cgo支持的一个坑)