How do I make a Rust program which can be executed without using `cargo run`? - rust

How can I make a program in Rust which can be executed from anywhere without using cargo run, by just clicking on the file?
Is there any crate? I have written code for snake game and I want to run it by just clicking on a file.

If you compile a Rust application with:
cargo build --release
it will place a binary file in ./target/release. So if your application is called snake_game, you can run it with ./target/release/snake_game, or by double-clicking on that file.
This binary is completely self-contained, so you can move or copy it to somewhere else on your computer.

First build your binary for release
cargo build --release
Next handle permissions
Typically chmod +x target/release/whateverYourProgramIsCalled to make executable, but cargo did this for us already
You can check it's Octal Permissions
ls -l target/release/whateverYourProgramIsCalled
chmod +x target/release/whateverYourProgramIsCalled
ls -l target/release/whateverYourProgramIsCalled
As you can see nothing has changed... the permissions were already correct for executing
Run that executable
./whateverYourProgramIsCalled
Optional: run from anywhere
You can run that binary anywhere from the command line
To do this you need to add it to your path
For mac you can add to your path from /etc/paths
whatever editor you prefer.... vi, code, etc...
sudo code /etc/paths
I added a path like this, saved and authenticated with a password
/Users/`whoami`/code/rust/binaries
whoami command is surrounded by `
next copy your new binary over to where it needs to be, in that binaries folder
cp whateverYourProgramIsCalled /Users/`whoami`/code/rust/binaries
Then open a new terminal window
Check your command is in your path
where whateverYourProgramIsCalled
Run your command from anywhere
whateverYourProgramIsCalled
Rejoice

There is another method you can do by using rustc. It will create an executable binary file in the same directory where your file exists.
Make sure you are in the src directory and the name of your file is main.rs.
rustc main.rs
./main
The advantage of using rustc is that you can run any file not only main.rs. just do:
rustc filename.rs
./filename
You can run it from terminal and also by clicking on that file.

Just wanted to add another answer here, not sure if it's related to a more recent version of rust.
If in the root of your project you just run the command cargo install --path . it will add it to cargo and allow you to run the binary just with project name.

Related

Bundle Expect with shell script to bypass installation of Expect

I want to create a shell script that uses the Expect library however Expect is not installed on any of the systems where I want to run the script. I also cannot install the library on these systems either. Can I build Expect from source and then put in same directory as the script? How would you go about this?
Yes you can, and it's not difficult.
Download Expect sources from https://sourceforge.net/projects/expect/files/latest/download?source=files
Unpack sources
gunzip expect.tar.gz
tar -xvf expect.tar
This will create a directory containing the Expect distribution. Change to that directory and
Configure sources for compilation:
./configure --prefix=~/
With --prefix parameter you specify where Expect should be installed. ~/ in my example is a shortcut for current user home directory, so it will be installed locally for your user only and you don't need root privileges this way to install it. In case you have root privileges and want to install it system-wide, you can omit --prefix parameter.
Compile Expect:
make
And install it:
make install
And that's it :)

Running cscope from CMake at build time

I'm using Ubuntu Linux.
I've been trying to get the following cscope command to run when I run "make" from my project directory, so it recompiles cscope and gets updated name information when I make my project.
cscope -b -q -U -R
Per my research and a bit of reading, I should be able to get CMake to run a command when you do 'make' by using the add_custom_command function in CMakeLists.txt.
However, many attempts and variations of it, have not been successful. Is it possible to run this as I want it with add_custom_command?
Simply doing this doesn't seem to work:
add_custom_command(OUTPUT
${CMAKE_CURRENT_BINARY_DIR}/cscope.in.out
${CMAKE_CURRENT_BINARY_DIR}/cscope.out
${CMAKE_CURRENT_BINARY_DIR}/cscope.po.out
COMMAND cscope -b -q -U -R)
I've tried using the TARGET overload of add_custom_command as well, and making a custom target with a dependency on either ALL or the main output file of the project, but that doesn't do anything either.
Ideally this would run after the project has been built, if could tell me what I'm doing wrong or if this is at all the way to do this, I'd be grateful?
I've tried using the TARGET overload of add_custom_command as well, and making a custom target with a dependency on either ALL or the main output file of the project, but that doesn't do anything either.
This seems to be the problem - when a CMake commands requires to be passed a target, they refer to the name of a target you've created previously by using any of add_executable, add_library or add_custom_target, which doesn't necessarily map to an actual artifact file generated by the command.
Here's my take on the problem, and it seems to generate the three cscope files in the build directory.
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(te)
add_executable(main main.cpp asdf.cpp)
add_custom_command(TARGET main POST_BUILD COMMAND cscope -b -q -U -R)
Note here that the name of the target here is whatever I've passed as the first argument to the add_executable command.

Can't execute from /usr/local/bin/ symlink

I've recently had to compile a program (Riak) from source since they don't have a repo available for Ubuntu 16.04 yet.
I've compiled the program and copied it to /opt/riak where it works fine.
Since this program requires sudo privileges, I've decided to symlink /opt/riak/bin/riak to /usr/local/bin/riak instead of adding the variable to the path via a profile.d file (because in order to work with sudo I'd have to remove env_reset from /etc/sudoers which I rather not do).
The error I get is the following:
/usr/local/bin/riak: 8: .: Can't open /usr/local/bin/../lib/env.sh
Shouldn't the symlink execute the file from the original's working directory? Is there a way to make it work?
The error message is almost self explanatory. Apparently the riak executable is trying to find a file called env.sh using a path relative to its own, namely ../lib/env.sh. Originally, this would resolve to the following path: /opt/riak/bin/../lib/env.sh, which is the same as /opt/riak/lib/env.sh. But now is trying to find the file at /usr/local/bin/../lib/env.sh which is the same as /usr/local/lib/env.sh and obviously the file is not there.
You have the following options (in order of preference):
Leave the program in /opt and invoke it from there
Leave the program in /opt and create a small wrapper shell script in /usr/local/bin that calls the original executable (see at the end of this post).
Recompile the program passing the right parameters to its configure script (e.g. --prefix=/usr/local) so that it works from /usr/local.
I would recommend against option 3; I prefer to let the /usr directory be managed by the distos package manager. If I have to compile something myself, I prefer to put it in a dedicated directory bellow /opt. This way, if I want to remove it later on, I can just delete that directory.
Example wrapper script for option 2:
#!/bin/bash
exec /opt/riak/bin/riak "$#"

Cargo path setup for rust-racer

I just installed racer using cargo. After installing it say this:
Installing /home/karthik/.cargo/bin/racer
warning: be sure to add `/home/karthik/.cargo/bin` to your PATH to be able to run the installed binaries
How do I do this? Googling didn't help. Also, Should I be setting a PATH variable for cargo bin as well?
Edit: OS is Ubuntu 14.04 and I have super user access
You have to add the cargo bin path to your PATH variable and set the RUST_SRC_PATH in .profile or .bash_profile.
Related unix.stackechange question
There are two steps:
(1) Add the Cargo bin to your PATH variable. You can run $ whereis cargo to find the bin path, and then do $ sudo -H gedit /etc/environment where you can add that new path section to your current PATH variable. You will need to save and close the file (and you can ignore the error message in the terminal during the saving portion) in order for it to take effect.
(2) Run $ rustup component add rust-src to download the necessary Rust source files for you.
At this point Racer should work properly.
This is based on the answer here.

How to create a cygwin executable

I was trying to follow the instructions here to get drush installed on cygwin:
https://www.drupal.org/node/1432756#comment-11184267
However, running "drush" from my drupal project's folder did nothing (it didn't tell me -bash: this_command_I_made_up: command not found as it does for a command that doesn't exist).
Eventually I tried to run the symlink command like this:
ln -s /usr/local/src/drush/drush.bat /usr/bin/drush.bat
instead of this:
ln -s /usr/local/src/drush/drush.bat /usr/bin/drush
In other words, I added the .bat suffix to the filename path (drush.bat) instead of leaving it as plain old drush. Now I get results as I expect when running drush commands from my drupal project folders, but I have to type in drush.bat instead of drush when running drush commands.
I was just wondering if anyone could shed some light on the situation as to why the plain old drush symlink without the .bat suffix doesn't work. Thanks!
You may need to make the src/drush/drush.bat executable. If the symlink you make has a .bat extension (or .exe, etc.), it will automatically be executable in cygwin.
For files without extensions, the file must be marked executable.
For symlinks without extensions, the source (src/drush/drush.bat) must be marked executable.
To mark a file executable, use the command chmod +x src/drush/drush.bat.

Resources