Dynamic include path in cgo?

744 views
Skip to first unread message

Miki Tebeka

unread,
Aug 9, 2020, 12:56:43 PM8/9/20
to golang-nuts
Is there a way to pass include path dynamically to cgo? 

I'm working with numpy and the way to find the include directory is to call (in Python) 
numpy.get_include()

In a Makefile I can run go build with 
CGO_CFLAGS=-I $(shell python -c 'import numpy; print(numpy.get_include())') go build

but I don't know how to do it as a
#cgo CFLAGS:
directive and I'd like to have the module go gettable

Ian Lance Taylor

unread,
Aug 9, 2020, 9:06:54 PM8/9/20
to Miki Tebeka, golang-nuts
The usual approach is to use "#cgo pkg-config:". But that assumes
that numpy provides pkg-config definitions. There isn't any other
support for dynamic dependencies specified in the Go input file. In
particular we don't and won't permit running arbitrary programs in a
#cgo line.

Ian

Miki Tebeka

unread,
Aug 9, 2020, 11:43:36 PM8/9/20
to golang-nuts
Thanks. I agree that running arbitrary code in #cgo directives is a bad idea :)
I'll probably document asking to 
export CGO_CFLAGS="-I $(python -c 'import numpy; print(numpy.get_include())')"
before running "go get"

alex.be...@gmail.com

unread,
Aug 10, 2020, 12:18:53 AM8/10/20
to golang-nuts
On Sunday, August 9, 2020 at 9:56:43 AM UTC-7, Miki Tebeka wrote:
Is there a way to pass include path dynamically to cgo?
The easiest way is to generate the include file and just reference it from cgo. It looks like your code needs to have a Makefile anyway.

Sebastien Binet

unread,
Aug 10, 2020, 2:01:07 AM8/10/20
to alex.be...@gmail.com, golan...@googlegroups.com
Another possible course of action is to have all the logic of generating these lines embedded inside a 'go generate' program.

That's what has been done in go-python/gopy for example.

-s






-------- Original Message --------
--
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/bded583f-4c18-4f65-ad6e-b5ccf40ed7c2o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages