Thread: compiling and linkning using gcc

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    10

    compiling and linkning using gcc

    Hi,
    I have a question about compiling and linking using gcc:

    What does a file name with a suffix '.a' represent?

    If I have multiple .c and .h files, how can I link them together?
    How can I create the .a file?

    The articles I read were broad and ddnt target the above questions.

    Thanks.

  2. #2
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    .a = static object code library.
    Basically it's a bunch of object files compressed into one archive.

    You can compile and link multiple .c files into an executable using:
    gcc one.c two.c three.c -o main.exe
    (Note: Make sure only one of them have a main function)
    (Double Note: You don't need to put the .h files here, they are included in your c file, and thus do not need to be "compiled" or "linked")

    To make an object file, just do
    gcc -c one.c
    -c says to generate an object file.

    To make a static object code library (or .a), you need an archiving utility like ar - Linux Command - Unix Command
    Last edited by Phenax; 03-13-2011 at 12:23 AM.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    10
    Thank you. It worked. I appreciate it.
    Question: Do i need to add a .exe prefix (main.exe). is it enough to just leave it (main).

  4. #4
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by MASX View Post
    Thank you. It worked. I appreciate it.
    Question: Do i need to add a .exe prefix (main.exe). is it enough to just leave it (main).
    If you are on Windows: You need the exe prefix. Windows uses file extensions to determine filetype I believe.
    If you are on a Unix-like operating system (Linux, *BSD, etc): You can name it anything you want. If it won't execute, try setting the executable flag (chmod +x filename).
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Phenax View Post
    If you are on Windows: You need the exe prefix. Windows uses file extensions to determine filetype I believe.
    Yes Windows actively uses the suffix (a "prefix" comes before) also called an Extension to determine what to do with a file. Only certain types (.exe, .com) are directly executable. The remainder are processed by the "file associations" system to know which executable to load in order to deal with the file.

    It's pretty cool really... on a properly configured system you can run the whole computer just by clicking on files...
    The only exception being that you usually need to launch the executable to create a new file.
    Last edited by CommonTater; 03-13-2011 at 05:34 AM.

Popular pages Recent additions subscribe to a feed