-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: searches in current directory for includes made with <angle brackets> #41059
Comments
This is happening because we add |
Oh. Well, for GCC, I think this could be fixed by using
https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html Clang also seems to support |
@ianlancetaylor I did some more research into this issue and I think it should be closed, because it turns out to be a breaking change (and not just theoretically). I started by putting together a patch that changed
That makes my test case work the way I expected (once I'd worked out I needed to invoke Then, I ran the test suite against this. There was one failure, from
Since I'd changed the I looked at these issues - #4339 just happens to be the header that's being included, but #29333 ("cmd/cgo: can not include header file in package directory with Go 1.12beta1") is relevant. Someone noticed that the cgo build of the module tl;dr - being able to include files from the current directory with Can you see a viable path forward here? At the very least, I figure there should be a warning in the cgo docs about the include path working this way - I'm happy to send a CL to add it if you think that's the way we should go. |
Thanks for doing the investigation. It does seem that we aren't going to be able to change this. I agree that this should be documented somewhere in cmd/cgo/doc.go. Perhaps you can solve your original problem using a #ifdef USE_LOCAL_HEADERS
local definitions
#else
#include <real-header-name.h>
#endif |
Change https://golang.org/cl/251758 mentions this issue: |
Thanks for the suggestion - putting a define in the CFLAGS sounds like a good option for fixing my original problem. I've sent a patch to document what's going on ☝️ |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
When importing a header file in cgo using the angle-bracket syntax (
#include <the/file.h>
), cgo looks in the code's directory for a file calledthe/file.h
, and if it finds it there, uses that file in preference to any other version of the file that might be found with a-I
CFLAGS or the system header directory.See this github repo for a reproduction of the issue: https://github.com/KJTsanaktsidis/cgo-header-repro
This is a problem in particular when using tags to allow users of a go module to switch between a bundled or system-installed version of a C library. See https://github.com/confluentinc/confluent-kafka-go for example, which, ordinarily, has
build_glibc_linux.go
selected by the default set of tags:It puts
${SRCDIR}
in the include path, so that#include <librdkafka/rdkafka.h>
includes in other files find the bundledlibrdkafka/kafka.h
.However, if the library is built with
tags=dynamic
,build_dynamic.go
is selected instead:Now, since
-I${SRCDIR}
is no longer in the include path, we expect that#include <librdkafka/kafka.h>
will find the system version of the header file located by pkgconfig. However, instead, it still finds the bundled header file, which is obviously a problem if they're different!What did you expect to see?
I expect that header includes are looked for in the SRCDIR only if
#include "the/file.h"
), or-I${SRCDIR}
is explicitly added to CFLAGSThis is the behaviour of gcc (I checked in my reproduction)
What did you see instead?
It seems like if an include is made with angle brackets, the header is looked for in SRCDIR even if SRCDIR isn't explicitly added to the include path.
The text was updated successfully, but these errors were encountered: