Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using linux environment variable in cgo failed #43364

Closed
bill0119 opened this issue Dec 24, 2020 · 4 comments
Closed

Using linux environment variable in cgo failed #43364

bill0119 opened this issue Dec 24, 2020 · 4 comments

Comments

@bill0119
Copy link

What version of Go are you using (go version)?

$ go version 1.15.6

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/bill/.cache/go-build"
GOENV="/home/bill/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/bill/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/bill/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.15"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.15/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build550771112=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Using cgo to load a shared library in linux.

tree
.
├── inc
│   └── mylib.h
├── libmylib.so
├── main.go
├── mylib.o
└── src
    └── mylib.c

src/mylib.c

#include 

void PrintHello()
{
    printf("Hello\n");
}

inc/mylib.h

void PrintHello();

main.go

package main

/*
#cgo linux CFLAGS: -I${LIB_PATH}/inc
#cgo linux LDFLAGS: -lmylib
#cgo linux LDFLAGS: -L${LIB_PATH}/cgo
#include "mylib.h"
*/
import "C"

func main() {
        C.PrintHello()
}

Compile shared library

gcc -fPIC -c src/mylib.c
gcc -shared mylib.o -o libmylib.so

Set the env.

export LIB_PATH=/home/bill/project/cgo
export LD_LIBRARY_PATH=/home/bill/project/cgo

run the code

go run main.go
/home/bill/project/cgo/main.go: malformed #cgo argument: -I${LIB_PATH}/inc

What did you expect to see?

No environment var is ok

package main

/*
#cgo linux CFLAGS: -I/home/bill/project/cgo/inc
#cgo linux LDFLAGS: -lmylib
#cgo linux LDFLAGS: -L/home/bill/project/cgo
#include "mylib.h"
*/
import "C"

func main() {
        C.PrintHello()
}
go run main.go
Hello
@mengzhuo
Copy link
Contributor

  1. Just remove ${LIB_PATH} in cgo arguments and put all files in the package.

  2. You might want to check "C_INCLUDE_PATH" and setting
    https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html
    and use Makefile to overwrite these enviorment values.

@bill0119
Copy link
Author

bill0119 commented Dec 24, 2020

  1. Just remove ${LIB_PATH} in cgo arguments and put all files in the package.
  2. You might want to check "C_INCLUDE_PATH" and setting
    https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html
    and use Makefile to overwrite these enviorment values.

So I can't load library in different folder? It seems not convenient.

@mengzhuo
Copy link
Contributor

  1. Just remove ${LIB_PATH} in cgo arguments and put all files in the package.
  2. You might want to check "C_INCLUDE_PATH" and setting
    https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html
    and use Makefile to overwrite these enviorment values.

So I can't load library in different folder? It seems not convenient.

That's what I mean in "2", for example: export C_INCLUDE_PATH=$C_INCLUDE_PATH:/your-custom-include-path

@bill0119
Copy link
Author

  1. Just remove ${LIB_PATH} in cgo arguments and put all files in the package.
  2. You might want to check "C_INCLUDE_PATH" and setting
    https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html
    and use Makefile to overwrite these enviorment values.

So I can't load library in different folder? It seems not convenient.

That's what I mean in "2", for example: export C_INCLUDE_PATH=$C_INCLUDE_PATH:/your-custom-include-path

https://golang.org/cmd/cgo/#hdr-Using_cgo_with_the_go_command

only ${SRCDIR} can be accept?
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. This allows pre-compiled static libraries to be included in the package directory and linked properly. For example if package foo is in the directory /go/src/foo:

// #cgo LDFLAGS: -L${SRCDIR}/libs -lfoo

@bill0119 bill0119 reopened this Dec 24, 2020
@golang golang locked and limited conversation to collaborators Dec 31, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants