C++ Compiling Question

Discussion in 'Mac Programming' started by LD220, Jun 3, 2009.

  1. LD220 macrumors member

    Jun 3, 2009
    Hi. I have never programmed in C++ and am trying to use "SAMS Teach Yourself C++" book to get a general idea of how to use it. I have programmed using Java, but it been about 5yrs since I've used it.

    Anyway, I'm on the 8th lesson, which is about modules and I have copied the PromptModule.h and .cpp, ErrorHandlingModule.h and .cpp, and the main.cpp from the book. I am now trying to compile it in the terminal, but I keep getting errors. I've used g++ to compile when I just have one .cpp file, but can I use this for the implementation files, or is there a different command? Do I need to compile the header files? Is there a certain order in which the files need to be compiled?

    I would really appreciate any help you could give me. The book does not give any information on how to compile or what commands to use. Here's the link to the book site on google books if it helps http://books.google.com/books?id=4cNyHipV5G4C&pg=PA49&dq=0672324253#PPA51,M1
  2. rowsdower macrumors 6502

    Jun 2, 2009
    The header files should be included within the .cpp files that use them. I think you need to put all of the .cpp files on the command line like this:

    g++ file1.cpp file2.cpp file3.cpp -o myapp
    I haven't done this in a while though.
  3. sammich macrumors 601


    Sep 26, 2006
    As the post above:

    In your main.cc, you should have at the top:

    #include "yourfile.h"
    #include "yourfile2.h"

    To compile just use:

    # g++ -o progname main.cc
  4. jpyc7 macrumors 6502

    Mar 8, 2009
    Denver, CO
    This is just an example which might not work correctly on Mac OS. (I mostly program Linux.)

    g++ -I. a.cpp b.cpp main.cpp -L../lib -ljoe -o foo

    So the above will compile the three files that end with the .cpp extension. It has 4 options:
    -I. means look for header files (with the .h extension) in the current directory.
    -L../lib means look for library files (usually have .so or .a extensions) in a directory called "lib" at the same level as the current directory.
    -ljoe means link to the library joe (e.g. "libjoe.so")
    -o foo means the final program output is called "foo"

    foo would be the program that you want to run. If you did not specify the output file name, it would be "a.out".

    I would suggest you read the man page for g++. You probably don't need all the above options, especially if everything is in the same directory. It kind of depends on the compiler's default behavior.
  5. LD220 thread starter macrumors member

    Jun 3, 2009
    Thanks for the replies. My main.cpp file starts with the following three lines:
    #include <iostream>

    #include "PromptModule.h"
    #include "ErrorHandlingModule.h"

    I attempted to compile each cpp file separately, as well as all on one line and neither worked. When I tried just "g++ main.cpp" I received the following error message:
    Undefined symbols:
    "SAMSPrompt::pauseForAcknowledgement()", referenced from:
    _main in ccjzSBE3.o
    "SAMSErrorHandling::Initialize()", referenced from:
    _main in ccjzSBE3.o
    "SAMSErrorHandling::HandleNotANumberError()", referenced from:
    _main in ccjzSBE3.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

    When I tried to compile them all together with "g++ PromptModule.cpp ErrorHandlingModule.cpp main.cpp" I received this:
    Undefined symbols:
    "SAMSPrompt::pauseForAcknowledgement()", referenced from:
    _main in ccQWZJuz.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

    Can anyone make any sense of what's going wrong here? I'm very frustrated at this point.
  6. LD220 thread starter macrumors member

    Jun 3, 2009
    Ok, so I realized I made a mistake with the name of one of the methods I was using, which is what gave me the error when I tried to compile all three at once. When I try the "g++ PromptModule.cpp ErrorHandlingModule.cpp main.cpp -o main" command now, it works, so thanks for your help.

    I'm still a little confused though. The book says that modules are convenient because if you make a change you can just compile the file you changed, but this does not seem to be the case. When I try to compile just main, or just the other files, I receive errors. Any idea what's going wrong?
  7. jpyc7 macrumors 6502

    Mar 8, 2009
    Denver, CO
    To compile a single file (module) do this:
    g++ -c module.cpp
    That should create a file "module.o"
    You can do that for multiple module files.

    Then the next step would be:
    g++ module1.o module2.o main.o -o progname

    So when you change one module file, you can compile it with the "-c" option, but you still have to do the second step (called linking) to combine the files together.
    In fact, sometimes that last step can be written as:
    ld module1.o module2.o main.o -o progname

    where "ld" is the name of the program that does linking. Of course, "ld" might be an alias for "g++".

    As you have more and more modules, you'll eventually want to use a special tool to help you build your program (so you don't have to do so much typing). With C++, that tool is usually "make", but there might be other choices.
  8. LD220 thread starter macrumors member

    Jun 3, 2009
    Ok great. Thanks for that info. I tried it and it worked. Since you have to do the linking anyway and type out all the file names, is there really any point to compiling them separately with the g++ -c command?
  9. Sander macrumors 6502

    Apr 24, 2008
    Linking is faster than compiling + linking.

    For small projects, this usually doesn't really matter. For large ones, it's a huge convenience that only your changed files are compiled, and linked together with the results of a previous build. I'm talking minutes versus hours here.

    Especially when you use a tool like "make", you don't even need to remember which files to recompile. You just make your changes and type "make"; it will figure out which files are changed and what their dependencies are, and only do as much work as really needed.

Share This Page