Run Rust program without cargo run [duplicate] - rust

This question already has answers here:
How do I make a Rust program which can be executed without using `cargo run`?
(4 answers)
Closed 8 months ago.
I built a simple CLI written in Rust that executes with the command cargo run <ARGUMENTS>. I want to be able to run the CLI from any directory. I used the clap crate and want to be able to call the script with the name passed to clap: brainfast <ARGUMENTS>. I am running on macOS.

This is more like a generic question (and I think a duplicate too, but I can't find any).
You have to copy your executable that is generated by cargo build --release (you can find it in target/release/crate_name) to a folder in your $PATH.
I'm not an expert in macOS, so I can't tell you what is a folder that is included in the $PATH, but you can find that out by yourself by opening a terminal and typing echo $PATH. Use one of the paths and it should be available in your terminal without cargo or using any path.
As an alternative, you can add a folder to your $PATH variable and put it there, e.g.
export PATH /home/foobar/.bin:$PATH
cp target/release/brainfast /home/foobar/.bin
brainfast abc.txt 1 3 99 u

Related

Git bash or NPM on Windows is converting an argument into a path [duplicate]

This question already has answers here:
How to prevent MSYS to convert the file path for an external program
(3 answers)
Closed 3 years ago.
I am writing a node.js script to patch server files. Our development machines are Windows, but the servers are Linux.
When I execute run the following script:
npm run patch-file --source-file "/path/to/file" --destination "/path/to/file"
Somewhere between Git bash or NPM the arguments are converted to windows paths. Which I don't want to happen here. I am handling the path conversion based on the source/ destinaiton in the script itself.
Actual Output:
node ./scripts/patch-file.js "--source-file" "C:/Git/path/to/file" "--destination" "C:/Git/path/to/file"
Expected output:
node ./scripts/patch-file.js "--source-file" "/path/to/file" "--destination" "/path/to/file"
Notes
This seems to be isolated to Git bash, I can't replicate it using windows command prompt or powershell. I'd still like to figure this out, because I would like this to work seemlessly between command terminals.
I believe that you are looking for the environment variable MSYS_NO_PATHCONV=1, as discussed here or here.
I did a test with notepad.exe. While this worked and opened the text file from the root of the Git for Windows installation
notepad.exe /LICENSE.txt
suggesting that the path conversion took place, this failed with notepad.exe complaining that the file /LICENSE.txt couldn't be found
MSYS_NO_PATHCONV=1 notepad.exe /LICENSE.txt
So, I think that the latter form is what you need.
Note that MSYS_NO_PATHCONV seems to be really specific to Git for Windows and is not available in other MSYS2-based terminals.

Ubuntu doesn't recognice any command [duplicate]

This question already has answers here:
Linux wrong path exported. How to recover ~./bashrc file
(2 answers)
Closed 4 years ago.
First of all, i was installing CUDA with cuDNN, the thing is that i put some new paths on the ~/.bashrc after that all the commands like ls, sudo, etc. doesn't work, it shows this message
The command could not be located because '/usr/bin' is not included in the PATH environment variable.
sudo: command not found
Do anyone knows what is the issue?
You need to include the /usr/bin path in your PATH environment variable because this is where the ls, cd & all others built-in commands are located.
The easiest way is to update your PATH environment variable with the export method :
export PATH="$PATH:/usr/bin"
If the export method doesn't work for you, you can edit your .bashrc file (nano ~/.bashrc) and update the PATH variable by adding the /usr/bin path in it.
Good luck

Cannot run Swift under Ubuntu after closing the terminal

I am trying to run the Swift compiler under Ubuntu. I followed this tutorial: https://itsfoss.com/use-swift-linux/ and everything seemed to work fine. I was able to run swift under Ubuntu.
However, when I closed the terminal, I was not able to run Swift anymore. The program was not found until I installed it again. I could not find any answers to this question as there aren't many people running Swift under Ubuntu.
It's not uninstalled, you just don't have the environment variables set up anymore, so Bash can't find the path to Swift. You can change that by exporting the appropriate environment variables in your .bashrc file.
When you followed the tutorial, you ran the following command:
export PATH=path_to_swift_usr_bin:$PATH
This command adds the path to the swift binary to your PATH environment variable. The PATH variable holds a list of places where Ubuntu will look for programs to run from the command-line. So if the Swift executable is not in one of the places listed in the PATH, your terminal will never find it.
There is a file in your home folder (the folder ~, which is an abbreviation for /home/username, where username is your username) named .bashrc, which runs whenever you open a new terminal window. If you need an environment variable to be available whenever you open the terminal, you should add the export line for that variable to your .bashrc.
In this case, your .basrhc should contain the same line above.
The important thing to remember is that your environment variables are not preserved between command-line sessions, so if you want to have an environment variable available every time you use the command-line, it needs to be defined in your .bashrc.

Set linker search path for build in CMake

It seems this question has been asked very often before but none of the solutions seem to apply in my case.
I'm in a CMake/Linux environment and have to run an executable binary during the build step (protoc in particular).
This binary needs a library but it's not installed (and cannot be) in the in the standard directories like /usr, so the library cannot be found.
Unfortunately I cannot manipulate the protoc call because it's embedded in a 3rd party script.
I can now set LD_LIBRARY_PATH before every make or set it system wide but this is very inconvenient especially when it comes to IDEs in which the build takes place or distributed build scenarios with continuous build environments.
I tried to set LD_LIBRARY_PATH via
set(ENV{LD_LIBRARY_PATH} "/path/to/library/dir")
but this seems to have no effect during the build step.
So my question is: can I set a library search path in CMake which is used during the build?
Try this
SET(ENV{LD_LIBRARY_PATH} "/path/to/library/dir:$ENV{LD_LIBRARY_PATH}")
I also used this dirty trick to temporary change some environment variables:
LD_LIBRARY_PATH="/path/to/library/dir:$LD_LIBRARY_PATH" cmake ...
After execution of this line LD_LIBRARY_PATH is not changed in the current shell.
Also, I do not find it bad to change LD_LIBRARY_PATH before invoking cmake:
export LD_LIBRARY_PATH=...
It won't change anything system-wide, but it would be used for your current shell, current build process. The same holds for CI builds. You can save the variable and restore it after cmake invocation:
MY_LD=$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=...
cmake...
export LD_LIBRARY_PATH=$MY_LD
I have recently run into a somewhat similar problem.
My solution was to incorporate sourcing a file that set the appropriate environment into every command.
For example, this custom command:
add_custom_command(
OUTPUT some_output
COMMAND some_command
ARGS some_args
DEPENDS some_dependencies
COMMENT "Running some_command some_args to produce some_output"
)
Would become:
set(my_some_command_with_environment "source my_environment_script.sh && some_command")
add_custom_command(
OUTPUT some_output
COMMAND bash
ARGS -c "${my_some_command_with_environment} some_args"
DEPENDS some_dependencies
COMMENT "Running some_command some_args to produce some_output"
VERBATIM
)
Obviously, this has some disadvantages:
It relies on a bash shell being available.
It sources the environment script for every command invocation (performance issue) and you will have to change all invocations of commands that rely on that environment variables.
It changes the normal syntax of having the command follow COMMAND and the arguments follow ARGS, as now the 'real' command is part of the ARGS.
My CMake-Fu has proven insufficient to find a syntactically nicer way of doing this, but maybe somebody can comment a nicer way.
I had a similar issue for an executable provided by a third party library. The binary was linked against a library not provided by the distribution but the required library was included in the libs directory of the third party library.
So running LD_LIBRARY_PATH=/path/to/thirdparty/lib /path/to/thirdparty/bin/executable worked. But the package config script didn't set up the executable to search /path/to/thirdparty/lib for the runtime dependent so CMake would complain when CMake tried to run the executable.
I got around this by configuring a bootstrap script and replacing the IMPORTED_LOCATION property with the configured bootstrapping script.
_thirdpartyExe.in
#!/bin/bash
LD_LIBRARY_PATH=#_thirdpartyLibs# #_thirdpartyExe_LOCATION# "$#"
CMakeLists.txt
find_package(ThirdPartyLib)
get_target_property(_component ThirdPartyLib::component LOCATION)
get_filename_component(_thirdpartyLibs ${_component} DIRECTORY)
get_target_property(_thirdpartyExe_LOCATION ThirdPartyLib::exe IMPORTED_LOCATION)
configure_file(
${CMAKE_CURRENT_LIST_DIR} _thirdpartyExe.in
${CMAKE_BINARY_DIR}/thirdpartyExeWrapper #ONLY
)
set_target_properties(ThirdPartyLib::exe PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/thirdpartyExeWrapper)
Honestly I view this as a hack and temporary stop gap until I fix the third party library itself. But as far as I've tried this seems to work on all the IDE's I've thrown at it, Eclipse, VSCode, Ninja, QtCreator, ... etc

Linux executable on cygwin terminal [duplicate]

This question already has answers here:
Executable file generated using GCC under cygwin
(5 answers)
Closed 7 years ago.
Let me tell you my problem. I've a shell script which execute a Linux executable. I don't have access to the source code of this exe.
When I run the script on the Linux machine, there is no problem.
But, if I try to run the script on my Windows laptop, using cygwin, I have the error "cannot execute binary file".
There is any solution ?
Thanks !
From https://stackoverflow.com/a/4144536/5704102:
"... Cygwin is a compatibility layer, which aims to implement as much as possible of the POSIX and Linux APIs within Windows. This means that programs have to be compiled specifically for Cygwin ..."
What does this script look like? Is it a bash script? If so, you may want to try sh <scriptfile> in Cygwin. If this doesn't work, and it is a bash script, make sure you have sh.exe. Type sh and hit tab twice to check.
Another potential issue if it is a bash script is that you don't have something installed that Cygwin requires to run the script. Make sure all the commands the script is trying to run will work. If not, you may have to run the Cygwin installer and install whatever is needed to run the script.
Also, you may want to check permissions.
If the file has an exe extension, it SHOULD work on Windows unless it was compiled for 64 bit architecture and you're running a 32 bit OS. Otherwise, you could be missing some .dll files that the executable relies on.
Edit:
If this is the contents of your script:
echo START
/oper/file.exe
then it's likely you have the incorrect path. Cygwin paths typically start with /cygdrive/c/ in Windows. Go to where the file exists and type pwd to get the correct path, then modify your script accordingly.
Content of Script.sh in oper folder :
echo START
/cygdrive/c/Users/jo/oper/file.exe
In Cygwin terminal (in the right folder):
sh Script.sh
(Path updated)

Resources