Find the answer to your Linux question:
Results 1 to 5 of 5
gcc -o options query hi all, I am using ubuntu 11.04 within vmware. I have a following query. for running and executing a C code , i can do following ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2011
    Location
    dallas ,texas
    Posts
    18

    gcc -o options query


    gcc -o options query
    hi all,
    I am using ubuntu 11.04 within vmware.
    I have a following query.

    for running and executing a C code , i can do following
    1)
    Compile but not link:
    Code:
    gcc c  filename.c
    Link using the object file:

    Code:
    gcc c filename filename.o
    2) Compile and link together :

    Code:
    gcc o  filename  filename.c
    however, i tried the following

    Code:
    gcc o  filename.o  filename.c
    this works, and creates a executable with .o extension. why? it should be a object file? though i did get 2 warnings regarding malloc ( which i was using in this code) when i gave the above option, but when i run

    Code:
    ./filename.o
    it gave me correct output. why? any help?

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,211
    My guess is that gcc has both compiled and linked this program, but has slavishly given you the filename you asked for, even though the .o suffix is not appropriate for an executable. You can check by running the file command on it. An unlinked object file is reported as ELF 32-bit LSB relocatable, whereas a linked executable file is ELF 32-bit LSB executable.
    "I'm just a little old lady; don't try to dazzle me with jargon!"
    www.hrussman.entadsl.com

  3. #3
    Just Joined!
    Join Date
    Jun 2011
    Location
    dallas ,texas
    Posts
    18
    hi, i checked and filename.o is also a 64 bit executable. i guess compiler skips the .o and treats it as full name. for example, sometimes , by mistake, if u save a firl abc.jpg as abc.d.jpeg,
    it still saves as jpeg as abc.d is treated as full name.

  4. #4
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,211
    Quote Originally Posted by sed_y View Post
    hi, i checked and filename.o is also a 64 bit executable. i guess compiler skips the .o and treats it as full name. for example, sometimes , by mistake, if u save a firl abc.jpg as abc.d.jpeg,
    it still saves as jpeg as abc.d is treated as full name.
    Yes, that's correct. In Unix systems like Linux, filenames can legally contain several periods. For example archive files that are assembled with tar and then compressed with gzip have names ending in ".tar.gz". But also Unix OS's attach less importance to suffixes than Windows does. Linux tends to identify file types by "magic numbers" (byte sequences that reliably occur at the beginning of certain types of file) rather than by suffixes.

    Gcc is very sensitive to the suffix of its input file because that tells it what language the file is written in, but your experiment suggests that it doesn't take much notice of the suffix you give the output file. If you don't use the -c option, you get a linked executable, whatever you call it.
    "I'm just a little old lady; don't try to dazzle me with jargon!"
    www.hrussman.entadsl.com

  5. #5
    Just Joined!
    Join Date
    Jun 2011
    Location
    dallas ,texas
    Posts
    18
    yes,I guess that is the way it is.
    so, we have a consensus

Posting Permissions

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