Do I understand correctly, that you have a project open, that has
main.cpp under
sources in the manager pane ?
Then you open another source file and click on build or build and run ?
If you do so, that can not work, because the toolbar-buttons work woth the active project, not with single files that does not belong to a project.
In this case you can either close the project and open the file or (better and the only simple way to use the debugger in gdb) add the file to a(n empty) project, to be sure to build and run the correct sources.
Single files can only be build from the menu or with keyboard-shortcut (strg+shift+f9).
If a project (or most likely the default workspace) always opens on C::B startup, you can try to check "Settings -> Environment... -> General settings -> On application start-up -> Open blank workspace".
A look into the documentation might also be helpful:
http://www.codeblocks.org/user-manual.