views:

1005

answers:

7

Hi All,

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

+6  A: 

.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.

plinth
IIRC cygwin g++ actually defaults to .exe extensions, including the default 'a.exe' if no -o is given
Jimmy
+9  A: 

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

diciu
+17  A: 

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.

Charlie Martin
it also uses the info after the '.' to put print pretty colors in your shell.
contagious
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
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
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
+2  A: 

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.

contagious
+2  A: 

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
+3  A: 

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.

Johannes Schaub - litb
+1  A: 

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