Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGO ignores the LDFLAGS -L option on MacOS

Tags:

gcc

go

cgo

I'm trying to compile the following code on a MacOS machine

// #cgo darwin LDFLAGS: -L${SRCDIR}/build/darwin -lprocessing_lib
// #cgo linux LDFLAGS: -L${SRCDIR}/build/linux -lprocessing_lib
// #include "Processing-bridge.h"
// #include <stdlib.h>
import "C"
import "unsafe"

type ProcessorWrapper struct {
    ptr unsafe.Pointer
}

func init() {
    pr.ptr = C.NewProcessor()
}

func GetDefault() (id int, name string) {
    var default = C.GetDefault(pr.ptr)
    id = int(default.materialId)
    name = C.GoString(default.name)
    return
}

I have the libprocessing_lib.so file in the correct location defined in the LDFLAGS. However the compiler still throws an error saying

dyld: Library not loaded: build/lib_processing_lib.so
  Referenced from: /Users/ag/workspace/src/github.com/apremalal/pq-processor/./pq-processor
  Reason: image not found

Once I copy build/lib_processing_lib.so to the root of the project it works.

It seems that the library directory provided by the LDFLAGS is somehow getting overridden.

I've noticed that setting DYLD_LIBRARY_PATH variable before compilation also override this value.(I ran above scenarios without setting this parameter)

Interestingly though the same code works in Linux environment by picking up the correct path.

Is there anything special that I need to do in MacOS to get the desirable LDFLAGS behavior?

like image 225
Anuruddha Avatar asked Jul 11 '18 10:07

Anuruddha


1 Answers

Set the dynamic library load path.For mac setting DYLD_LIBRARY_PATH for linux LD_LIBRARY_PATH

like image 119
momo0853 Avatar answered Nov 16 '22 21:11

momo0853