我正试图在同一个目录中创建一个具有单独C源文件的CGO程序。正如官方Cgo文档中提到的,Cgo将编译同一个目录中的所有C文件,但在本例中,它不是编译C文件。然后给出一个链接器错误。
myTest.go
package main
/*
#include <stdlib.h>
#include "myTest.h"
*/
import "C"
import "unsafe"
func Print(s string) {
cs := C.CString(s)
C.print_str(cs)
C.free(unsafe.Pointer(cs))
}
func main() {
str1 := "hi how are you\n"
Print(str1)
}myTest.h
void print_str(char *s);myTest.c
#include "stdio.h"
void print_str(char *s)
{
printf("%s\n", s?s:"nil");
}在编译过程中,我得到了以下错误:
> go build myTest.go
# command-line-arguments
/tmp/go-build989557946/b001/_x002.o: In function `_cgo_4c80c9e4eaf0_Cfunc_print_str':
/tmp/go-build/cgo-gcc-prolog:49: undefined reference to `print_str'
collect2: error: ld returned 1 exit statusGo版本是:
> go version
go version go1.10 linux/amd64发布于 2018-03-29 14:54:31
$ go帮助构建使用: go build -o输出build标志编译由导入路径命名的包以及它们的依赖项,但是它没有安装结果。如果要生成的参数是.go文件的列表,则生成将它们视为指定单个包的源文件列表。<< SNIP>>
如果要生成的参数是.go文件的列表,则生成将它们视为指定单个包的源文件列表。
> go build myTest.go构建文件myTest.go
跑
> go buildhttps://stackoverflow.com/questions/49556169
复制相似问题