dpyoh6553 2013-10-17 20:55
浏览 38
已采纳

使用多个文件编译项目

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 (
    "os"
    "fmt"
    "strconv"
    "src/fibo"
)

func main(){

    if len(os.Args) < 2 {
        fmt.Printf("ur input sucks
")
    }
    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("
")
}

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??

  • 写回答

2条回答 默认 最新

  • dsvtnz6350 2013-10-19 12:35
    关注

    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 "fibo"
    

    in main.go.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含