#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0

    Noobie question here on object files.


    Mkay, if you didn't know I really don't understand file extensions well. I don't know a whole lot of them.

    [CODE]
    #include<stdio.h>

    void encrypt(char *message)
    {
    while(*message != 0)
    {
    *message ^= 31;
    message++;
    }
    }

    int main()
    {
    char word[] = "Hello";
    encrypt(word);
    printf("%s",word);
    getchar();
    return 0;
    }

    Say this is my file called encrypt.C. If I wanted to convert this to an object file, would I say encrypt.o and just change the name?

    Also, can someone tell me more in depth what object files are used for? I looked it up online and said it's used for part of the linker so that then all the object files can combine together and make an executable file. Object files have machine code in them. Is that all there is to know about them?

    How would I convert this to an object file? No, I don't have a gcc
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    Mkay, if you didn't know I really don't understand file extensions well. I don't know a whole lot of them.

    Code:
    #include<stdio.h>
    
    void encrypt(char *message)
    {
    while(*message != 0)
    {
    *message ^= 31;
    message++;
    }
    }
    
    int main()
    {
    char word[] = "Hello";
    encrypt(word);
    printf("%s",word);
    getchar();
    return 0;
    }
    Say this is my file called encrypt.C. If I wanted to convert this to an object file, would I say encrypt.o and just change the name?

    Also, can someone tell me more in depth what object files are used for? I looked it up online and said it's used for part of the linker so that then all the object files can combine together and make an executable file. Object files have machine code in them. Is that all there is to know about them?

    How would I convert this to an object file? No, I don't have a gcc
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Any decent compiler will be able to generate object files.
    For GCC, it would be gcc -c prog.c
    For Microsoft, it would be cl /c prog.c

    If you're just compiling a single file on the command line, then the object file is usually deleted.
    In other words, something like gcc -o program prog.c

    But you could also do this
    gcc -c prog.c
    gcc -o program prog.o

    With a large project (with many source files), and an intelligent build system, the object files are usually kept for later use. This is to reduce build times.

    > Object files have machine code in them. Is that all there is to know about them?
    Well not just machine code. They also contain symbol information, like the names of your functions, for use by the linker, and some debug information as well (for use by the debugger).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Read up on it; eg at http://en.wikipedia.org/wiki/Object_file.

    A compiler generates object files as an intermediate step in the creation of an executable. As you already noted, the linker will take the object files created in a project and library files and combine them together to generate an executable. In order to do that, the linker needs not only object code, but also a lot of other information. Object files contain that information in addition to object code. For example, from that Wikipedia article:
    Types of data supported by typical object file formats:
    - Header (descriptive and control information)
    - Text segment (executable code)
    - Data segment (static data)
    - BSS segment (uninitialized static data)
    - External definitions and references for linking
    - Relocation information
    - Dynamic linking information
    - Debugging information
    There are many different file formats for object files, just as there are different file formats for executable files. To learn exactly what your object files' format is, you will need to learn what format your particular compiler generates.

    Also, the file extension for an object file can vary. With gcc, the extension is .o, but in MS-DOS and Windows, it's .OBJ. Similarly, library files, which can be thought of as a special type of object file, use the extension .a with gcc, but .LIB with MS-DOS and Windows.

    File extensions are actually arbitrary, though several conventions exist. The designers of application program choose what file extensions their program will recognize and what format those files are to have. While that choice is arbitrary, application designers tend to follow certain conventions. You learn file extensions by learning what file extensions your application uses and how it uses them. You could also read lists of file extensions; eg at http://en.wikipedia.org/wiki/List_of_file_formats.

    In order to generate an object file, simply compile it. It seems that MinGW gcc cleans up after itself and delete the object files, so you would use the -c option to generate the object file, but not link it. With Visual Studio, look in the project's debug or release directory under the source files' directory and you should find them there.

IMN logo majestic logo threadwatch logo seochat tools logo