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/go: cgo is not compiling C files in current directory #24592

Closed
kanwar-saad opened this issue Mar 29, 2018 · 3 comments
Closed

cmd/go: cgo is not compiling C files in current directory #24592

kanwar-saad opened this issue Mar 29, 2018 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@kanwar-saad
Copy link

Please answer these questions before submitting your issue. Thanks!

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

go version go1.10 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ekanwli/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/ws/proj/playground/go"
GORACE=""
GOROOT="/usr/lib/go-1.10"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.10/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build388128647=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I am trying to create a CGO program with separate C source file in the same directory. As mentioned in the official Cgo docs, Cgo will compile all C files in the same directory but in this case, it is not compiling the C file. And later gives a linker error.

myTest.go

package main

/*

#include <stdlib.h>
#include "myTest.h"

*/
import "C"
import "unsafe"

func Print(s string) {
    cs := C.CString(s)
    C.print_str(cs)
    C.free(unsafe.Pointer(cs))
}

func main() {
    str1 := "hi how are you\n"
    Print(str1)
}

myTest.h
void print_str(char *s);

myTest.c

#include "stdio.h"

void print_str(char *s)
{
    printf("%s\n", s?s:"nil");
}

What did you expect to see?

I expected that all C files in the current directory are compiled by go build

What did you see instead?

While compiling, I am getting the following error:

> go build myTest.go

# command-line-arguments
/tmp/go-build989557946/b001/_x002.o: In function `_cgo_4c80c9e4eaf0_Cfunc_print_str':
/tmp/go-build/cgo-gcc-prolog:49: undefined reference to `print_str'
collect2: error: ld returned 1 exit status

@kanwar-saad kanwar-saad changed the title GoC is not compiling C files in current directory CGo is not compiling C files in current directory Mar 29, 2018
@ianlancetaylor
Copy link
Contributor

Please show us ls -l of the directory containing your sources. Please show us the output of go build -x. Thanks.

@ianlancetaylor ianlancetaylor changed the title CGo is not compiling C files in current directory cmd/go: cgo is not compiling C files in current directory Mar 29, 2018
@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 29, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Mar 29, 2018
@jbardin
Copy link
Contributor

jbardin commented Mar 29, 2018

Hi @kanwar-saad,

You're only building a single go file. You need to build the entire package, either implicitly with go build, or via it's import path, go build import/path.

@ianlancetaylor
Copy link
Contributor

Ah, right. Thanks. This is not a bug.

@golang golang locked and limited conversation to collaborators Mar 29, 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.
Projects
None yet
Development

No branches or pull requests

4 participants