Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

go - Cgo build settings for Docker

I am using cgo to call a C library from my Go code. I added .so and .h file into project in following structure. I am able to build in my local but I cannot build with build tools to get Docker image. I am getting following error. Can you help me for this issue?

/usr/lib64/go/1.14/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: cannot find -lm_server
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: cannot find -lpthread
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: cannot find -lc

Folder structure:

cmd
--main.go
src
--libs
----m_server.h
----libm_server.so
--internal
----server
------server.go

Yaml file:

...
properties:
- docker-params: "--workdir ${env.PWD}/src 
      --env HOME=/tmp 
      --env GOBIN=${env.PWD}/build 
      --env GOCACHE=/tmp/.gocache 
      --env GO111MODULE=on 
      --env CGO_ENABLED=1"
  - golangci-lint-config-yml: ${env.PWD}/golangci.yml

  - golang-ldflags: "-w -s -X main.Version=${var.version} -extldflags '-static'"

  build:
    - task: build
      docker-image: go-builder
      docker-flags:
        - ${docker-params}
      cmd:
        - sh -c '${go-prepare} && go install -v -a -ldflags="${golang-ldflags}" ./...'

...

Go file for C wrapper functions

package server

/*
#cgo CFLAGS: -I../../libs/m_server/
#cgo LDFLAGS: -L../../libs/m_server-lm_server -Wl,-rpath=../../libs/m_server
#include "m_server.h"
*/
import "C"
...
question from:https://stackoverflow.com/questions/66061060/cgo-build-settings-for-docker

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...