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
cmd/cgo: inject preamble before other include directives #35315
Comments
/cc @ianlancetaylor |
Can you show an example where it would be appropriate for cgo code to include Python.h? |
Sure. Basically just before any generated Actual
Desired
The same goes for any other generated file which may contain Go exported functions and/or their declaration. |
A possible solution could be to remove |
@philtay Thanks. I understand the change you want to make. What I'm hoping to see is an example of actual Go code that requires that change. Not the output of cgo, but the code that a user would write that requires this change. |
@ianlancetaylor Ah, ok. Please find below a self contained example:
package main
/*
#cgo pkg-config: python3
#cgo CFLAGS: -std=c99
#include <Python.h>
*/
import "C"
//export foobar
func foobar() *C.PyObject {
return nil
}
func main() {}
#include <Python.h>
#include "_cgo_export.h"
PyObject *
foobar_wrap(PyObject *self, PyObject *Py_UNUSED(ignored))
{
return foobar();
} Run
|
Thanks for the example. I'm sorry to be so unclear, but that is still not what I am looking for. I'm trying to understand in what cases someone would want to write Go code that uses cgo that does a Looking at your example, why would someone write Go code that returns a |
@ianlancetaylor Two full blown examples in the wild:
My code is just a minimal reproducible example. I think the author of this blog post is on your team: https://blog.filippo.io/building-python-modules-with-go-1-5/ It explains why someone would want to write cgo code that does a |
Thanks for the pointers. |
/cc @FiloSottile |
@ianlancetaylor I'd like to help with this issue. Can you point me in the right direction? Thanks. |
At this point I don't know what the right direction is. Changes in this area have a history of breaking existing code. |
What about a cgo directive? Something like:
|
A |
Quite a tricky change to get into this cycle, thus I shall move it to Go1.16. |
Moving to Unplanned, since currently we don't have a plan to address this. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What did you see?
The cgo generated file
_cgo_export.c
includesstdlib.h
before anything else (see here). It precludes the possibility to use some "include first" headers without errors or warnings, such as Python.From the Python docs:
Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included.
Cgo should allow to inject a C preamble before any other include directive.
The text was updated successfully, but these errors were encountered: