Find the answer to your Linux question:
Results 1 to 5 of 5
hey.. im writing this small (trial) program to get used to working with header files and what they can do to ease finding problems faster.. so i made three files, ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60

    ar(1) to create an executable??

    hey..
    im writing this small (trial) program to get used to working with header files and what they can do to ease finding problems faster..
    so i made three files, file1.h that has a signature of a foo() function, file2.c that has the implementation of the foo() function, and file3.c that has the main() and calls foo()...of course, both file2.c and file3.c have #include "file1.h"
    anyway, i read around, and i did the following to create the executable file:
    Code:
    >gcc -c -g file3.c
    >gcc -c -g file2.c
    >gcc -o file3 file3.o file2.o -lm
    and then i have my executable file! and it worked..
    however, a proffessor i have at college told me that it can be done using the ar(1) call...so i typed man ar and it gave me all the information about ar(1), and it lost me coz it had so many options in it..so how can it actually be done with ar(1)??
    Last edited by techieMoe; 10-25-2007 at 06:04 PM. Reason: Fixed code tags

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Either your professor misspoke or you misunderstood him.

    ar cannot be used either to compile files or to link together files which have been compiled.

    It is useful in the linking stage, however. If you have source files (other than the source file which contains function main()) and you compile each of them individually with the -c option, you can use ar to take the resultant .o files and place them in an archive. Then when you compile the .c file which contains function main(), you can link it against this archive rather than against each of the .o files. ar is not actually run at this point; you simply name the archive file instead of each of the object files (other than the object file defining function main(); you still name that one separately).

    If you need to recompile a module whose object file is in the archive, you can then use ar to replace the old compiled object with the new one.

    It is customary to use the extension.a on the name of the archive file.

    The advantage of all this is not particularly speed, but (in the eyes of some) clarity of expression and organization.

    In your example, you'd link the files together thus:
    Code:
    gcc -o file3 file3.o mycollection.a -lm
    Now that you know that ar's only function is to manage a collection of files (in this case, object files), you can reread the ar man page with a far simpler goal in mind.

    Hope this helps.

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60
    well i read it again..and i tried it.. it seems that whatever way or order of options i try, it doesnt seem to be the correct way to type it in order to create the archive..so how can i create an archive before inserting object files into it?

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Hmm. The man page seems clear to me, and it would be unusual for your tar man page not to cover what you want. But to help narrow this down, please do two things.

    Thing one: to cover the possibility that you have an incomplete man page, please post your entire tar man page. To do so, do this first:
    Code:
    man tar | col -b > somefile.txt
    Then post the content of that file in this thread, within CODE markers as though it were code.

    I've yet to see a man page that's incomplete, but if it doesn't contain info on creating an archive, this would be a first.

    Thing two: please post exactly (copy and paste into this thread) what tar command or commands you've tried, along with the exact (copy and paste into this thread) error message or messages you received as a result.

    These two steps sound tedious, but they can help get to the root of the problem relatively quickly.

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    drl was kind and polite enough to point out to me privately what I must admit publicly: I have fat fingers.

    I meant ar wherever I said tar in the above post.

    Geez. I oughtta be tarred and feathered for that. ar ar

Posting Permissions

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