Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 3, 2009, 06:39 PM   #1
LD220
macrumors member
 
Join Date: Jun 2009
C++ Compiling Question

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=4cN...24253#PPA51,M1
LD220 is offline   0 Reply With Quote
Old Jun 3, 2009, 07:01 PM   #2
rowsdower
macrumors 6502
 
Join Date: Jun 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:

Code:
g++ file1.cpp file2.cpp file3.cpp -o myapp
I haven't done this in a while though.
rowsdower is offline   0 Reply With Quote
Old Jun 3, 2009, 07:10 PM   #3
sammich
macrumors 601
 
sammich's Avatar
 
Join Date: Sep 2006
Location: Sarcasmville.
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
__________________
Official MR IRC
Look at me, I'm own a bunch of things and everyone must know about every single one.
sammich is offline   0 Reply With Quote
Old Jun 3, 2009, 07:12 PM   #4
jpyc7
macrumors 6502
 
Join Date: Mar 2009
Location: 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.
jpyc7 is offline   0 Reply With Quote
Old Jun 3, 2009, 07:19 PM   #5
LD220
Thread Starter
macrumors member
 
Join Date: Jun 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.
LD220 is offline   0 Reply With Quote
Old Jun 3, 2009, 07:33 PM   #6
LD220
Thread Starter
macrumors member
 
Join Date: Jun 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?
LD220 is offline   0 Reply With Quote
Old Jun 3, 2009, 07:41 PM   #7
jpyc7
macrumors 6502
 
Join Date: Mar 2009
Location: Denver, CO
Quote:
Originally Posted by LD220 View Post
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?
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.
jpyc7 is offline   0 Reply With Quote
Old Jun 3, 2009, 08:27 PM   #8
LD220
Thread Starter
macrumors member
 
Join Date: Jun 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?
LD220 is offline   0 Reply With Quote
Old Jun 4, 2009, 04:05 AM   #9
Sander
macrumors 6502
 
Join Date: Apr 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.
Sander is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Need help compiling c files Paulie87 Mac Programming 27 Mar 11, 2014 08:00 PM
Compiling for Powerpc, how to? skinniezinho PowerPC Macs 12 Jun 21, 2013 04:10 AM
Cross compiling DenBeke Mac Programming 8 May 18, 2013 02:08 AM
compiling VTK using Xcode ilantal Mac Programming 7 Jan 2, 2013 10:43 AM
C++ compiling issues Marcus89 Mac Programming 2 Sep 20, 2012 08:24 AM

Forum Jump

All times are GMT -5. The time now is 09:58 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC