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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!