Build An Linux Executable Using GCC

42,251

Solution 1

That executable is a "Linux executable" - that is, it's executable on any recent Linux system. You can rename the file to what you want using

rename a.out your-executable-name

or better yet, tell GCC where to put its output file using

gcc -o your-executable-name your-source-file.c

Keep in mind that before Linux systems will let you run the file, you may need to set its "executable bit":

chmod +x your-executable-name

Also remember that on Linux, the extension of the file has very little to do with what it actually is - your executable can be named something, something.out, or even something.exe, and as long as it's produced by GCC and you do chmod +x on the file, you can run it as a Linux executable.

Solution 2

To create an executable called myprog, you can call gcc like this:

gcc -c -o myprog something.c

You could also just rename the *.out file gcc generates to the desired name.

Solution 3

That is the executable. If you don't like a.out, you can pass an -o flag to the compiler. If the executable isn't marked with an executable bit, you need to do so yourself:

chmod u+x ./a.out
./a.out
Share:
42,251
Nathan Campos
Author by

Nathan Campos

Electrical Engineer, Ham radio operator, photographer, used to be a programmer.

Updated on July 21, 2022

Comments

  • Nathan Campos
    Nathan Campos almost 2 years

    I'm using Linux Ubuntu Intrepid Ibex and compiling C++ files with GCC, but when I compile, gcc makes a.out file, that is the executable, but how I can make Linux executables? Thanks!

  • Nathan Campos
    Nathan Campos almost 15 years
    The *.out file already is an executable, but how i can build a native Linux executable, linux native executables don't have extensions.
  • notnoop
    notnoop almost 15 years
    In linux, extensions don't matter. You can just rename the file to anything you like.
  • Nathan Campos
    Nathan Campos almost 15 years
    When the problem is permition i know. Thanks!
  • CesarB
    CesarB almost 15 years
    Doesn't gcc (in fact the linker it calls) already set the executable bit in its output file?
  • quark
    quark almost 15 years
    @CesarB: Yes, the gcc call makes 'your-executable-name' executable by default.
  • Keith Thompson
    Keith Thompson about 6 years
    Some systems might have a rename command, but mv is the usual way to rename a file.
  • Peter Mortensen
    Peter Mortensen about 2 years
    Conversely it may or may not set too much: -rwxrwxr-x.