Creating a New Project in Your IDE
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.
-
It’s really the location where the IDE is going to store your project settings.
-
The project settings include information on what compiler you are using, which optional settings on the compiler you want to use, what program or programs the project is supposed to build, what steps are necessary to build them, etc.
-
One of the most important and most obvious thing that the project settings will record is what source code files (in our case,
.h
and.cpp
files) are part of your project.-
Many people will put those files into the same directory where the project settings have been stored.
-
But they don’t have to be in there. Many programmers may put their source code into subdirectories of the project directory. Sometimes you may “import” source code from a completely different directory.
-
It’s also possible to have
.h
and.cpp
files in the project directory that aren’t part of the project and won’t be compiled when we try to build the 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.
-
If you use the IDE itself to create that file of source code, it will add it to the current project by default.
-
But if you create files outside of the IDE (e.g., by using the PC’s operating system to copy files from another location into your project directory, the IDE may ignore these new files unless you explicitly tell the IDE to add them to your project.
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.
- Keep the earlier discussion about safe places to store your work in mind as you choose this.
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.
- If you see an error message that says you are using “an invalid compiler”, then Code::Blocks did not actually locate the compiler properly. From the “Settings” menu, select “Compiler and Debugger”, On the tab “Toolchain excutables”, Use the “…” button for the “Compiler’s Installation Directory” to point it to the directory where you installed the MinGW or CygWin compilers.
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:
#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.
-
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).
-
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.
-
Finish the rest of the New Project Wizard.
-
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 ownmain
function and Right-click onmain.cpp
and don’t want this one. Right-click onmain.cpp
and select “Remove file from project”. -
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
-
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”.
-
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.
-
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.