I wrote a little game using Rust, and I used cargo build --release to compile a release version on Mac.
I tried to share this with my friend who is using Ubuntu, but when he tried to run the binary, he got the following error:
cannot execute binary file: Exec format error
I searched for this but found no answers. Doesn't Rust claim to have "no runtime"? Shouldn't it be able to run anywhere in binary form?
Rust not having a runtime means that it doesn't have a lot of code running as part of the language (for example a garbage collector or bytecode interpreter). It does still need to use operating system primitives (i.e. syscalls), and these are different on MacOS and Linux.
What you want is a cross compiler. If you're using rustup, then installing a cross compiler should be simple:
# Install the toolchain to build Linux x86_64 binaries
rustup target add x86_64-unknown-linux-gnu
Then building is:
cargo build --release --target=x86_64-unknown-linux-gnu
Caveat: I don't have an OS X machine to test this on; please comment or edit to fix this if it works!
Well, it is because Rust has no runtime (unlike e.g. Java's JVM) that you can't just compile code on one OS and expect it to run on a different one; what you are looking for is cross-compilation. I haven't done it in Rust, but from what I can gather you can find relevant information on different cross-compilation Rust strategies on this GitHub repo.
Related
I am using Rust to cross compile a CLI.
An issue I am having is that typically the Rust compiler only works flawlessly when it compiles for the same OS/arch as the host it is running on (otherwise there are missing components, e.g. dynamic libs or compiler toolchains).
I want to release a binary that I have compiled on my Mac, but I want to be able to archive all inputs so that I can recreate the build for any future security audits.
For Linux I am using Docker containers which snapshot all files used as input to the cargo build process.
But for my Mac I have no idea how to isolate the cargo install, or what other toolchains or dynamic libs it may be calling out to.
Thanks
When I try to cross compile my golang project from OSX to Linux, then I get following error message:
# runtime/cgo
ld: unknown option: --build-id=none
clang: error: linker command failed
and the compilation aborts.
This is how I try to build my application:
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build
I also tried using gox:
gox -os="linux"
but it still did not work.
Everything works as expected if I do not use the GOOS=linux tag and I am able to build/run my project for/on my OSX machine successfully.
I can confirm that the command
$env CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -v main.go
works perfectly fine with a "Hello World" main.go file on MacOS X High Sierra without installing anything else than just go (see also here).
As already pointed out in the comments, you are probably trying to compile with cgo and obviously lack parts of the tool chain and/or headers and that is why your linker throws an error. Please provide an acceptable example, otherwise we won't be able to help you.
You need to install a proper toolchain to do that.
In order to build binaries for architectures different that your build
host, you need far more than just a cross-compiler - you need a
full-blown toolchain, which can be a real pain to create, as you
probably discovered.
A couple of approaches:
Use a proper Linux distribution in a virtual machine, such as
VirtualBox. If you only want to build binaries for Linux/i386 on an
MacOSX/x86_64 host, this is - in my opinion - the easiest, safest and
most clean solution. It is not a cross-compiler, of course, but it
works and it has the added advantage that you can actually test your
executables.
Use a script such crosstool-NG (a descendant of the original
crosstool) to automatically build the toolchain - definitely easier
than building it on your own, although you may have to compromise for
slightly older compiler versions.
Cross compiler for linux on mac os x
TL:DR;
Can I write GUI programs on Win 10 (64) using Rust without installing the full MinGW toolchain (nor the MS equivalent)?
Supplementary questions:
- If not, should I just go ahead and install MinGW?
- Does anything GNUish in Windows 10 Anniversary Update change any of this?
Longer version
I saw that Rust is the most loved programming language hereabouts so 15 minutes ago...
I read
No additional software installation is necessary for basic use of the GNU build.
Rust's support for the GNU ABI is more mature, and is recommended for typical uses.
So I downloaded Windows (GNU ABI †) (.msi) 64-bit to my PC running Windows 10.
TUI
I read some basic intro and used rustc to compile a Hello World! and ran it OK. I then read about cargo and reorganised buit and ran the same code using that.
GUI
I then searched for Rust GUI and found Kiss_UI
a simple UI tookit for Rust
So I did a cargo new Hello_GUI --bin and added
[dependencies.kiss-ui]
git = "https://github.com/cybergeek94/kiss-ui"
to Cargo.toml
I cut and pasted a simple example from that website into main.rs
I then ran cargo run --verbose. It did
Updating git repository `https://github.com/cybergeek94/kiss-ui`
Updating registry `https://github.com/rust-lang/crates.io-index`
Downloading iup-sys v0.0.3
Downloading libc v0.2.14
Downloading libc v0.1.12
Compiling libc v0.1.12
...
warning: crate `Hello_GUI` should have a snake case name such as `hello_gui`, #[warn(non_snake_case)] on by default
error: linking with `gcc` failed: exit code: 1
note: "gcc" "-Wl,--enable-long-section-names" ...
note: ld: cannot find -liup
error: aborting due to previous error
So I learned two things
I need to use snake case.
GUI programming is not "basic use".
I have no complaints about either of these points but could use a clue or two about the second:
Assuming I want to write a GUI equivalent of Hello World in a simple way, what are my main options now?
According to its documentation, kiss-UI depends on the IUP library.
The error from gcc (can't find -liup) suggests that you don't have IUP installed. You may be able to install it and have things work; it depends on whether the IUP bindings used by kiss-UI can cope with windows.
Some other GUI libraries can be found at awesome-rust. There are bindings to libraries like Qt and Gtk. If you know the windows API, you could also check out the winapi crate.
If you want to avoid messing with linking and stuff, you could try using a pure rust library like conrod, which should 'just work' on windows.
To answer your more broad questions:
See the footnote on the downloads page. The MSVC version of rust depends on MSVC being installed. The GNU/MinGW build is standalone.
Upon finishing a COBOL program, how do I compile it into an executable file that may be run on other PCs? I'm using OpenCOBOL via cygwin.
Check out this getting started page from the user manual for OpenCOBOL:
But in case the link is broken, just do this:
$ cobc -x hello.cob
$ ./hello
cobc is the compiler. hello.cob is the source file. The output is simply the file hello which can be run by calling ./hello. The -x option is necessary to build an executable.
However, with all compiled programs, it is compiled for the machine is was built on. It will work on machine with similar architectures, but you don't true cross-platform ability unless you're using an interpreted language like Python or Java.
If you compile with Cygwin, the target computers also need Cygwin, or in particular the cygwin dynamic libraries along with the OpenCOBOL runtimes.
Many times, you can also compile under MinGW, which lessens the dependencies, but also lessens the available POSIX features.
Easiest path, install OpenCOBOL and Cygwin on the target machines, and you'll be good to go, otherwise you'll need to produce release packages with all the dependencies and instructions for PATH settings.
I downloaded the latest Boost Jam binary from SourceForge and I'm using the command:
bjam toolset=gcc --build-type=complete stage
I've installed Cygwin along with the GCC compiler. While the command produces a stage/lib directory, I cannot find the thread library that I'm using in Linux. Boost Jam takes a while to run, so there could have been errors along the way. Can anyone guess as to why I don't have the thread library? Is there a specific command I can run in an attempt to only build the thread library? Thanks!
You most definitely need to check for, and provide the error messages. You can also try passing either threadapi=pthread or threadapi=win32 to Boost.Build invocation.
In general, using the GNU/Kernel32.dll operating system, sometimes referred to as Cygwin, is risky business. Will using the native port of gcc, from mingw, be sufficient for you?