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

Comments

@gooid
Copy link

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

This comment has been minimized.

Copy link
Contributor

commented May 6, 2017

@bradfitz

This comment has been minimized.

Copy link
Member

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

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented May 7, 2017

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

This comment has been minimized.

Copy link
Member

commented May 7, 2017

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

This comment has been minimized.

Copy link
Contributor

commented May 7, 2017

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

This comment has been minimized.

Copy link
Contributor

commented May 8, 2017

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

This comment has been minimized.

Copy link
Contributor

commented May 22, 2017

@aarzilli

This comment has been minimized.

Copy link
Contributor

commented May 22, 2017

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

@rsc

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link

commented May 26, 2017

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

@rsc rsc added NeedsFix and removed NeedsDecision labels Jun 5, 2017

@gopherbot gopherbot closed this in b488073 Jun 13, 2017

gopherbot pushed a commit that referenced this issue Jun 14, 2017
cmd/go: fix TestIssue7573 for absolute paths in -L options
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.
Projects
None yet
9 participants
You can’t perform that action at this time.