How To Compile .cpp Files On Mac

Joined
Jan 31, 2009
Messages
49
Reaction score
0
Points
6
I downloaded g++ and put

Code:
g++ password.cpp -o RandomPswrd

^ this in the termional but I get a bash error. The .cpp is in the gcc-4.40 folder. I just dragged the password.cpp into the folder. How can I compile these files?
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
do you get a specific bash error?
 
OP
S
Joined
Jan 31, 2009
Messages
49
Reaction score
0
Points
6
-bash: g++: command not found

???

That's after I put in the code the RandomPswrd can be anything right?
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
That error usually shows up if either:

1) The path to the executable is not in the exported path
2) The software hasn't been installed.

Just out of curiosity, have you installed g++, or the xcode developer pack? AFAIK, installing OSX by itself doesn't install the compilers. I think a version of XCode may be on the OSX disc that you can install, but you might as well get the most current version from apple and register as a developer. It's free and once properly installed, you should have no problems.
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
GCC/G++ has to be compiled (the irony).

Download the developer tools from the Apple developer site or install Xcode/dev tools from your OS X installation disc to get an installed binary of GCC. Your best bet is to get it from the Apple dev site though as Xcode will be a newer version there.
 
OP
S
Joined
Jan 31, 2009
Messages
49
Reaction score
0
Points
6
bump

I downloaded xcode but now I get

i686-apple-darwin9-g++-4.0.1: password.cpp: No such file or directory
i686-apple-darwin9-g++-4.0.1: no input files
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
so is password.cpp in the directory you're running the compile from?
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
you have to be in the directory your source is in to compile it.

So, for instance.. if your password.cpp was in your home directory under a folder called source.. you'd have to

cd ~/source
g++ password.cpp -o RandomPasswd

for it to compile. Or, you'd have to specify the path to the source like

g++ /path/to/password.cpp -o RandomPasswd

It can't guess where your source is.
 
OP
S
Joined
Jan 31, 2009
Messages
49
Reaction score
0
Points
6
Ok, so I put a folder called source in finder > steven (right below "desktop") and inside the source folder is the password.cpp and now I get this:

password.cpp: In function ‘int main()’:
password.cpp:8: error: ‘srand’ was not declared in this scope
password.cpp:18: error: ‘rand’ was not declared in this scope
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
Now you have programatic errors ;)

Try gdb password.cpp
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
I'll give you a hint - you're missing an include file
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Read here and here - those may help.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top