How do you add an include directory to Go/LiteIDE? - go

I'm attempting to use LiteIDE to run the program:
// cudatest
package cudatest
import (
"fmt"
"github.com/barnex/cuda5/cu"
)
func main() {
fmt.Println("Hello, your GPU is:", cu.Device(0).Name())
}
All I get is:
c:/go/bin/go.exe build -i [C:/godev]
# github.com/barnex/cuda5/cu
c:\GoDev\src\github.com\barnex\cuda5\cu\context.go:5:18: fatal error: cuda.h: No such file or directory
//#include <cuda.h>
^
compilation terminated.
Error: process exited with code 2.
I have the latest CUDA SDK from NVIDIA installed. What do I need to do in order to make the Go compiler see it?

It looks like your C compiler doesn't know what directory has the cuda header file. You can tell go to give the C compiler extra options using the CGO_CPPFLAGS environment variable. You probably want to set it to something like
CGO_CPPFLAGS="-isystem /path/to/dir/with/cuda/header/in/it"
See https://golang.org/cmd/cgo/

Related

VSCode import problem with windows API call

When importing golang.org/x/sys/windows in VSCode, I can only choose SIGDescribe, nothing else.
Hovering over the import, following errors appear.
error while importing golang.org/x/sys/windows: build constraints exclude all Go files in /home/username/go/pkg/mod/golang.org/x/sys#v0.0.0-20210630005230-0f9fa26af87c/windows
could not import golang.org/x/sys/windows (no required module provides package "golang.org/x/sys/windows")compilerBrokenImport
The manual command go get golang.org/x/sys/windows gives the following error message
Command 'gopls.go_get_package' failed: Error: err: exit status 1: stderr: package golang.org/x/sys/windows: build constraints exclude all Go files in /home/username/go/pkg/mod/golang.org/x/sys#v0.0.0-20210630005230-0f9fa26af87c/windows .
I already re-installed Golang and updated GoTools in VSCode, no changes.
Goal: The following code below should work.
package main
import "golang.org/x/sys/windows"
func main() {
user32DLL := windows.NewLazyDLL("user32.dll")
}
OS: Ubuntu 21.04
GO Version: 1.16.6
Editor: VSCode 1.58.1
Make a folder somewhere something. Then make a file something/main.go:
package main
import "golang.org/x/sys/windows"
func main() {
println(windows.EVENTLOG_ERROR_TYPE == 1)
}
Then build:
go mod init something
go mod tidy
go build

cgo - 'runtime.h' file not found in MacOSX

I am trying to execute the cgo program as mentioned here
package main
/*
#include "runtime.h"
int goId() {
return g->goid;
}
*/
import "C"
import "fmt"
func main() {
x := C.goId()
fmt.Printf("Id - %d", x)
}
On running the above program I am getting the below error:-
jab-MacBook-Pro-4:src debraj$ go build gid.go
# command-line-arguments
./gid.go:4:10: fatal error: 'runtime.h' file not found
#include "runtime.h"
^
1 error generated.
If I change the header to be like below:-
#include <objc/runtime.h>
then it is giving me the below error:-
jab-MacBook-Pro-4:src debraj$ go build gid.go
# command-line-arguments
./gid.go:7:9: error: use of undeclared identifier 'g'
return g->goid;
^
1 error generated.
Environment
MacOSX - 10.11.6
Go - 1.7.3
Can someone let me know how can I run the above program in MacOSX?
As discussed in golang-nuts this is not possible as of Go 1.5
That's not a cgo program there, but a Go program, which uses the gc
compiler's affinity to compile C programs into the resulting binary.
But since Go 1.5.0, that C backend is removed (the last version is
1.4.2), so that side effect has vanished.

Stringer tool complains about wrong archive header

I am trying to use go generate/stringer (golang.org/x/tools/cmd/stringer) to generate String() methods on enums. I have problems, which I believe, are because of slightly different format of .a packages on different systems. I have this file:
package main
import (
"math/rand"
)
//go:generate stringer -type=Foo
type Foo int;
const (
FooPrime Foo = iota
FooBis
)
func main() {
//Just use rand anywhere, otherwise we get a compiler error
rand.Seed(1)
}
Now if I run go generate example.go on my machine everything is all right: foo_string.go is created. However, on a test machine I get:
stringer: checking package: example.go:4:2: could not import math/rand (reading export data: /usr/lib64/go/pkg/linux_amd64/math/rand.a: go archive is missing __.PKGDEF)
Now, after some digging in the code I think that I get this error, because on my machine rand.a has the following header:
!<arch>
__.PKGDEF 0 0 0 644 2051
`
while on test machine it has the following header:
!<arch>
__.PKGDEF/ 0 399 399 100644 2051
`
I think that the crucial difference is slash after PKGDEFF. gcimporter refuses to process .a file, if it doesn't have __.PKGDEF header.
To check this, I edited manually gcimporter/exportdata.go and changed one of the line from this:
if name != "__.PKGDEF"
to this:
if name != "__.PKGDEF" && name != "__.PKGDEF\"
After this change (and compiling and installing everything) I was able to run go generate on example.go.
My questions are: why do I get this problem and how do I get rid of it (other then manually editing external library)?
What I can see from the spec for openSUSE's packaging they are disabling reinstallation of the standard library at updates. __.PKGDEF is a Go specific informational section, and some linker OpenSUSE has used has simply produced incompatible output.
There's nothing you can do except install a healthy Go from the official source.

I can't run Go programs anymore

This is the oddest problem I've ever encountered. I have my Go development environment set up on a Windows 2008 R2 virtual machine. I don't even restart it nor run Windows update.
Today I just realized that I can no longer run Go programs. I can successfully build and run unit tests with 'go test'. However, running any compiled Go program, (even hello world) causes a pop-up window titled 'Unsupported 16-bit application' to appear. The error message is as follows:
The version of this file is not compatible with the version of Windows
you're running. Check your computer's system information to see
whether you need an x86 (32-bit) or x64 (64-bit) version of the
program, and then contact the software publisher.
The result is the same regardless of what version of Go I use (x86/x64). Also note that I'm not using any IDE. I call go.exe to build/test from the command line.
I can't get my head around this since running 'go test' works just fine.
Any thoughts?
EDIT:
Here's the console output when I build and run the program:
build/run output
Interestingly, dumpbin suggests that indeed there's something wrong with the executable
C:\Program Files (x86)\Microsoft Visual Studio 11.0>dumpbin /headers
C:\Projects \GoPlayground\src\playground\playground.exe Microsoft (R)
COFF/PE Dumper Version 11.00.51106.1 Copyright (C) Microsoft
Corporation. All rights reserved.
Dump of file C:\Projects\GoPlayground\src\playground\playground.exe
File Type: LIBRARY
C:\Projects\GoPlayground\src\playground\playground.exe : warning
LNK4003: invali d library format; library ignored
C:\Projects\GoPlayground\src\playground\playground.exe : warning
LNK4048: Invali d format file; ignored
Summary
C:\Program Files (x86)\Microsoft Visual Studio 11.0>
And here's the full source code:
package playground
import "fmt"
import "playground/another"
func main() {
fmt.Println("Hello world!")
fmt.Println(another.Foobar(2))
}
-------------------
package another
func Foobar(i int) int {
return i + 1
}
EDIT2:
I've reinstalled Go twice with no effect.
The Go Programming Language Specification
Program execution
A complete program is created by linking a single, unimported package
called the main package with all the packages it imports,
transitively. The main package must have package name main and declare
a function main that takes no arguments and returns no value.
func main() { … }
Program execution begins by initializing the main package and then
invoking the function main. When that function invocation returns, the
program exits. It does not wait for other (non-main) goroutines to
complete.
Use package main, not package playground. For example,
playground.go:
package main
import (
"fmt"
"playground/another"
)
func main() {
fmt.Println("Hello world!")
fmt.Println(another.Foobar(2))
}
playground/another.go:
package another
func Foobar(i int) int {
return i + 1
}
Output:
Hello world!
3

How can I compile a Go program?

I got Go to compile:
0 known bugs; 0 unexpected bugs
and typed in the "hello world":
package main
import "fmt"
func main() {
fmt.Printf("Hello, 世界\n")
}
Then I tried to compile it, but it wouldn't go:
$ 8c gotest2
gotest2:1 not a function
gotest2:1 syntax error, last name: main
This is going on on Ubuntu Linux on Pentium. Go installed and passed its tests. So where did I go wrong? Can someone tell me where to go from here?
I also tried this program:
package main
import fmt "fmt" // Package implementing formatted I/O.
func main() {
fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n");
}
But this was also no go (must stop making go puns):
$ 8c gotest3.go
gotest3.go:1 not a function
gotest3.go:1 syntax error, last name: main
For Go 1.0+ the correct build command is now: go build
You're using 8c, which is the c compiler. 8g will compile go, and 8l will link.
(Update for Go1.0.x)
The section "Compile packages and dependencies" now list go build as the way to compile in go.
You still call 8g behind the scene, and the parameters you could pass to 8g are now passed with -gcflags.
-gcflags 'arg list'
arguments to pass on each 5g, 6g, or 8g compiler invocation
use go run to run the go program. Here is the output.
$ cat testgo.go
package main
import "fmt"
func main() {
fmt.Printf("Hello, 世界\n")
}
$go run testgo.go
Hello, 世界
To compile Go code, use the following commands:
go tool compile gotest3.go # To create an object file.
go tool link -o gotest3 gotest3.o # To compile from the object file.
chmod +x gotest3 # To apply executable flag.
./gotest3 # To run the binary.

Resources