Search code examples
c++goswigpkg-config

Go + Swig building with external lib


I'm trying to build a cpp file with opencv functions.

Go 1.3 states that swig building is now bundled in go build tool but I didn't find a way to tell that build tool to add include dirs and libs args with pkg-config.

go test -x cv_test.go                                                  
cd /Users/pierre/Projects/go-swig
clang++ -I . -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common -g -O2 -o $WORK/_/Users/pierre/Projects/go-swig/_obj/binding.cpp.o -c ./binding.cpp
# _/Users/pierre/Projects/go-swig
In file included from ./binding.cpp:1:
./binding.h:5:10: fatal error: 'cv.h' file not found
$WORK/command-line-arguments/_test/tiler.test
FAIL    command-line-arguments [build failed]

Has anyone did it successfully ?


Solution

  • You can tell CGo to use the include and link flags for a particular pkg-config library using the following syntax in one of your Go source files:

    // #cgo pkg-config: some-package
    import "C"
    

    That is, include it along with any other declarations in the comment block processed by cgo. It is only necessary to include this in one of the .go files in the package. You can also specify additional compile and link flags:

    // #cgo CXXFLAGS: -std=c++11
    // #cgo LDFLAGS: -L/some/library/dir -lfoo
    

    Full details can be found in the cgo documentation