cgo can't find .so in same directory

1,311 views
Skip to first unread message

Dean Schulze

unread,
Apr 30, 2020, 12:19:36 AM4/30/20
to golang-nuts
I'm following a simple example of using cgo to call a C library function from go.  Executing the binary gives

error while loading shared libraries: libperson.so: cannot open shared object file: No such file or director


Here's the relevant part from the main.go file:

/*
#cgo LDFLAGS: -L. -lperson
#include "person.h"
 */

import "C"


The file libperson.so is right in the same directory with main.go and person.h.  I've also created a soft link libperson.so.0 -> libperson.so but that doesn't have any effect.  I've tried this with go run and by go build to create a binary but both give the same error.

Does cgo recognize the LDFLAGS: -L. symbol at all?

What do I need to do to get a go binary to call a C function in a .so?  I really don't want to put my .so in the /usr/lib directory just to get a static linked binary for something like this.

Tamás Gulácsi

unread,
Apr 30, 2020, 1:26:48 AM4/30/20
to golang-nuts
Tweak your ld.conf configs and call ldconfig till ldd finds all libs of your binary.

Miki Tebeka

unread,
Apr 30, 2020, 3:05:58 AM4/30/20
to golang-nuts
IMO the #cgo directive is just building the executable, not for running it.
You can set the LD_LIBRARY_PATH environment variable before running the exeutable
LD_LIBRARY_PATH=${PWD} ./app
$ LD_LIBRARY_PATH=${PWD} ./app

Dean Schulze

unread,
Apr 30, 2020, 9:38:18 AM4/30/20
to golang-nuts
That was it.  It's actually
export LD_LIBRARY_PATH=${PWD}

mi...@pekim.co.uk

unread,
Apr 30, 2020, 1:06:32 PM4/30/20
to golang-nuts
> When the cgo directives are parsed, any occurrence of the string ${SRCDIR} will be replaced by the absolute path to the directory containing the source file.

So this might be what you need.

#cgo LDFLAGS: -L${SRCDIR}/. -lperson

Ian Lance Taylor

unread,
Apr 30, 2020, 9:38:17 PM4/30/20
to mi...@pekim.co.uk, golang-nuts
On Thu, Apr 30, 2020 at 10:06 AM <mi...@pekim.co.uk> wrote:
>
> https://golang.org/cmd/cgo/
> > When the cgo directives are parsed, any occurrence of the string ${SRCDIR} will be replaced by the absolute path to the directory containing the source file.
>
> So this might be what you need.
>
> #cgo LDFLAGS: -L${SRCDIR}/. -lperson

On Unix systems, the -L option sets the search path for the shared
library at link time. It does not set the search path for the shared
library at run time. You can set the run time search path using the
-R option. See the system linker docs.

Ian



> On Thursday, 30 April 2020 05:19:36 UTC+1, Dean Schulze wrote:
>>
>> I'm following a simple example of using cgo to call a C library function from go. Executing the binary gives
>>
>> error while loading shared libraries: libperson.so: cannot open shared object file: No such file or director
>>
>>
>> Here's the relevant part from the main.go file:
>>
>> /*
>> #cgo LDFLAGS: -L. -lperson
>> #include "person.h"
>> */
>> import "C"
>>
>>
>> The file libperson.so is right in the same directory with main.go and person.h. I've also created a soft link libperson.so.0 -> libperson.so but that doesn't have any effect. I've tried this with go run and by go build to create a binary but both give the same error.
>>
>> Does cgo recognize the LDFLAGS: -L. symbol at all?
>>
>> What do I need to do to get a go binary to call a C function in a .so? I really don't want to put my .so in the /usr/lib directory just to get a static linked binary for something like this.
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/77f0e481-188f-4ec8-a621-37ff6c720cfd%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages