1

I know a lot of answers have been written about this but they all seem to be about how every file in Linux (even text files) are executable.

What I want is to compile code into machine code similar to a Windows .exe so that no one can tamper with my code. I'm not interested in interpreters either. Is there a straightforward answer to this?

  • 1
    Sure. Write your code in some compilable langugage, like C or C++ and have gcc (the compiler) and ld (the linker) create a binary for you. – PerlDuck Sep 11 '18 at 16:11
  • 1
  • 1
    For the question as stated in your title, see Executable and Linkable Format – steeldriver Sep 11 '18 at 16:20
  • 4
    I think you may be asking the wrong question in your title, and I suspect your real question is too broad. If you tell us what you're actually trying to achieve that might make this more answerable. As Perl Duck pointed out, there's nothing Windows-specific about compiled executables - lots of parts of a Linux system are written in compiled languages. If you want to "tamper" with the code of a program, you typically have to download the source code (and recompile it). What's special about Linux is that anyone is allowed to do that. Making source code available is central to the Linux ethos. – Zanna Sep 11 '18 at 16:25
  • 1
    Python is essentially an interpreted language (that's one of its appealing features) however if you really want to compile it see How to compile a python file? – steeldriver Sep 11 '18 at 16:54
3

In simple terms, what you are looking to create is a binary file, one that does not store text but is essentially a list of instructions to the Linux kernel to execute some kind of program.

Typically, these files are created by a program called a compiler. Common examples of programming languages where code is compiled to create binary executables include C, C++, and Fortran. gcc and gfortran are the corresponding programs that can turn your code into a binary executable. While it is possible to create a binary file from Python, this is not the norm.

In Linux any file can be marked as an "executable" and unlike Windows, the file extension has no bearing on its executability or its content (i.e. whether it is a binary or text file, or whether it contains C or Python code) although it can serve as a reminder. The command chmod allows you to change the read, write and execute permissions of a file with any extension. When you compile code with gcc or gfortran on a typical Linux system, it will automatically be marked as executable.

Not the answer you're looking for? Browse other questions tagged or ask your own question.