c++ compiler problems

rdizz

Registered
Hello, I'm a newbie to programming. I've worked through a basic unix book, applescript and ruby(beginner). Now I've taken the plunge into a c++ class at my local community college, but I keep having trouble doing the stuff from Visual Studio in class to my Mac at Home. This is the first code we ran at school and the cin.ignore(99,'\n') keeps the command open to show the output on the command line, but it doesn't work for me on my mac. when I take out the cin.ignore(99,'\n'), the program runs, but I can't view the output. Is there any alternatives to this?

This is how my program looks:

#include<iostream>
using namespace std;

int main(void)
{
cout << "Hello, World!!!";
cin.ignore(99,'\n');
return 0;
}

I'll run the program in the terminal with
g++ filename.cpp
 
When you enter the following command:

Code:
g++ filename.cpp

All you're doing is compiling the file into an object file. You're not building an executable that you can run and view the output. Use the -o option with gcc to build an executable.

Code:
g++ -o HelloWorld filename.cpp

Now you'll have an application titled HelloWorld that you can launch and see "Hello World!" appear on the screen. Double-clicking the executable in the Finder will run the program in the Terminal.
 
Thanks so much szymczyk, it worked. So when I put in the -o option it builds an executable that is within my project folder (finder) that I can test to run as an executable and view the output. Is there any other way to run the executable from the terminal rather than double-clicking, now that the executable is made? Or is it the only way/easiest? Thanks again.
 
Yes, you can run the executable from the Terminal. When I was typing my first response, I wasn't 100% sure what you had to type to run the program for the Terminal (I don't run a lot of programs from the Terminal) so I mentioned the double-clicking from the Finder to avoid giving you wrong information.

To run the executable from the Terminal, launch Terminal, go to the location where the executable resides, and type the following:

Code:
./ProgramName

Where ProgramName is the name of the executable.
 
Thanks for the help, I actually remember learning how to open files in folder with that type of command in the terminal, thanks for refreshing my memory. From your response it sounds like there is another way to do this rather than from the terminal. For a while now I've been learning ruby and code in textmate, and then run it in the terminal. However, when I started the c++ class I didn't see a bundle in textmate for c++, so I just wrote the code in Xcode (c++ tool) and tried to execute the code in the terminal similar to how I was learning ruby, and started running into problems. Can I just do this all within Xcode and not really bother with the terminal? (I mean write the code, compile, make an executable and view the output all in xcode) I know I have lots of questions, but I also noticed on your link that you have a pretty cool book out, how would you recommend navigating from where I am to creating mac/iphone applications. I was planning on learning C++ then C-objective, and then how to use Cocoa/Xcode tools. At what point does the book you wrote become viable to a newb like me?
 
You have three options.

  1. Write your code in TextMate and compile from the Terminal.
  2. Create an Xcode project (C++ Tool in your case) and use TextMate to write the code and build the project. TextMate can build Xcode projects. You could run your program from Xcode or the Terminal.
  3. Do everything in Xcode.

If you go with option 3, I have an article on my website that walks you through the creation of a C++ Tool project.

With your knowledge of Ruby, you could start writing Mac applications now if you wanted. Xcode 3 has project templates that let you write Cocoa applications in Ruby. If you want to write iPhone applications using Apple's iPhone SDK, you'll have to learn Objective C. Your plan to learn C++, Objective C, then Cocoa will work fine.

My book complements Cocoa/iPhone programming books. It covers topics a programming book doesn't have space to cover, such as debugging, profiling, and version control. The book would help you after you learned some Cocoa and were ready to start writing your own Mac/iPhone applications.
 
When you enter the following command:

Code:
g++ filename.cpp

All you're doing is compiling the file into an object file. You're not building an executable that you can run and view the output.

No. When you like to only compile a file (for either to use several files, for example one per class and only compile the changed one), you use

Code:
g++ -c filename.cpp

Your example actually made an executable file, named a.out. a.out was the name of the executable format in older Unices; the name has keeped as the default name even if the OS X uses Mach-O.
 
Back
Top