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: inconsistent definitions for C.uint #26743

Closed
navytux opened this issue Aug 1, 2018 · 12 comments
Closed

cmd/cgo: inconsistent definitions for C.uint #26743

navytux opened this issue Aug 1, 2018 · 12 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker
Milestone

Comments

@navytux
Copy link
Contributor

navytux commented Aug 1, 2018

Please answer these questions before submitting your issue. Thanks!

What did you do?

I was trying to rebuild my software with gotip and got the following error:

# github.com/DataDog/czlib
cgo: inconsistent definitions for C.uint
FAIL    lab.nexedi.com/kirr/neo/go/zodb/btree [build failed]

The build is working with Go1.10 and earlier releases.

What did you expect to see?

Builds ok.

What did you see instead?

The build for github.com/DataDog/czlib fails with cgo: inconsistent definitions for C.uint error. I've extracted the minimal reproducer:

fastzlib.h:

#ifndef _A_XXX_H
#define _A_XXX_H

typedef unsigned int uint;
void c_compress2(char *input, uint length);

#endif

fastzlib.c:

#include "fastzlib.h"

void c_compress2(char *input, uint length) {
        return;
}

fastzlib.go:

package czlib

/*
#include "fastzlib.h"
*/
import "C"

func Compress(input []byte) {
        C.c_compress2(nil, C.uint(len(input)))
}

zstream.go (EDIT: originally posted fastzlib.go content here by mistake):

package czlib
  
/*
void zstream_set_in_buf(char *strm, void *buf, unsigned int len) {
        return;
}
*/
import "C"

func setInBuf(buf []byte, size int) {
        C.zstream_set_in_buf(nil, nil, C.uint(size))
}

Does this issue reproduce with the latest release (go1.10.3)?

No, it is a regression:

kirr@deco:~/src/neo/src/github.com/DataDog/czlib/a$ gotip build
# github.com/DataDog/czlib/a
cgo: inconsistent definitions for C.uint
kirr@deco:~/src/neo/src/github.com/DataDog/czlib/a$ go build
kirr@deco:~/src/neo/src/github.com/DataDog/czlib/a$ echo $?
0
kirr@deco:~/src/neo/src/github.com/DataDog/czlib/a$ gotip version
go version devel +6b9c782f9f Wed Aug 1 00:57:00 2018 +0000 linux/amd64
kirr@deco:~/src/neo/src/github.com/DataDog/czlib/a$ go version
go version go1.10.3 linux/amd64

System details

go version devel +6b9c782f9f Wed Aug 1 00:57:00 2018 +0000 linux/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/kirr/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/kirr/go"
GOPROXY=""
GORACE=""
GOROOT="/home/kirr/src/tools/go/gotip"
GOTMPDIR=""
GOTOOLDIR="/home/kirr/src/tools/go/gotip/pkg/tool/linux_amd64"
GCCGO="/usr/bin/gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
GOROOT/bin/go version: go version devel +6b9c782f9f Wed Aug 1 00:57:00 2018 +0000 linux/amd64
GOROOT/bin/go tool compile -V: compile version devel +6b9c782f9f Wed Aug 1 00:57:00 2018 +0000
uname -sr: Linux 4.17.0-1-amd64
Distributor ID:	Debian
Description:	Debian GNU/Linux testing (buster)
Release:	testing
Codename:	buster
/lib/x86_64-linux-gnu/libc.so.6: GNU C Library (Debian GLIBC 2.27-5) stable release version 2.27.
gdb --version: GNU gdb (Debian 8.1-4) 8.1
@ianlancetaylor ianlancetaylor added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker labels Aug 1, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Aug 1, 2018
@ianlancetaylor
Copy link
Contributor

CC @randall77

@ianlancetaylor
Copy link
Contributor

What does gcc --version print?

@ianlancetaylor
Copy link
Contributor

Thanks for the reproduction case. Unfortunately it doesn't build:

> go build
# _/tmp/x/src/a
./zstream.go:8:6: Compress redeclared in this block
	previous declaration at ./fastzlib.go:8:23

When I change the name of Compress in one of the files it builds without error using GCC 7.3.0.

@ianlancetaylor
Copy link
Contributor

Can you see if your issue is fixed on tip now that CL 127156 is committed?

@navytux
Copy link
Contributor Author

navytux commented Aug 1, 2018

Thanks for feedback. My gcc is:

kirr@deco:~$ gcc --version
gcc (Debian 8.2.0-1) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The sample indeed does not build, after I tried downloading it afresh from here in another directory. I turned out I've made a copy-paste error. Here is correct zstream.go which should have been pasted in the first place:

zstream.go:

package czlib
  
/*
void zstream_set_in_buf(char *strm, void *buf, unsigned int len) {
        return;
}
*/
import "C"

func setInBuf(buf []byte, size int) {
        C.zstream_set_in_buf(nil, nil, C.uint(size))
}

(appologize for the pasting mistake on my side).

The problem persists with CL 127156 applied:

kirr@deco:~/tmp/trashme/bug$ gotip version
go version devel +b8669ef1ce Wed Aug 1 14:29:58 2018 +0000 linux/amd64
kirr@deco:~/tmp/trashme/bug$ gotip build
# _/home/kirr/tmp/trashme/bug
cgo: inconsistent definitions for C.uint

@navytux
Copy link
Contributor Author

navytux commented Aug 1, 2018

I've edited original issue description and put zstream.go content into there.

@ianlancetaylor
Copy link
Contributor

Thanks, I can recreate the problem now.

@navytux
Copy link
Contributor Author

navytux commented Aug 1, 2018

Thanks, good.

@ianlancetaylor
Copy link
Contributor

I think it's because cgo has its own definition of uint in nameToC, and the header file has typedef unsigned int uint;.

Shorter repro:

a.go:

package a

// typedef unsigned int uint;
// int C1(uint x) { return x; }
import "C"

var V1 = C.C1(0)

b.go:

package a

import "C"

var V2 C.uint

@gopherbot
Copy link

Change https://golang.org/cl/127356 mentions this issue: cmd/cgo: don't give inconsistent typedef error for cgo-defined types

@navytux
Copy link
Contributor Author

navytux commented Aug 2, 2018

Thanks for the fix.

@gopherbot
Copy link

Change https://golang.org/cl/128396 mentions this issue: [release-branch.go1.10] cmd/cgo: don't give inconsistent typedef error for cgo-defined types

gopherbot pushed a commit that referenced this issue Aug 8, 2018
…r for cgo-defined types

The cgo tool predefines some C types such as C.uint. Don't give an
error if the type that cgo defines does not match the type in a header file.

Fixes #26743

Change-Id: I9ed3b4c482b558d8ffa8bf61eb3209415b7a9e3c
Reviewed-on: https://go-review.googlesource.com/127356
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
(cherry picked from commit c29370c)
Reviewed-on: https://go-review.googlesource.com/128396
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@golang golang locked and limited conversation to collaborators Aug 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker
Projects
None yet
Development

No branches or pull requests

3 participants