简体   繁体   English

如果我导入“C”,则无法交叉编译

[英]Cannot cross-compile if I import "C"

Environment环境

  • Windows 10视窗 10
  • Go 1.13.7去 1.13.7
GOOS=windows
GOARCH=amd64
CGO_ENABLED=1

The problem问题

After days of trying to cross-compile my project, I figured that if one of my files does import "C", it seems that go build just skip it.在尝试交叉编译我的项目几天后,我想如果我的一个文件确实导入了“C”,那么go build似乎只是跳过它。

Here is my first file first.go :这是我的第一个文件first.go

package main

import "C"

type myFirstType struct {
    str string
}

func main() {
    v := myFirstType{str: "Hello"}
    printFirstTypeWithParam(v)
}

And here is the second second.go :这是第二个second.go

package main

import (
    "fmt"
)

func printFirstTypeWithParam(v myFirstType) {
    fmt.Println(v.str)
}

When I run go build , all is correct.当我运行go build ,一切都是正确的。 But when I run env GOOS=linux go build , here is the output:但是当我运行env GOOS=linux go build ,输出如下:

# test-package
.\second.go:7:32: undefined: myFirstType

As soon as I remove the import "C" line, all work perfectly.一旦我删除了import "C"行,一切正常。

Why is this a problem ?为什么这是个问题 ?

In the real project, I use gopkg.in/goracle.v2 , and this library does use C libraries in drv.go :在实际项目中,我使用gopkg.in/goracle.v2 ,这个库确实使用了drv.go C 库:

/*
#cgo CFLAGS: -I./odpi/include -I./odpi/src -I./odpi/embed

#include <stdlib.h>

#include "dpi.c"
*/
import "C"

When I run the same build command on my project ( env GOOS=linux go build ), here is the output:当我在我的项目( env GOOS=linux go build )上运行相同的构建命令时,输出如下:

# myproject/vendor/gopkg.in/goracle.v2
vendor\gopkg.in\goracle.v2\drv_10.go:22:2: undefined: ConnectionParams
vendor\gopkg.in\goracle.v2\drv_10.go:23:2: undefined: drv

Both undefined type and variable are defined in this drv.go file.未定义类型和变量都在此drv.go文件中定义。

Is this normal behavior, or am I missing something ?这是正常行为,还是我遗漏了什么?

goracle was moved to godror and doesn't support cross-compile. goracle 已移至godror ,不支持交叉编译。 Read the same issue here: https://github.com/go-goracle/goracle/issues/171在这里阅读同样的问题: https : //github.com/go-goracle/goracle/issues/171

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

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