Creating a New Project in Your IDE

Last modified: Jan 21, 2019
Contents:

IDEs are generally organized around the idea of a “project”, a collection of files that, somehow or other, will get turned into one or more programs.

Let’s try, however, to head off one popular misconception before we get started.

A project is not a folder or directory.

When you create a project, you will be asked to choose a directory. We may call this the “project directory”. But that directory isn’t your project.

A source code file will only be treated by the IDE as a part of your project if you tell the IDE to do so.

In the remainder of this document, we’re going to look at how to create new C++ projects in both Code::Blocks and Eclipse. We will look at two common cases: 1) creating a new project “from scratch”, and 2) creating a new project based on some already existing source code files.

Some IDEs will allow you to use them to edit source code files without creating a project first. But doing that is usually a mistake.

If you don’t set up a project first, you will probably be unable to compile the source code that you have prepared.

1 New Projects in Code::Blocks

1.1 Creating a Project From Scratch

Let’s start out with a very simple project. Select “File->New->Project.” Select “Console Application” and click “Go”. Move through the Wizard, choosing C++ as the language. Give the project the title “FirstProject”, click on the “…” button to choose a place in which to save your work.

Accept the rest of the defaults until the wizard closes.

On the left you can now see your project structure. If you open the Sources folder, you can see that it has already created a basic main.cpp file for you. Double-click this to open it in the editor.

Let’s make some changes before compiling this. Change the program to look like this:

#include <iostream> 
#include <string> 

using namespace std; 

int main() 
{ 
  string greeting = "Hello world!"; 
  cout << 
  return 0; 
}

Save this and click the Build button (the blue gear) to attempt compilation. You should see some compilation errors due to the incomplete statement.

Now put the cursor at the end of the “cout” line and type “greet” (without the quotes). Pause a moment and notice that a box pops up suggesting a possible completion for this variable name. Hit Tab to accept this suggestion. Now finish the program like this:

helloWithString.cpp
#include <iostream> 
#include <string> 

using namespace std; 

int main() 
{
  string greeting = "Hello world!"; 
  cout << greeting << endl;
  return 0; 
}

Save and again click the Build button. This time things should succeed.

Click the Run button (the blue triangle) to execute your program.

When you are satisfied, close that project.

1.2 Creating a Project From Existing Code

Now let’s think about a situation where we already have code in a directory and we want to build a project using that code.

  1. Select “File->New->Project.” Select “Console Application” and click “Go”. Move through the Wizard, choosing C++ as the language.

    Give the project the title “ExistingProject” (or whatever else you prefer).

  2. Click on the “…” button to choose a place in which to save your work. Navigate to the directory where you already have your code. You want to get Code::Blocks to put your new ExistingProject.cbp (the file that contains the project settings) in that same directory.

Code::Blocks will try very hard to put that file into a subdirectory also named “ExistingProject”. For example, if you have your code in courses/homework/asst2/ and navigate to there, Code::Blocks will try to set up the project in courses/homework/asst2/ExistingProject/ExistingProject.cbp. You will probably need to manually edit the “Resulting File Name” path to remove the extra “ExistingProject/” from the path.

  1. Finish the rest of the New Project Wizard.

  2. Look at the Sources list for your project. Code::Blocks will not automatically include your existing source code as part of the project settings.

    Code::Blocks will have set up a simple “Hello World” program in main.cpp. But since you are working with existing code, you probably have your own main function and Right-click on main.cpp and don’t want this one. Right-click on main.cpp and select “Remove file from project”.

  3. From the Project menu, select “Add Files”. Select your existing .h and .cpp files.

You should now be ready to go.

2 New Projects in Eclipse

2.1 Creating an Eclipse Project From Scratch

  1. From the “File” menu select “New…”, then “Project…”, then choose the type of project you want (typically, “Java Project”, “C++ project”, or “Makefile project with existing code”.

  2. In the next screen, give your project a name.

    I don’t generally recommend that you store the actual code of your project inside your Workspace, which is the default behavior in Eclipse.

    Instead, clear the check box labeled “Use default location” and use the Location Browse button to select an appropriate directory where you would like to keep your code.

    For C++ projects, you may need to further choose between “Executable…empty project” and “Makefile…empty project” depending on whether you have been provided with a make file or intend to write one of your own. If you are working on a PC with more than one C++ compiler, you may need to indicate which one (“Toolchain”) you want to use.

  3. Move forward with Next and Finish to complete the project setup.

2.2 Creating an Eclipse Project From Existing Code

Follow the same instructions as when creating a new empty project.

If Eclipse finds source code in your chosen project directory, it will automatically include it as part of your project settings.