Thread: GCC Beginner - Quick Question

  1. #1
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382

    GCC Beginner - Quick Question

    How do you compile multiple files into one project under gcc?
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    There are actually a couple ways to do it, the one I am most familiar with is compiling each source file by separately, then linking them all (and any libraries) together at the end, such as

    gcc -c file1.c
    gcc -c file2.c
    gcc -c file3.c
    gcc -o program file1.o file2.o file3.o

    The -c option tells the compiler not to do the link phase, simply produce the object code. you can then pass the object code files to gcc, and tell it where the resulting binary should be named with -o

    I believe the common reason to do this, is one make files can expand a list of files into multiple lines consisting of that each of the files in the place, and order needed. as well as when doing long builds, if the build fails, you have all the object code from the good files, while you can view the error, fix it, and continue the build without having to completely start from scratch compiling.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  3. #3
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Code:
    gcc -o app.exe file1.c file2.c file3.c
    ?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    thats another option, and will still tell you any error information, but for larger projects, the method I mentioned above is much more common.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually for larger projects a make file is more common.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, you could use a Makefile like this one:
    Code:
    program.exe: source1.o source2.o sourceN.o
    <TAB>gcc -o program.exe source1.o source2.o sourceN.o
    The <TAB> means a tab: I didn't actually insert a tab because a tab looked too much like eight spaces. Spaces don't work.

    Then just use the following command
    Code:
    C:\>make
    and make will compile only the files that need compiling.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  2. Quick Question on File Names and Directories
    By Kyoto Oshiro in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2002, 02:54 AM
  3. * quick question *
    By SavesTheDay in forum C Programming
    Replies: 3
    Last Post: 03-27-2002, 06:58 PM
  4. just a reaaaaaaly quick question plz help
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2002, 11:39 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM