Question:
Coding in C++, using Eclipse, How do I start writing code?!?
Deedsy
2012-05-22 08:43:18 UTC
First - I am a total n00b. Like seriously, 17yo and starting my coding career.
I'm using Eclipse for Mac OS X and I downloaded something to allow me to code in C++, but I dont know how to run my first program.
It comes up with an error saying:
"The selection cannot be launched, and there are no recent launches."
Halp me :(
Four answers:
Unca Alby
2012-05-22 09:07:59 UTC
First things first. Make sure you download the CDT edition of Eclipse, as it's already configured to handle C and C++.



http://www.eclipse.org/cdt



Second things second, make sure you download Xcode from Apple to get a C++ compiler. Eclipse does not come with a compiler. I will assume you have the compiler.



When you start Eclipse, you'll have an option to select a workspace. It will default to something like your user-name, documents, workspace. Slight differences depending on Windows, Linux, or Mac (I use Windows), but the last folder will be "workspace". Change that to have a new sub-folder named for your project. Eclipse gets confused (and confusing) if you try to have more than one project in a workspace.



E.g., your first project is likely to be the notorious "Hello World", so create a "workspace/hello" or some such.



Next, lose the "Welcome" page. Mostly useless, unless you want links to the overview, tutorial, etc.



Now, create your project. File->New->Project...

Select C++ Project (of course), then choose Mac, or Xcode, or whatever options there are for an Empty Project. (again, mine is Windows -- the process is similar, but not identical) Give the project a decent name, say, "Hello".



If it asks if you want to use the "C++ Perspective", go ahead and say yes.



Now you've got a project, but you need to add compilation units. Files to be compiled. Right-click the "Hello" project, (oh wait, this is Mac -- well, you're familiar with Mac, do the Mac equivalent, whatever it is) Anyhow, you want to add a New Source File.



Name it something with a ".cpp" extension, say, "Hello.cpp".



Now you have a source editor up. Add the traditional "Hello World" code:



#include

using namespace std;

int main() {

cout << "Hello world\n";

}



Save the file, then select Menu Option, Project->Build All



Now you need to create a "Run Configuration". Select Run->Run Configurations...



Select "C/C++ Application", the click the "New" button (which looks like a page with a plus sign)



You'll probably get a "Debug" configuration, which is cool. The defaults are cool too, just click the "Run" button. The program should run, and you'll see "Hello world" in the console window.



Now, any time you make changes to this file, save it, build it, you can select "Run->Run", or "Run Last Configuration", or whatever it says.



I think the Mac uses the "Command" key (with the funny symbol) where Windows will use the "Ctrl" key (labeled as such). So, a shortcut is:



Edit the file

Command S to save

Command B to build

Command F11 to run



That ought to be enough to get you started.
2012-05-22 16:03:15 UTC
I'm guessing you have your reasons to not use XCode. So I'm going to answer as if you've decided it isn't for you already.



For a beginner I'd definitely advise Code::Blocks over Eclipse. Eclipse is a powerful tool, but with that power comes a lot of options and tools that can be overbearing for a novice, and most are only really useful for a small handful of experts.



Added



No matter what IDE you're using, when using C++ on Macs make sure it's pointed towards the real GCC and not the llvm-gcc. Clang and llvm are great and all, it's what I'm using for my own hobby OS project, but it's C++ support isn't quite there yet.
?
2012-05-22 16:01:58 UTC
Eclipse can be temperamental... For C++ I recommend codeblocks or better yet since you're on a mac get Xcode (it's free too).
Damianek
2012-05-25 23:28:59 UTC
Use code::blocks or dev c++, code::blocks is better option


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...