Find the answer to your Linux question:
Results 1 to 4 of 4
Hi! I am a programmer that is new to the Linux environment, and I have a question regarding this. Suppose I have a hello.c file, I know it's compiled by: ...
  1. #1
    Just Joined!
    Join Date
    Nov 2011
    Posts
    12

    Question about building in linux & object files

    Hi!

    I am a programmer that is new to the Linux environment, and I have a question regarding this.
    Suppose I have a hello.c file, I know it's compiled by:

    gcc -c hello.c

    which in turn creates an object file "hello.o", and then when I do

    gcc -o Hello hello.o

    It creates a file without any extension, called "Hello". When I say "file Hello" and "file hello.o" to learn the file types, it says:

    "Hello: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped"

    and

    "hello.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped"

    Now, I thought that the "hello.o" object file was the ".exe" of Windows programs, that I could run it, but what is "Hello"?
    Besides, when I say

    gcc -o Hello hello.c directly without doing the compilation first, it doesnt give any "hello.o" file but directly a "Hello" executable. Why is this and what are the differences between "hello.o" and "Hello"?

    Another question is what does "not stripped" mean?

    Thanks!

    Can

  2. #2
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    121
    Quote Originally Posted by ccb85 View Post
    Now, I thought that the "hello.o" object file was the ".exe" of Windows programs, that I could run it, but what is "Hello"?
    Nope - hello.o is an object file (an ELF relocatable, as 'file' says). It contains only the information in the source unit it was built from (i.e. hello.c) so doesn't contain information about where it needs to get external symbols like printf etc. from.

    "Hello" is the executable/.exe equivalent - it contains the information the OS needs to be able to locate your program correctly in memory, locate any necessary shared libraries and find an entry point to start your program. It's formed from some object files, including those you supply on the command-line and some start-up ones the compiler provides, which get your program from _start (your program's real entry point) to main().


    Quote Originally Posted by ccb85 View Post
    Besides, when I say gcc -o Hello hello.c directly without doing the compilation first, it doesnt give any "hello.o" file but directly a "Hello" executable. Why is this and what are the differences between "hello.o" and "Hello"?
    It does produce a .o file, it just makes it a temporary file and discards it unless you tell the compiler not to with -save-temps (which will also keep the preprocessed source and assembler output).


    Quote Originally Posted by ccb85 View Post
    Another question is what does "not stripped" mean?
    An executable has symbols that allow you to see where bits of code and (global) data are from the outside - stripping an executable is the act of removing these symbols. This means you can't debug the executable, but it makes it smaller.


    If you want more info, I wrote a few blog posts about this sort of stuff a while ago:

    Linux Programming Internals

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    A question that you didn't ask:

    Why would we want to create object files? What purpose do they serve if just doing the direct compilation produces the same output?


    Imagine that we have an executable called hello that is compiled from three source files: one.c, two.c, and three.c. Now we change two.c.

    If we use object files, we need to recompile two.c:
    Code:
    gcc -c two.c
    and then recompile the executable using the two pre-existing object files and the new one:
    Code:
    gcc -o hello one.o two.o three.o
    If, however, we don't use object files, this simple change to two.c means that we must recompile ALL of our source files, even though two of them did not change.

    For small programs, this doesn't really matter, so you can do it however you want (though do note that standard utilities like automake use object files no matter what). However, for large files, making it so that changes only require recompiling the affected files, and not all files, can drastically speed up your build process.
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Just Joined!
    Join Date
    Nov 2011
    Posts
    12
    Thanks a lot guys..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...