What is the difference between executable files generated using chmod and those generated from a compilation?

Asked

Viewed 58 times

0

When I use Python on Linux, I usually transform extension files .py executable using the chmod. Example:

Filing cabinet user_sum_python.py:

#!/usr/bin/env python
def user_sum(L):
    if len(L)==1:
        return L[0]
    else:
        return L[0]+user_sum(L[1:])
print(user_sum([2,4,8,23,56,21,20,4,87]))

On the command line:

chmod +x user_sum_python.py

And so I can run the program using only ./user_sum_python.py.

On the other hand, if I write the same program in a compiler language, the computer already returns an executable. Example using Rust:

Filing cabinet user_sum_rust.rs:

fn user_sum(lista: &[i32]) -> i32 {
    if lista.len() == 1 {
        return lista[0];
    } else {
        return lista[0] + user_sum(&lista[1..]);
    }
}

fn main() {
    let one_list: [i32; 9] = [2, 4, 8, 23, 56, 21, 20, 4, 87];
    println!("{}", user_sum(&one_list));
}

Whirling rustc user_sum_rust.rs in the terminal, the program generates a file that I can run using ./user_sum_rust.

What is the difference between the two procedures? There are advantages and disadvantages associated with each of them?

  • 4

    Simply the Rust compiler ran chmod after the compilation. The question is mixing two things that have no direct relation (executable program with flag authorizing execution). the linux "+x" simply indicates that the user is allowed to ask for the environment to run that. You can give a chmod +x in a jpeg if you want. Only it is not good for much, because you authorized the user to execute something that is not executable.

  • 4

    Even without +x you can continue running . py using python directly. There is already a third thing, which is the linux shell using +x for another purpose, already described here https://answall.com/questions/57702/70

  • Rust only ran chmod? So you mean if I run chmod +x user_sum_rust.rs I will have the same result as running rustc user_sum_rust.rs?

  • 3

    I didn’t say "just". He compiled it. Then he spun the chmod (so you don’t have to do 2 steps). The Rust is generating an executable. Yours. py is not an executable, it’s just a script (whoever runs it is Python, even if you call it directly - see the link I passed earlier - it’s not it alone), so it doesn’t need to be compiled.

  • so if I remove the Rust from my computer the command ./user_sum_rust will continue working, but if I remove python the command ./user_sum_python.py will stop working?

  • 3

    In theory, yes. Of course there are other N factors. In fact you don’t even need to remove Py, if you take it out of the path indicated in the first line of your script already prevents direct execution. So much so that when you run the rustc it generates another file (without the rs extension) for you to run (you don’t run the source). py no, you are calling the original file.

  • 3

    I think we can start here: What is a scripting language and by the answer links here: Scripting language is always built on another language? - in theory, would answer the central problem of the question.

  • 1

    Worth reading about Shebang: https://pt.wikipedia.org/wiki/Shebang

Show 3 more comments

1 answer

0


When you use the chmod with the +x in some file, you are not necessarily turning it into executable. In fact you are saying that this file will now have permission to be executed.

In the case of your file .py, it is interpreted by the binary of the python why the first line of the file informs that this should happen: #!/usr/bin/env python. Also known asShebang.

The same way, if you trade for #!/bin/bash, this file will be executed by bash. Which will not know the python instructions and will give error. That is, the extension .py is not responsible for who will run the file, who does this is the instruction of the first line.

In relation to his example with Rust, in fact it is different to compare with Python. Since the Rust really takes your code and compiles it, giving you a new file (this yes we can call executable, for ease) that will no longer be processed by Rust, because it is already 'converted' to bytecode (machine language).

When you write compiled language codes, the execution is not of the code you wrote directly, but of the result of the compilation of that code by the language. Other than Python and of PHP for example, which are interpreted by the binary itself of the language.

This story goes very far in terms of when to use what and I can tell you that learning is super rich and valid.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.