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 Mar 30, 2009, 09: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, 10: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, 10: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, 12: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, 01: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, 05: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, 02: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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Compiling C Files in Xcode is not working - warning: no rule to process file Reini iPhone/iPad Programming 3 Feb 20, 2011 01:25 PM
Newbie with C++ Compile/Link problem PeteY48 Mac Programming 4 Jul 5, 2008 10:40 PM
Safari Download Linked File link problem bdoyle807 Mac Applications and Mac App Store 0 Mar 30, 2008 06:00 PM
Compiling .class files using terminal Mac Player Mac Programming 1 Sep 29, 2006 12:08 PM
Free Pascal Compiler - File Handling satans_banjo Mac Programming 5 Jan 10, 2006 08:48 AM


All times are GMT -5. The time now is 01:30 AM.

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

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