Difference between revisions of "Creating a new project"

From Code::Blocks
Line 1: Line 1:
 
[[Category: User Documentation]]
 
[[Category: User Documentation]]
 +
'''This is a work in progress, and may be missing key details.'''
 +
==The project wizard==
 
[[File:ProjectWizard.png|right]]
 
[[File:ProjectWizard.png|right]]
'''This is a work in progress, and may be missing key details.'''
 
 
 
Launch the Project Wizard through ''File->New->Project...'' to start a new project. Here there are many pre-configured templates for various types of projects, including the option to create custom templates. Select '''Console application''', as this is the most common for general purposes, an click '''Go'''.
 
Launch the Project Wizard through ''File->New->Project...'' to start a new project. Here there are many pre-configured templates for various types of projects, including the option to create custom templates. Select '''Console application''', as this is the most common for general purposes, an click '''Go'''.
  
Line 10: Line 10:
  
 
[[File:SelectSource.png|right]]Finally, the wizard will ask if this project should use the default compiler (normally GCC) and the two default builds: '''Debug''' and '''Release'''. All of these settings are fine. Press finish and and the project will be generated. The main window will turn gray, but that is not a problem, the source file needs only to be opened. In the '''Projects''' tab of the '''Management''' pane on the left expand the folders and double click on the source file '''main.cpp''' to open it in the editor. This file contains the following standard code.
 
[[File:SelectSource.png|right]]Finally, the wizard will ask if this project should use the default compiler (normally GCC) and the two default builds: '''Debug''' and '''Release'''. All of these settings are fine. Press finish and and the project will be generated. The main window will turn gray, but that is not a problem, the source file needs only to be opened. In the '''Projects''' tab of the '''Management''' pane on the left expand the folders and double click on the source file '''main.cpp''' to open it in the editor. This file contains the following standard code.
 +
 +
''main.cpp''
 
<source lang="cpp" line>
 
<source lang="cpp" line>
 
#include <iostream>
 
#include <iostream>
Line 24: Line 26:
 
===Adding a blank file===
 
===Adding a blank file===
 
In this example, we will be splitting the function
 
In this example, we will be splitting the function
 +
 +
''main.cpp''
 
<source lang="cpp" line start="7">
 
<source lang="cpp" line start="7">
 
     cout << "Hello world!" << endl;
 
     cout << "Hello world!" << endl;
Line 31: Line 35:
 
[[File:NewFile.png]]
 
[[File:NewFile.png]]
 
[[File:Hello.cpp.png|right]]
 
[[File:Hello.cpp.png|right]]
Select '''C/C++ source''' and click '''Go'''. Continue through the following dialogs very much like the original project creation, selecting '''C++''' when prompted for a language. On the final page, you will be presented with several options. The first box will determine the new filename and location (as noted, the full path is required). You may optionally use the '''...''' button to bring up a file browser window to save the file's location. Checking '''Add file to active project''' will store the filename in the '''Sources''' folder of the '''Projects''' tab of the '''Management''' panel. Checking any of the build targets will alert Code::Blocks that the file should be compiled and linked into the selected target(s). This can be useful if, for example, the file contains debug specific code, as it will allow the inclusion (or exclusion) from the a the appropriate build target. In this example, however, the hello function is of key importance, and is required in each target, so select all the boxes.
+
Select '''C/C++ source''' and click '''Go'''. Continue through the following dialogs very much like the original project creation, selecting '''C++''' when prompted for a language. On the final page, you will be presented with several options. The first box will determine the new filename and location (as noted, the full path is required). You may optionally use the '''...''' button to bring up a file browser window to save the file's location. Checking '''Add file to active project''' will store the filename in the '''Sources''' folder of the '''Projects''' tab of the '''Management''' panel. Checking any of the build targets will alert Code::Blocks that the file should be compiled and linked into the selected target(s). This can be useful if, for example, the file contains debug specific code, as it will allow the inclusion (or exclusion) from the a the appropriate build target. In this example, however, the hello function is of key importance, and is required in each target, so select all the boxes and click '''Finish''' to generate the file.
 +
 
 +
The newly created file should open automatically; if it does not, open it by double clicking on its file in the '''Projects''' tab of the '''Management''' panel. Now add in code for the function '''main.cpp''' will call.
 +
 
 +
''hello.cpp''
 +
<source lang="cpp" line>
 +
#include <iostream>
 +
 
 +
using namespace std;
 +
 
 +
void hello()
 +
{
 +
    cout << "Hello world!" << endl;
 +
}
 +
</source>
 +
===Adding a pre-existing file===
 +
Now that the <code>hello()</code> function is in a separate file, the function must be declared for '''main.cpp''' to use it. Launch a plain text editor (for example Notepad or Gedit), and add the following code.
 +
 
 +
''hello.h''
 +
<source lang="cpp" line>
 +
#ifndef HELLO_H_INCLUDED
 +
#define HELLO_H_INCLUDED
 +
 
 +
void hello();
 +
 
 +
#endif // HELLO_H_INCLUDED
 +
</source>
 +
[[File:TargetBelonging.png|right]]
 +
Save this file as a header ('''hello.h''') in the same directory as the other source files in this project. Back in Code::Blocks, click ''Project->Add files...'' to open a file browser. Here you may select one or multiple files (using combinations of ''Ctrl'' and ''Shift''). (The option ''Project->Add files recursively...'' will search through all the subdirectories in the given folder, selecting the relevant files for inclusion.)  Select '''hello.h''', and click '''Open''' to bring up a dialog requesting to which build targets the file(s) should belong. For this example, select both targets.
 +
 
 +
''Note: if the current project has only one build target, this dialog will be skipped.''
 +
 
 +
Returning to the main source ('''main.cpp''') include the header file and replace the <code>cout</code> function to match the new setup of the project.
 +
 
 +
''main.cpp''
 +
<source lang="cpp" line>
 +
#include "hello.h"
 +
 
 +
int main()
 +
{
 +
    hello();
 +
    return 0;
 +
}</source>
 
==Modifying build options==
 
==Modifying build options==

Revision as of 21:29, 15 August 2011

This is a work in progress, and may be missing key details.

The project wizard

ProjectWizard.png

Launch the Project Wizard through File->New->Project... to start a new project. Here there are many pre-configured templates for various types of projects, including the option to create custom templates. Select Console application, as this is the most common for general purposes, an click Go.

The console application wizard will appear next. Continue through the menus, selecting C++ when prompted for a language. In the next screen, give the project a name and type or select a destination folder. As seen below, Code::Blocks will generate the remaining entries from these two.

ConsoleApplication.png

SelectSource.png

Finally, the wizard will ask if this project should use the default compiler (normally GCC) and the two default builds: Debug and Release. All of these settings are fine. Press finish and and the project will be generated. The main window will turn gray, but that is not a problem, the source file needs only to be opened. In the Projects tab of the Management pane on the left expand the folders and double click on the source file main.cpp to open it in the editor. This file contains the following standard code.

main.cpp

#include <iostream>

using namespace std;

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

Changing file composition

A single source file is of little uses in programs of any useful complexity. In order to handle this, Code::Blocks has several very simple methods of adding additional files to the project.

Adding a blank file

In this example, we will be splitting the function

main.cpp

    cout << "Hello world!" << endl;

into a separate file. To add the new file to the project, bring up the file template wizard through either File->New->File... or Main Toolbar->New file (button)->File...

NewFile.png

Hello.cpp.png

Select C/C++ source and click Go. Continue through the following dialogs very much like the original project creation, selecting C++ when prompted for a language. On the final page, you will be presented with several options. The first box will determine the new filename and location (as noted, the full path is required). You may optionally use the ... button to bring up a file browser window to save the file's location. Checking Add file to active project will store the filename in the Sources folder of the Projects tab of the Management panel. Checking any of the build targets will alert Code::Blocks that the file should be compiled and linked into the selected target(s). This can be useful if, for example, the file contains debug specific code, as it will allow the inclusion (or exclusion) from the a the appropriate build target. In this example, however, the hello function is of key importance, and is required in each target, so select all the boxes and click Finish to generate the file.

The newly created file should open automatically; if it does not, open it by double clicking on its file in the Projects tab of the Management panel. Now add in code for the function main.cpp will call.

hello.cpp

#include <iostream>

using namespace std;

void hello()
{
    cout << "Hello world!" << endl;
}

Adding a pre-existing file

Now that the hello() function is in a separate file, the function must be declared for main.cpp to use it. Launch a plain text editor (for example Notepad or Gedit), and add the following code.

hello.h

#ifndef HELLO_H_INCLUDED
#define HELLO_H_INCLUDED

void hello();

#endif // HELLO_H_INCLUDED
TargetBelonging.png

Save this file as a header (hello.h) in the same directory as the other source files in this project. Back in Code::Blocks, click Project->Add files... to open a file browser. Here you may select one or multiple files (using combinations of Ctrl and Shift). (The option Project->Add files recursively... will search through all the subdirectories in the given folder, selecting the relevant files for inclusion.) Select hello.h, and click Open to bring up a dialog requesting to which build targets the file(s) should belong. For this example, select both targets.

Note: if the current project has only one build target, this dialog will be skipped.

Returning to the main source (main.cpp) include the header file and replace the cout function to match the new setup of the project.

main.cpp

#include "hello.h"

int main()
{
    hello();
    return 0;
}

Modifying build options