Thread: gcc or g++ Question

  1. #1

    gcc or g++ Question

    I'm working through a Linux programming book, however the first half is all on shell scripting, the latter half is on gcc/g++ and make files. I glanced at the gcc part and learned that I can do a simple compile like so:

    gcc -o newname filename.cpp

    Well, I want to read this book sequentially, however can someone quickly help me out and tell me what the command line option is for building a project with multiple files: Say I have these files:


    How would I compile this with gcc. I glanced at the chapter on make files. I don't want to get into that just yet. What is the long solution?

    gcc ?

    Yes I am lazy. I didn't even try to anything. Just give me the answer anyway!

  2. #2
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    I assume that those headers are only headers and they are already #included in the files?

    gcc -c source1.c
    gcc -c source2.c

    Now you have source1.o and source2.o

    gcc -o program source1.o source2.o

    If you have two source files then Makefile would speed up your developing process, because it compiles only those files which have changed since last compilation. Here is a suitable Makefile for this:
    # Makefile
    # <tabs>('\t') are replaced with 8 spaces
    program: source1.o source2.o
            gcc -o program source1.o source2.o
    source1.o: source1.c
            gcc -c source1.c
    source2.o: source2.c
            gcc -c source2.c
    # Makefile ends
    I am not using Dev-C++.
    #!/usr/bin/env python
    import sys;file=open(sys.argv[0]);print;file.close()

  3. #3
    I saw something like this in the book, but I was skipping ahead too far and didn't know if I was missing something. I was wondering if the .h file needed to be specified, however I see that it just needs to be found in the sources and that it will be preprocessed. I'll test it out soon.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > I was wondering if the .h file needed to be specified
    Yes they do (if the makefile is going to work all the time)

    For instance
    source1.o: source1.c pet.h

    This would tell make to recreate source1.o if either (or both) source1.c or pet.h has changed since source1.o was last updated.

    In this example, I've assumed that source1.c includes pet.h

    You should really list all the include files each .c file has on the dependency line

    Fortunately, the command
    gcc -M source1.c
    Does most of this work for you (there are other options like -MM, see your manual )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question - linux - gcc - c standards
    By AMAMH in forum C Programming
    Replies: 12
    Last Post: 12-03-2009, 02:49 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Gcc can't find obvious copy constructor
    By SevenThunders in forum C++ Programming
    Replies: 13
    Last Post: 03-19-2009, 02:41 PM
  4. Quick Compilation Question
    By chacham15 in forum C Programming
    Replies: 10
    Last Post: 10-12-2008, 08:15 PM
  5. Question about linux compiling with GCC
    By elsheepo in forum C++ Programming
    Replies: 23
    Last Post: 03-07-2008, 11:36 PM