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?

Zanna
  • 70,465
  • 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
  • In Linux the file extensions don't mean much. You can call your compiled executable file anything like MyProg, MyProg.bin, or MyProg.I.Have.compiled.it.so.you.cant.tamper.with.it. It does not matter. – user68186 Sep 11 '18 at 16:18
  • 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
  • Thanks for all the replies , I am starting out with Linux and it is still a bit confusing and all the previous explanations I read did not realy tell me where to look for the compiled programs. Let's say for instance I wrote a program in Python. I see a .py file that is source. how do I compile it? – J.Cilliers Sep 11 '18 at 16:34
  • Linux does not use extensions such as .exe or .text or .pdf ro identify files, it uses magic. See https://askubuntu.com/questions/108600/how-does-ubuntu-know-what-file-type-a-file-without-extension-is – Panther Sep 11 '18 at 16:51
  • 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

1 Answers1

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.

rviertel
  • 759