Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 30, 2009, 10:18 AM   #1
muthuveerappan
macrumors newbie
 
Join Date: Aug 2008
C++ compile / link files

Hi,

I am a newbie to c++, I would like to compile / link c++ files:

game.h (header / interface file)
game.cpp (implementation of the header file)
use_game.cpp (a program file that uses the definitions of the functions in game.cpp)

How would I compile the above in the terminal ?

I used to use g++ for compiling simple (one source code file).

Thanks,
Muthu
muthuveerappan is offline   0 Reply With Quote
Old Mar 30, 2009, 11:26 AM   #2
Sander
macrumors 6502
 
Join Date: Apr 2008
GCC lets you provide multiple source files on the same command line, so you could simply do

Code:
g++ -o mygame -Wall game.cpp use_game.cpp
Sander is offline   0 Reply With Quote
Old Mar 30, 2009, 11:30 AM   #3
dydx
macrumors newbie
 
Join Date: Mar 2009
muthuveerappan,

Assuming the files are within the same folder. Substitute the desired name of the compiled executable for "gameApp" in following terminal command:
Code:
g++ -o gameApp game.cpp use_game.cpp
dydx is offline   0 Reply With Quote
Old Mar 30, 2009, 01:44 PM   #4
muthuveerappan
Thread Starter
macrumors newbie
 
Join Date: Aug 2008
Thanks a ton !!!!

Thanks Sander and Dydx

Works Great !!!

I was under the impression, that the game.cpp and use_game.cpp need to be compiled separately. Then the object code of the 2 files needed to be linked.

I think g++ does that in one step, correct me if I am wrong.
muthuveerappan is offline   0 Reply With Quote
Old Mar 30, 2009, 02:23 PM   #5
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by muthuveerappan View Post
I think g++ does that in one step, correct me if I am wrong.
It still does each step, but it does it for you with one command.

-Lee
lee1210 is offline   0 Reply With Quote
Old Mar 31, 2009, 06:57 AM   #6
muthuveerappan
Thread Starter
macrumors newbie
 
Join Date: Aug 2008
One more doubt

Thanks all for all your replies, really helped.

One more doubt:
---------------------
Header files (game.h) are in the directory $HOME/c++/header
Implementation files (game.cpp) are in the directory $HOME/c++/implementation
Application files (use_game.cpp) are in the directory $HOME/c++/application

How do I compile in this case ?
How can I tell the compiler that the header is present in a specific directory ?

Hope I am not being unreasonable.
Somehow the compiler knows where the predefined header files are though it is not in the same of the source directory
muthuveerappan is offline   0 Reply With Quote
Old Apr 6, 2009, 03:40 AM   #7
Sander
macrumors 6502
 
Join Date: Apr 2008
You can add directories to the header file search path with the -I command line switch.
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
Nike Twitter Store Link Detector - Auto Link Click/Auto Add To Cart Vizio Community Discussion 0 May 25, 2013 09:18 PM
How do I use an image to link link to another page in app? twn2 iPhone/iPad Programming 2 May 17, 2013 05:08 PM
Simple way to compile external linked files chrispholt Web Design and Development 1 Sep 10, 2012 04:07 PM

Forum Jump

All times are GMT -5. The time now is 05:22 PM.

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

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