Adding source and header files to project in codeblocks

How are you supposed to add a new source and header file to an existing codeblocks project.

I know you can create the files outside of codeblocks, in which case the files won’t show up in codeblocks under the src folder.

If you add the class within codeblocks the header file and source file get filed under two newly created directories - Sources and Headers.

I know if I add a file to the src directory it is supposed to be automatically compiled but it would be nice if it showed up in codeblocks. It seems I could edit the cbp to include my files but that is less than ideal.


I encountered the same problem. Let me share my findings/thoughts. There are two things relevant here, the location in a physical folder (read: where is the file on the hard drive) and the location in a virtual folder (read: where is the file in the codeblocks project). In both cases the default behavior of codeblocks differs from the OpenFrameworks ProjectGenerator solution.

Physical folder
In all OF examples the source and header files are in the same folder (namely the src folder). When you add a new class in CB, the source file is placed in the src folder, but the header file is placed in the include folder. So the location on the hard drive differs.

Solution: To ensure the header file of a class is also added to the src folder, you can manually set the folder location in the “create new class” dialog box. You can find it in the bottom left under “Header file” and then “Folder”. Change the end from \include to \src. Then the header file is placed in the src folder and thus both class files are in the same location.

Note: Unfortunately this has to be done manually, each time you add a class. I haven’t found a way yet to change the default header file folder.

Virtual folder
In the CB project (as generated by the ProjectGenerator) of all OF examples the files are placed in a virtual folder in CB called src. When adding a new class the source file is added to a Sources folder and the header file is added to a Headers folder. So they are not in the src virtual folder as the rest of the OF-added files.

There are multiple ways to correct this.

Solution 1: Manually drag both files (in CB’s project structure) from respectively the Sources and the Headers folder (and perhaps a subfolder inside that) to the src virtual folder. This has to be done each time you add a class.

Solution 2: Change the view. In the menu, go to “Project”, then “Project tree” and uncheck “Categorize by file types”. Then the Sources and Headers folders in the CB project disappear and all files are shown under their physical folder (which is src if you followed the steps for the single physical folder solution as mentioned above). Changing this view setting has to be done only once.

Solution 3: Once the class files have been added, go through the following steps. Close CB. Remove the CB project files from the project’s root folder. Run the ProjectGenerator to create an OF-style project (read: with all the files in the src folder inside a virtual folder called src in the CB project).

Note: The last solution is probably the most work. The second solution is the least work, if you like these view settings and make sure the header file is also in the src folder (and not CB’s default include folder). The first solution is pretty quick, but has to be repeated for each class. Solution 1 and 3 are clean solutions, solution 2 is a workaround. It does not seem possible to prevent files from being added to these default Header and Sources folders in CB. But at least there are alternative option on how to deal with this.

Hope this may be of use to you and others who face the same problem! :smile:

Thanks for the reply.
I ended up getting a bit frustrated with codeblocks, tried visual studio and found 2013 isn’t supported so I gave cinder a try and I’m going to stick with that.

here’s a working vs2013 release:

we’ll have 2013 working with 0.9…

HI Zach,
That link doesn’t work. How far away is 0.9? Days, weeks, months, years?

it’s close – in the next few months.

here’s a new link –

(note this is 64 bit OF, there’s also this branch to check out as well:
via OpenFrameworks And VS2013?)

hey, I don’t know how to add an OF my new class with Visual Studio 2015.
I tried dragging the files, also with add class / wizard …etc. I don’t know if I need to specify the ofBaseClass.h or what…
Any help around?
VS puts the files on the root, but dunno if it works… I just still testing blind…