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: go run command fails but go build runs ok (macOS) #33470

Closed
arash-hacker opened this issue Aug 5, 2019 · 13 comments
Closed

cmd/go: go run command fails but go build runs ok (macOS) #33470

arash-hacker opened this issue Aug 5, 2019 · 13 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@arash-hacker
Copy link

arash-hacker commented Aug 5, 2019

What did you do?

package main

/*
#include <stdlib.h>
#include <greeter.h>
*/
import "C"
import (
	"fmt"
	"unsafe"
)

func Random() int {
	return int(C.random())
}
func Seed(i int) {
	C.srandom(C.uint(i))
}
func main() {
	fmt.Println(Random())
	fmt.Println(Random())
	fmt.Println(Random())
	name := C.CString("Gopher")
	defer C.free(unsafe.Pointer(name))

	year := C.int(2018)

	ptr := C.malloc(C.sizeof_char * 1024)
	defer C.free(unsafe.Pointer(ptr))

	size := C.greet(name, year, (*C.char)(ptr))

	b := C.GoBytes(ptr, size)
	fmt.Println(string(b))
}

#include "greeter.h"
#include <stdio.h>
//export greet
int greet(const char *name, int year, char *out) {
    int n;
    
    n = sprintf(out, "Greetings, %s from %d! We come in peace :)", name, year);

    return n;
}
#ifndef _GREETER_H
#define _GREETER_H

int greet(const char *name, int year, char *out);

#endif

What did you expect to see?

program work with go run

What did you see instead?

Undefined symbols for architecture x86_64:
"_greet", referenced from:
__cgo_c4abf705382a_Cfunc_greet in _x002.o.
(maybe you meant: __cgo_c4abf705382a_Cfunc_greet).
ld: symbol(s) not found for architecture x86_64.
clang: error: linker command failed with exit code 1 (use -v to see invocation).

System details

go version go1.12.7 darwin/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/macuser/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/macuser/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
GOROOT/bin/go version: go version go1.12.7 darwin/amd64
GOROOT/bin/go tool compile -V: compile version go1.12.7
uname -v: Darwin Kernel Version 16.7.0: Thu Jun 15 17:36:27 PDT 2017; root:xnu-3789.70.16~2/RELEASE_X86_64
ProductName:	Mac OS X
ProductVersion:	10.12.6
BuildVersion:	16G29
lldb --version: lldb-900.0.64
  Swift-4.0
@arash-hacker arash-hacker changed the title go run but go build runs ok go run command fails but go build runs ok Aug 5, 2019
@arash-hacker
Copy link
Author

when i put defination from .c into .h file it works with go run but with warning.
./greeter.h:7:9: warning: implicitly declaring library function 'sprintf' with type 'int (char *, const char *, ...)' [-Wimplicit-function-declaration] ./greeter.h:7:9: note: include the header <stdio.h> or explicitly provide a declaration for 'sprintf'

@bcmills
Copy link
Contributor

bcmills commented Aug 5, 2019

What did you do?

Specifically, what arguments (if any) did you pass to go run? If the argument is one or more source files, they are compiled as a standalone package (independent of any other source files in the directory).

(Go is a compiled language; in general you should expect to run binaries using a go build command followed by a separate invocation of the resulting binary.)

@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Aug 5, 2019
@bcmills bcmills changed the title go run command fails but go build runs ok cmd/go: go run command fails but go build runs ok Aug 5, 2019
@arash-hacker
Copy link
Author

@bcmills it used without argument ;
it works if i put all c defination in header file ; also builds well ;
it seems clang option is wrong and must specify clang option to arms64 or smting else.

but any clue if i had source file? any more arguent required?(i also read about this in golang.org)
if there is some good tutorial, can you point me?
thanks.

@ianlancetaylor
Copy link
Contributor

Show us precisely what you did, for both go build and go run. Show us precisely what happened. Cut and paste from your terminal window. Don't tell us what happened, show us. Thanks.

@bcmills bcmills added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Aug 5, 2019
@arash-hacker
Copy link
Author

arash-hacker commented Aug 5, 2019

@ianlancetaylor
go run

@bcmills
Copy link
Contributor

bcmills commented Aug 5, 2019

@Arshiamidos, please cut and paste from the terminal window. Screenshots are neither efficient nor accessible, and it particular they make it very difficult for us to replicate the issue by cutting and pasting the same commands into our own terminals.

@bcmills bcmills added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Aug 5, 2019
@ianlancetaylor
Copy link
Contributor

Yes, please just cut and paste plain text. Don't show us an animated GIF.

I tried to understand the animated GIF and as far as I can tell you showed us go run main.go. How exactly did you run go build? Don't show us a GIF! Just show us in plain text.

@arash-hacker
Copy link
Author

go run main.go.

Undefined symbols for architecture x86_64: "_greet", referenced from: __cgo_cfbaede89d88_Cfunc_greet in _x002.o (maybe you meant: __cgo_cfbaede89d88_Cfunc_greet) ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

go bulid && ./CGO
Greetings, Gopher from 2018!

@zegl
Copy link
Contributor

zegl commented Aug 6, 2019

I've been able to reproduce it:

~/s/go-33470 ls
greeter.c greeter.h main.go
~/s/go-33470 cat greeter.c
#include "greeter.h"
#include <stdio.h>
//export greet
int greet(const char *name, int year, char *out) {
    int n;

    n = sprintf(out, "Greetings, %s from %d! We come in peace :)", name, year);

    return n;
}

~/s/go-33470 cat greeter.h
#ifndef _GREETER_H
#define _GREETER_H

int greet(const char *name, int year, char *out);

#endif
~/s/go-33470 cat main.go
package main

/*
#include <stdlib.h>
#include <greeter.h>
*/
import "C"
import (
	"fmt"
	"unsafe"
)

func Random() int {
	return int(C.random())
}
func Seed(i int) {
	C.srandom(C.uint(i))
}
func main() {
	fmt.Println(Random())
	fmt.Println(Random())
	fmt.Println(Random())
	name := C.CString("Gopher")
	defer C.free(unsafe.Pointer(name))

	year := C.int(2018)

	ptr := C.malloc(C.sizeof_char * 1024)
	defer C.free(unsafe.Pointer(ptr))

	size := C.greet(name, year, (*C.char)(ptr))

	b := C.GoBytes(ptr, size)
	fmt.Println(string(b))
}


~/s/go-33470 go build
~/s/go-33470 ./go-33470
1804289383
846930886
1681692777
Greetings, Gopher from 2018! We come in peace :)
~/s/go-33470 go run main.go
# command-line-arguments
Undefined symbols for architecture x86_64:
  "_greet", referenced from:
      __cgo_2810c9ee33be_Cfunc_greet in _x002.o
     (maybe you meant: __cgo_2810c9ee33be_Cfunc_greet)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
~/s/go-33470 go run
go run: no go files listed
~/s/go-33470 go run .
1804289383
846930886
1681692777
Greetings, Gopher from 2018! We come in peace :)

Since the program is using more than one file, I'm guessing that the single-file use of go run is not supposed to work, as go run does not "import" other Go-files in the same package either.

@arash-hacker arash-hacker changed the title cmd/go: go run command fails but go build runs ok cmd/go: go run command fails but go build runs ok (macOS) Aug 6, 2019
@bcmills
Copy link
Contributor

bcmills commented Aug 6, 2019

This all seems to be working as designed.

@Arshiamidos, note in particular https://golang.org/cmd/go/#hdr-Package_lists_and_patterns:

As a special case, if the package list is a list of .go files from a single directory, the command is applied to a single synthesized package made up of exactly those files, ignoring any build constraints in those files and ignoring any other files in the directory.

@bcmills bcmills closed this as completed Aug 6, 2019
@arash-hacker
Copy link
Author

arash-hacker commented Aug 6, 2019

it just use header file , what about source file .c ??@bcmills
how use defination in source file ?

@bcmills
Copy link
Contributor

bcmills commented Aug 6, 2019

@Arshiamidos, Go is a compiled language. Sometimes you will need to use go build instead of just go run, and that's ok.

@arash-hacker
Copy link
Author

disagree @bcmills
my code finally run after putting this

package main

/*
#cgo darwin LDFLAGS: -framework Cocoa
#include "tray.h"
*/
import (
	"C"
)
import "fmt"

tell golang to compile with which tools link

@golang golang locked and limited conversation to collaborators Aug 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants