2

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\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")
}

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

Community
  • 1
  • 1
vanangamudi
  • 673
  • 1
  • 8
  • 21
  • 4
    Not sure about gcc. But: You want to import a local package so your import should look like `import "./fib"` not `"src/fib"` as there really is no package "src/fib" neither under GOROOT (and you don't seem to use GOPATH, but I don't know how gcc handles this). – Volker Oct 17 '13 at 21:13

2 Answers2

3

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.

Intermernet
  • 18,604
  • 4
  • 49
  • 61
1

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

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