why I tried to use cgo to link dylib file on mac fail?

Viewed 40
    package main

// #cgo CFLAGS: -I./lib
// #cgo LDFLAGS: -L./lib -lfifo
// #include "shmfifo.h"
import "C"

func main() {
    fifo := C.shmfifo_init(13, 3, 64)
    for i := 1; i <= 100; i++ {
        C.shmfifo_put(fifo, 9)
    }
}

enter image description here

that is my project structure, error msg blow:

$ go build pppp.go 
# command-line-arguments
ld: library not found for -lfifo
clang: error: linker command failed with exit code 1 (use -v to see invocation)
1 Answers

Ok the issue seems to be isolated to your linker is not finding the libfifo.dylib file. So hence the following error:

ld: library not found for -lfifo

I would recommend either:

  1. Replacing the relative reference in the -L args with the special cgo variable $SRCDIR to more explicitly point to the location. More on that in the godocs.

  2. You can also copy the directory structure and cgo args in the following cgo/dylib tutorial by copying the libfifo file (assuming it can also link to other dependencies without issue).

Related