Default file extension of the executable created by g++ under Cygwin vs Linux

放肆的年华 提交于 2019-12-04 04:43:50
Charlie Martin

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.

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

plinth

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

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.

Johannes Schaub - litb

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.

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!