How do I run my program on the server

My server operating system is Rocky Linux like Centos Stream.

I cargo check and cargo build --release locally. When I package and upload the release in the target directory to the server, an error will be reported when it runs. The error is as follows

rustc covid-mange-admin

error: couldn't read covid-mange-admin: stream did not contain valid UTF-8

error: aborting due to previous error

my local os is Mac, local build and run success

assuming that covid-mange-admin is an executable, you should execute it, not pass it to rustc.

The command (under Linux) would be ./covid-mange-admin instead of rustc covid-mange-admin. (And possibly you need to mark the file as executable first.)

Running an executable produced on a different operating system won't work though, unless you use cross-compilation. The easier approach might be to build it from the source code (using cargo) on the server itself.

2 Likes

did you verify that covid-mange-admin is a rust program? also, rustc != cargo

Hi frank :slight_smile: are there any official documents or blogs running similar servers?

it is rust program

use cargo run on the server

My packaged directory:

build/
covid-mange-admin
covid-mange-admin.d
deps/
examples/
incremental/
libcovid_mange_admin.d
libcovid_mange_admin.rlib

so its an executable, not a rust program.

run it like so:

./cargo-mange-admin
./covid-mange-admin
-bash: ./ Covid mange admin: unable to execute binary file: executable file format error

it seems you did not type exactly ./covid-mange-admin, as the bash errors say you seem to have included 1) spaces instead of dashes and 2) capitalisations

please type exactly the executable name

I tried to change the name covid. The binary file format is incorrect when running, but I run the command on the mac machine

./covid-mange-admin

It runs successfully

Can I directly move the whole project to the service and use cargo run to run it, but I don't think this is correct. Shouldn't I deploy the application from the target directory

you compiled the program in a mac environment and tried to run that in a rocky linux server? thats probably why you are having problems.

you should probably instead do

cargo run --release
1 Like

yes you can, but it would take cross-compilation to compile the program in a different environment.

2 Likes

which im not experienced in btw, so @steffahn do you know how to cross compile?

what is cross compilation :grinning:

No; looking things up, it looks somewhat nontrivial though. Cross-compilation is important to target e.g. many embedded chips or mobile phones, because you (typically) cannot run the compiler on those devices directly; I’d say the most straightforward approach to target a Linux server is build it on the server, or another linux machine, or a linux VM.

I’ve never set up a server though, I’m just saying that I personally couldn’t think of any strong reason against building on the server that’s worth the hassle of setting up cross-compilation. (AFAIK there’s also the goal to make Rust cross compilation easier (to set up) eventually, but we’re probably not there yet.)

1 Like

Probably easiest to look up the term elsewhere. For example, this is from Wikipedia

Cross compiler

From Wikipedia, the free encyclopedia

A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. For example, a compiler that runs on a PC but generates code that runs on an Android smartphone is a cross compiler.

A cross compiler is necessary to compile code for multiple platforms from one development host. Direct compilation on the target platform might be infeasible, for example on embedded systems with limited computing resources.

Cross compilers are distinct from source-to-source compilers. A cross compiler is for cross-platform software generation of machine code, while a source-to-source compiler translates from one programming language to another in text code. Both are programming tools.

I don’t know about availability of Wikipedia for your location though. The Chinese term for “cross compiler” appears to be “交叉编译器”.

1 Like

Rust builds native binaries that don't have any dependency on a language runtime (unless you count the C library), but they do only work on a single target, defined as a "triple" of processor architecture, operating system and C runtime library.

By default, cargo build will produce a binary that works for the machine you're running it on. If you want to run it somewhere else, you need to install the "toolchain" that can build for the target environment and use that when building -this is called cross-compiling.

Fortunately, it's pretty easy to do with Rust when targeting Linux, at least:

  • First, find out the target environment's triple. Here it's probably x86_64-unknown-linux-gnu. Yes, triples actually have four parts now.

  • Then, install the target build tools with rustup toolchain install stable-x86_64-unknown-linux-gnu - eg channel-target

  • Then in the project you're cross compiling, tell cargo what target you want to use, with cargo build --target x86_64-unknown-linux-gnu

  • Your build is now in target under the target name, then release / debug as usual.

4 Likes