Golang使用pkg-config自动获取头文件和链接库的方法
(2016-04-19 21:08:04)
标签:
golangcgopkg-config |
分类: IT那点事儿说起来却也又臭又长 |
# tree
/home/ubuntu/third-parties/hello/
/home/ubuntu/third-parties/hello/
├──
include
│
└── hello_world.h
└──
lib
# cat hello.pc
prefix=/home/ubuntu/third-parties/hello
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${exec_prefix}/include
Name:
hello
Description:
The hello library just for testing pkgconfig
Version:
0.1
Libs: -lhello
-L${libdir}
Cflags:
-I${includedir}
# export
PKG_CONFIG_PATH=/home/ubuntu/third-parties/hello/lib/pkgconfig
# pkg-config --list-all | grep libhello
libhello libhello -
The hello library just for testing pkgconfig
# export
LD_LIBRARY_PATH=/home/ubuntu/third-parties/hello/lib
package main
// #cgo pkg-config: libhello
// #include < stdlib.h >
// #include < hello_world.h >
import "C"
import (
"unsafe"
)
func main() {
msg := "Hello, world!"
cmsg := C.CString(msg)
C.hello(cmsg)
C.free(unsafe.Pointer(cmsg))
}
# go build
hello_world.go
# ldd hello_world
linux-vdso.so.1 =>
(0x00007ffff63d3000)
libhello.so =>
/home/ubuntu/third-parties/hello/lib/libhello.so
(0x00007fc31c0e1000)
libpthread.so.0 =>
/lib/x86_64-linux-gnu/libpthread.so.0
(0x00007fc31bec3000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6
(0x00007fc31bafe000)
# ./hello_world
Hello, world!
# ldd hello_world
linux-vdso.so.1 =>
(0x00007fffa49e2000)
libhello.so
=> not found
libpthread.so.0
=> /lib/x86_64-linux-gnu/libpthread.so.0
(0x00007feb0fe93000)
libc.so.6 =>
/lib/x86_64-linux-gnu/libc.so.6 (0x00007feb0face000)