问题
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
回答1:
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
.
回答2:
It's just a naming convention. On Unix/Linux, executables don't have an extension, just an executable bit.
回答3:
.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.
回答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.
回答5:
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.
回答6:
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++.
回答7:
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.
回答8:
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.
来源:https://stackoverflow.com/questions/359707/default-file-extension-of-the-executable-created-by-g-under-cygwin-vs-linux