9

I've done most of my work on VisualStudio and don't have much experience with gcc or g++. When I tried to compile a (ex. aprogram.cpp) this morning on my pc using cygwin, I got (aprogram.exe) when I tried to compile the same thing on my Ubuntu box I got (aprogram) w/o any extension. I am just wondering if someone be kind enough to tell me why. This question is just out of curiosity. :)

Thanks in advance!

EDIT: (from Jimmy's comment) g++ under Cygwin defaults to .exe

41104
  • 225
  • 1
  • 4
  • 13

8 Answers8

18

That's easy: on UNIX, you don't need no steenkin' extensions. In fact, an "extension" like .c is just a convenient naming convention; unlike Windows, the file system sees the file name as one string, .c and all.

For a really good time, compile a C program with no -o flag at all. Your executable will still show up --- named the default name for executables: a.out.

Rob
  • 47,999
  • 5
  • 74
  • 91
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • it also uses the info after the '.' to put print pretty colors in your shell. – helloandre Dec 11 '08 at 15:35
  • MS-DOS actually stored file names as an 8 character name and a 3 character extension. Windows 95 and up treated the file name as one long string. Unfortunately, old habits can be hard to break. You can still see people using *.* to mean all files when the correct wildcard is * – Ferruccio Dec 11 '08 at 16:47
  • Almost - even on NTFS, a final "." on a filename is ignored/removed (in the same way that case is ignored), for no discernable good reason. – Steve Jessop Dec 11 '08 at 22:38
  • No, a final period is *not* removed on NTFS. It is removed by the Windows shell (explorer.exe) for no good reason - NTFS as such (i.e. the Microsoft NTFS driver and all alternatives,including ntfs-3g) does not remove anything. Please be clearer. – Mihai Limbășan Jan 05 '09 at 06:21
  • Actually, it's pretty fun to mess with NTFS. For example, you can put on a Linux LiveCD, write files called for example `NUL`, and it will be off-limits to Windows :) Also, you can have mixed upper- and lower-case on NTFS. – Camilo Martin Aug 13 '12 at 19:52
13

It's just a naming convention. On Unix/Linux, executables don't have an extension, just an executable bit.

diciu
  • 29,133
  • 4
  • 51
  • 68
7

.exe is a windows thing. Unix doesn't care about extensions. Executability is based on metadata on the file as well as the file's contents. g++ through cygwin is not really a windows app, so it carries its unix roots with it.

Lawrence Dol
  • 63,018
  • 25
  • 139
  • 189
plinth
  • 48,267
  • 11
  • 78
  • 120
  • 1
    IIRC cygwin g++ actually defaults to .exe extensions, including the default 'a.exe' if no -o is given – Jimmy Dec 11 '08 at 15:56
4

If you were wondering how to execute the program on UNIX, simply navigate to the folder with your program you wish to execute (aprogram) and type

./aprogram

This will tell the shell you wish to execute 'aprogram' in the current directory.

helloandre
  • 10,541
  • 8
  • 47
  • 64
4

Executables have no extension in the unix world, because they are meant to be executed in the shell. Imagine the following:

cat.bin file.txt | less.bin

That's ugly! Unix makes use of so-called magic bytes at the start of each file to detect the file-type. For the default binary format, called ELF, there is a 4 byte word 7f 45 4c 46 at the start. That's not possible for all file formats. Consider C code or Java code. They can both start with comments, and can be made look exactly the same. So you still have to use file-name extensions, and it's a good thing when used where it's appropriate.

BCS
  • 75,627
  • 68
  • 187
  • 294
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • 1
    Well, you don't need to type .exe in Windows either... Good point on the ELF header though. – Macke May 18 '11 at 12:59
2

Honestly, just name them .elf. And if you're not sure what file type they are, execute:

$ file MyFile

This will tell you what are the contents of the file, and you can pick a name this way, but it's not necessary - just cosmetic if you have been used to extensions all life.

Camilo Martin
  • 37,236
  • 20
  • 111
  • 154
2

If you want the output to have an .exe extension then just use the -o flag to do so (e.g. -o aprogram.exe). It will work just fine under linux either way.

The ability to execute a program under linux is based on the file's permissions (see chmod). Execute permissions will be automatically set by gcc/g++.

Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99
1

ls /bin There are lot's of programs and all of them without extension :)

ls -l /bin you will see that all of them has +x flag to mark them as an executable.

Malx
  • 990
  • 1
  • 9
  • 16