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.
Related
As default, Forth has only a little amount of working libraries so that everything has to be programmed from scratch. The reason is, that the stackbased Forth virtual machine identifies itself as a slim system.
According to the Gforth manual, it's possible to use existing C libraries and get access to precompiled graphics libraries and game-engines written in C. Before it's possible to include the C library in Forth, it's a good idea to test the library within a normal C project.
I've created a library in C from scratch. It provides an add function and can be called from the main program. The files are compiled and linked and it works fine.
### add.c ###
int add(int a, int b) {
return a + b;
}
### add.h ###
int add(int, int);
### main.c ###
#include <stdio.h>
#include "add.h"
void main() {
printf("5 + 7 = %d\n", add(5,7));
}
### compile ###
gcc -c -fPIC add.c
gcc -c main.c
gcc main.o add.o
./a.out
5 + 7 = 12
The plan is to use this precompiled c-library from Forth. The Gforth compiler provides a special keyword for that purpose which connects a Forth program with a C library. Unfortunately, I get an error message saying that the library wasn't found. Even after copying it manually to the Gforth folder, the error message doesn't disappear.
### Forth source code ###
\c #include "add.h"
c-function add add n n -- n
5 7 add .
bye
### Execution ###
gforth "main.fs"
/home/user1/.gforth/libcc-tmp/gforth_c_7F5655710258.c:2:10: fatal error: add.h: No such file or directory
#include "add.h"
^~~~~~~
compilation terminated.
in file included from *OS command line*:-1
b.fs:3: libtool compile failed
5 7 >>>add<<< .
Backtrace:
$7F56556BD988 throw
$7F56556F9798 c(abort")
$7F56556F9F08 compile-wrapper-function
gforth: symbol lookup error: /home/user1/.gforth/libcc-tmp/.libs/gforth_c_7F0593846258.so.0: undefined symbol: add
### Copy missing file and execute again ###
cp add.h /home/user1/.gforth/libcc-tmp/
gforth "main.fs"
gforth: symbol lookup error: /home/user1/.gforth/libcc-tmp/.libs/gforth_c_7F5256AC2258.so.0: undefined symbol: add
What's wrong with the “Forth-to-C interface”?
You have to declare add as a function for export, compile it as a shared library (e.g. libadd.so) and add this library using the add-lib word, see Declaring OS-level libraries.
s" add" add-lib
NB: prefix 'lib' and suffix '.so' are added automatically.
I tried to use bindgen to automatically generate Rust FFI bindings to C and C++ libraries for the OCI bindings for an Oracle database.
I followed the bindgen User Guide, and I did it like this:
extern crate bindgen;
use std::env;
use std::path::PathBuf;
fn main() {
println!("cargo:rustc-link-search={}", "E:\\Rust\\instantclient_11_2\\sdk\\lib\\msvc");
let bindings = bindgen::Builder::default()
.header("wrapper.h")
.clang_arg("-I/E:\\Rust\\instantclient_11_2\\sdk\\include")
.generate()
.expect("Unable to generate to bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings.write_to_file(out_path.join("oci.rs"))
.expect("Couldn't write bindings");
}
My Oracle client SDK path is E:/Rust/instantclient_11_2, and the active toolchain is nightly-x86_64-pc-windows-msvc The wrapper.h file is:
#include <oci.h>
When I run cargo build, I get the following error:
Compiling test_bindgen v0.1.0 (file:///E:/Rust/test_bindgen)
error: failed to run custom build command for `test_bindgen v0.1.0 (file:///E:/Rust/test_bindgen)`
process didn't exit successfully: `E:\Rust\test_bindgen\target\debug\build\test_bindgen-67bec61306f8f8d4\build-script-build` (ex
it code: 101)
--- stdout
cargo:rustc-link-search=E:\Rust\instantclient_11_2\sdk\lib\msvc
wrapper.h:1:10: fatal error: 'oci.h' file not found, err: true
--- stderr
wrapper.h:1:10: fatal error: 'oci.h' file not found
thread 'main' panicked at 'Unable to generate to bindings: ()', src\libcore\result.rs:906:4
note: Run with `RUST_BACKTRACE=1` for a backtrace.
If I perform the same operation on Ubuntu 16.04 it succeeds, but I don't know how to do it on a Windows system.
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'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/
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.