Andreas Andreas - 1 year ago 432
Linux Question

gcc creates mime type application/x-sharedlib instead of application/x-application

Given the following C++ code 'mini.cpp':

#include "iostream"

using namespace std;

int main() {
cout << "Hello World" << endl;
return 0;

and the compiler command:

g++ -o hello mini.cpp

the result of

mimetype hello


hello: application/x-sharedlib

How do I get 'application/x-application' as a mime type?

I'm using gcc 6.2.0 on Kubuntu.

hvd hvd
Answer Source

gcc doesn't set the mime type. mimetype guesses the appropriate mime type based on the contents of the file. For ELF files (most compiled binaries and shared libraries), the header contains a field e_type which identifies its type. If it is ET_DYN, then mimetype will treat it as a shared library.

By default, gcc/ld will produce binaries which set e_type to ET_EXEC, which get detected as application/x-executable. When the command-line option -pie is used, a position-independent executable is created, which may, like shared libraries, be loaded at different addresses and still work. Because this works so much like a shared library, to avoid too many changes to the loader, such binaries get marked as ET_DYN, even though they can be executed directly.

Some Linux distributions, yours included, have set -pie as the default. It's still possible to override this with -no-pie, but the fact that the mime type is misdetected should not be seen as a bug, and unless you know what you're doing, you shouldn't override it. -pie allows for some extra security protections that are fundamentally incompatible with -no-pie.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download