使用Go编译具有多个文件的项目。

huangapple go评论89阅读模式
英文:

compiling projects with multiple files in go

问题

我使用gccgo来编译我的项目。这是我的目录结构。我阅读了这个问答主题如何在golang中使用自定义包?

所以我按照这个结构来组织代码:

src/  
 +-fibo/  
 |   +-fibo.go  
 +main.go  

以下是代码清单:

main.go

package main

import (
    "os"
    "fmt"
    "strconv"
    "src/fibo"
)

func main(){

    if len(os.Args) < 2 {
        fmt.Printf("ur input sucks\n")
    }
    num,_ := strconv.Atoi(os.Args[1])
    fibo.Fibo(num)
}

fibo/fibo.go

package fibo

import "fmt" 

func Fibo(num int) {

    var a,b int
    for i :=0; i< num; i++ {
        a, b = b, a+b
        fmt.Print(a, " ")
    }
    fmt.Print("\n")
}

但是当我尝试编译时,我按照通常的gcc过程进行操作。将文件分别编译,然后将它们链接到最终的可执行文件中。我得到了以下错误:

.../go-lang-expts/src $ gccgo -c -ofibo/fibo.o fibo/fibo.go 
.../go-lang-expts/src $ gccgo -c -omain.o main.go 

   main.go:7:10: error: import file ‘src/fibo’ not found 
   main.go:18:2: error: reference to undefined name ‘fibo’

.../go-lang-expts/src $ 

我被卡住了。我尝试了不同的目录结构组合,但都没有帮助。我错过了什么?我需要设置任何环境变量吗,即使是为了这个?

英文:

I use gccgo to compile my projects. here is my directory layout. I read this Q/A thread How to use custom packages in golang?

so followed this one

src/  
 +-fibo/  
 |   +-fibo.go  
 +main.go  

and here are the code listing

main.go

package main

import (
    &quot;os&quot;
    &quot;fmt&quot;
    &quot;strconv&quot;
    &quot;src/fibo&quot;
)

func main(){

    if len(os.Args) &lt; 2 {
        fmt.Printf(&quot;ur input sucks\n&quot;)
    }
    num,_ := strconv.Atoi(os.Args[1])
    fibo.Fibo(num)
}

fibo/fibo.go

package fibo

import 	&quot;fmt&quot; 

func Fibo(num int) {

    var a,b int
    for i :=0; i&lt; num; i++ {
        a, b = b, a+b
        fmt.Print(a, &quot; &quot;)
    }
    fmt.Print(&quot;\n&quot;)
}

but when I try to compile, i follwed usual gcc procedure. compile files separately and link them together into final executable. I get this error

.../go-lang-expts/src $ gccgo -c -ofibo/fibo.o fibo/fibo.go 
.../go-lang-expts/src $ gccgo -c -omain.o main.go 

   main.go:7:10: error: import file ‘src/fibo’ not found 
   main.go:18:2: error: reference to undefined name ‘fibo’

.../go-lang-expts/src $ 

I am stuck here. I tried different combination of directory structures. none helped. what am I missing? is there any environment variable I should set, even for this??

答案1

得分: 3

看起来你可能没有设置GOPATH环境变量

根据如何编写Go代码

>> GOPATH环境变量指定了你的工作空间的位置。在开发Go代码时,这可能是你唯一需要设置的环境变量。

根据你当前的目录结构:

src/
 +-fibo/
 |   +-fibo.go
 +main.go

如果你的src目录在GOPATH下,你只需要在main.go中写入:

import "fibo"

另请参阅“GOPATH环境变量”,来自go命令文档

英文:

It looks like you may not have set the GOPATH Environment Variable

From How to Write Go Code

>> The GOPATH environment variable specifies the location of your workspace. It is likely the only environment variable you'll need to set when developing Go code.

Given your current directory structure of

src/  
 +-fibo/  
 |   +-fibo.go  
 +main.go

If your src directory is under GOPATH then you should be able to just do:

import &quot;fibo&quot;

in main.go.

See also "GOPATH environment variable" from The go command documentation.

答案2

得分: 1

这组命令对我有效。

.../go-lang-expts/src $ gccgo -c -fgo-pkgpath=fibo -ofibo/fibo.o fibo/fibo.go

这将命名包为fibo,因此您在main.go中导入时必须使用这个名称。

import "fibo"

现在,您可以通过告诉编译器fibo.o库的位置来编译main.go。

.../go-lang-expts/src $ gccgo -c main.go -Ifibo

然后,您需要将这两个文件链接在一起以创建可执行文件main。

.../go-lang-expts/src $ gccgo -o main main.o fibo/fibo.o
英文:

This set of commands worked for me.

.../go-lang-expts/src $ gccgo -c -fgo-pkgpath=fibo -ofibo/fibo.o fibo/fibo.go

This will name the package fibo, so you will have to import it as such in main.go

import &quot;fibo&quot;

Now you can compile main.go by telling where fibo.o library is

.../go-lang-expts/src $ gccgo -c main.go -Ifibo

Then you need to link the two file to create an executable main

.../go-lang-expts/src $ gccgo -o main main.o fibo/fibo.o

huangapple
  • 本文由 发表于 2013年10月18日 04:55:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/19437086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定