c++linuxunixsolarissunstudio

difference between -h <name> and -o <outputfile> options in cc (C++)


I am building .so library and was wondering - what is the difference b/w -h and -o cc complier option (using the Sun Studio C++) ?

Aren't they are referring to the same thing - the name of the output file?


Solution

  • -o is the name of the file that will be written to disk by the compiler

    -h is the name that will be recorded in ELF binaries that link against this file.

    One common use is to provide library minor version numbers. For instance, if you're creating the shared library libfoo, you might do:

    cc -o libfoo.so.1.0 -h libfoo.so.1 *.o
    ln -s libfoo.so.1.0 libfoo.so.1
    ln -s libfoo.so libfoo.so.1
    

    Then if you compile your hello world app and link against it with

    cc -o hello -lfoo
    

    the elf binary for hello will record a NEEDED entry for libfoo.so.1 (which you can see by running elfdump -d hello ).

    Then when you need to add new functions later, you could change the -o value to libfoo.so.1.1 but leave the -h at libfoo.so.1 - all the programs you already built with 1.0 still try to load libfoo.so.1 at runtime, so continue to work without being rebuilt, but you'll see via ls that it's 1.1.

    This is also sometimes used when building libraries in the same directory they're used at runtime, if you don't have a separate installation directory or install via a packaging system. To avoid crashing programs that are running when you overwrite the library binary, and to avoid programs not being able to start when you're in the middle of building, some Makefiles will do:

    cc -o libfoo.so.1.new -h libfoo.so.1 *.o
    rm libfoo.so.1 ; mv libfoo.so.1.new libfoo.so.1
    

    (Makefiles built by the old Imake makefile generator from X commonly do this.)