Skip to content
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: #include relative to current directory broken since Go 1.8 #20266

Closed
gooid opened this issue May 6, 2017 · 10 comments
Closed

cmd/cgo: #include relative to current directory broken since Go 1.8 #20266

gooid opened this issue May 6, 2017 · 10 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@gooid
Copy link

gooid commented May 6, 2017

What version of Go are you using (go version)?

go version devel +4b2f7b4 Sat May 6 01:28:38 2017 +0000 windows/amd64

What did you do?

main.go

package main

//#cgo CFLAGS: -Iinc
//#include "foo.h"
import "C"

func main() {
	println(C.FOO)
}

inc\foo.h

#define FOO	5

go build main.go
.\main.go:4:10: fatal error: foo.h: No such file or directory
//#include "foo.h"
^~~~~~~
compilation terminated.

go1.8.1 is ok.

@davecheney
Copy link
Contributor

davecheney commented May 6, 2017 via email

@bradfitz
Copy link
Contributor

bradfitz commented May 6, 2017

If Go 1.8 is okay and tip is not, though, I want to make sure this was intentional.

/cc @rsc @ianlancetaylor

@bradfitz bradfitz added this to the Go1.9 milestone May 6, 2017
@bradfitz bradfitz added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label May 6, 2017
@ianlancetaylor
Copy link
Contributor

Works for me using both Go 1.8 and Go 1.9.

For the failing case please show us the output of go build -x main.go.

@alexbrainman
Copy link
Member

please show us the output of go build -x main.go.

Fails for me on the tip on both windows-amd64 and linux-amd64:

c:\Users\Alex\dev\src\issues\issue20266>dir
 Volume in drive C has no label.
 Volume Serial Number is F0C8-6DFE

 Directory of c:\Users\Alex\dev\src\issues\issue20266

07/05/2017  11:40 AM    <DIR>          .
07/05/2017  11:40 AM    <DIR>          ..
07/05/2017  11:40 AM    <DIR>          inc
07/05/2017  11:38 AM               105 main.go
               1 File(s)            105 bytes
               3 Dir(s)  82,081,398,784 bytes free

c:\Users\Alex\dev\src\issues\issue20266>type main.go
package main

//#cgo CFLAGS: -Iinc
//#include "foo.h"
import "C"

func main() {
        println(C.FOO)
}
c:\Users\Alex\dev\src\issues\issue20266>type inc\foo.h
#define FOO     5

c:\Users\Alex\dev\src\issues\issue20266>go version
go version devel +5300362 Sun May 7 01:26:02 2017 +0000 windows/amd64

c:\Users\Alex\dev\src\issues\issue20266>go build -x
WORK=C:\Users\Alex\AppData\Local\Temp\go-build643016190
mkdir -p $WORK\issues\issue20266\_obj\
mkdir -p $WORK\issues\issue20266\_obj\exe\
cd c:\users\alex\dev\src\issues\issue20266
CGO_LDFLAGS="-g" "-O2" "c:\\users\\alex\\dev\\go\\pkg\\tool\\windows_amd64\\cgo.exe" -objdir "C:\\Users\\Alex\\AppData\\Local\\Temp\\go-build643016190\\issues\\issue20266\\_obj\\" -importpath issues/issue20266 -- -I "C:\\Users\\Alex\\AppData\\Local\\Temp\\go-build643016190\\issues\\issue20266\\_obj\\" -g -O2 -Iinc main.go
cd $WORK
gcc -fdebug-prefix-map=a=b -c trivial.c
gcc -gno-record-gcc-switches -c trivial.c
cd $WORK\issues\issue20266\_obj
gcc -I "c:\\users\\alex\\dev\\src\\issues\\issue20266" -m64 -mthreads -fmessage-length=0 "-fdebug-prefix-map=C:\\Users\\Alex\\AppData\\Local\\Temp\\go-build643016190=/tmp/go-build" -gno-record-gcc-switches -I "C:\\Users\\Alex\\AppData\\Local\\Temp\\go-build643016190\\issues\\issue20266\\_obj\\" -g -O2 -Iinc -o "C:\\Users\\Alex\\AppData\\Local\\Temp\\go-build643016190\\issues\\issue20266\\_obj\\_cgo_export.o" -c _cgo_export.c
gcc -I "c:\\users\\alex\\dev\\src\\issues\\issue20266" -m64 -mthreads -fmessage-length=0 "-fdebug-prefix-map=C:\\Users\\Alex\\AppData\\Local\\Temp\\go-build643016190=/tmp/go-build" -gno-record-gcc-switches -I "C:\\Users\\Alex\\AppData\\Local\\Temp\\go-build643016190\\issues\\issue20266\\_obj\\" -g -O2 -Iinc -o "C:\\Users\\Alex\\AppData\\Local\\Temp\\go-build643016190\\issues\\issue20266\\_obj\\main.cgo2.o" -c main.cgo2.c
# issues/issue20266
.\main.go:4:17: fatal error: foo.h: No such file or directory
compilation terminated.

c:\Users\Alex\dev\src\issues\issue20266>

Alex

@hirochachacha
Copy link
Contributor

It seems that 8ae60dc is the cause.
8ae60dc converts -o relpath to -o abspath.
But it doesn't handle -I relpath and -L relpath well.

@ianlancetaylor
Copy link
Contributor

Oh, I see. It should work to write

-I ${SRCDIR}/inc

See https://golang.org/cmd/cgo/ .

I'm not sure whether we have introduced a new bug or whether your code is relying on undocumented behavior. I mean, you are definitely using undocumented behavior, and there is a documented workaround, but if a lot of people do what you are doing then perhaps we shouldn't change it.

aarzilli added a commit to aarzilli/golua that referenced this issue May 22, 2017
aarzilli added a commit to aarzilli/golua that referenced this issue May 22, 2017
aarzilli added a commit to aarzilli/golua that referenced this issue May 22, 2017
@davecheney
Copy link
Contributor

davecheney commented May 22, 2017 via email

@aarzilli
Copy link
Contributor

@davecheney never mind, I didn't read all of @ianlancetaylor's message.

@rsc
Copy link
Contributor

rsc commented May 22, 2017

This worked as recently as Go 1.8. Seems like we should keep it working for Go 1.9.

@rsc rsc changed the title cmd/go: gcc include error cmd/cgo: #include relative to current directory broken since Go 1.8 May 22, 2017
@gopherbot
Copy link

CL https://golang.org/cl/44291 mentions this issue.

@rsc rsc added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Jun 5, 2017
gopherbot pushed a commit that referenced this issue Jun 14, 2017
Updates #20266.
Fixes #20664.

Change-Id: Ifca30409fc7971497efb6d84f3f98760157c2233
Reviewed-on: https://go-review.googlesource.com/45650
Reviewed-by: Austin Clements <austin@google.com>
@golang golang locked and limited conversation to collaborators Jun 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

9 participants