Multiple main C++ files in a single project in Code::Blocks?

c++ multiple source files one project
how to run multiple files in code::blocks
multiple definition of main error in code::blocks
multi file programs in c++
program codes in multiple files
c program with multiple source files
how to create cpp file in code blocks
how to create multiple files in c++

I do not regularly write code. There are times I write code daily for 6 months, and then do not code for up to 2 years. This approach has forced me to keep a bunch or reference code that I (and other much, much better programmers) have written. I refer to this "library" when writing code after a long period; I read it, I execute it, and that is a massive help in refreshing myself. This system has served me very well with Eclipse & Java over the past 5 years.

I am now learning C++ and am using Code::Blocks. I would like to somehow stuff a bunch of C++ files that have main methods into a single Code::Blocks project. I am willing to rewrite the code to achieve this task (if it's reasonable...)

I am not the first to look for a meaningful answer for this issue: and

I do not want to change the IDE or compare it to other IDEs. What I am looking for, is the ability to execute one of hundreds of tiny programs that are in a well organized in an expandable file tree in C::B quickly and easily. If I put each C++ file with a main in it's own project I will have so many C::B projects that it will be unreasonable.

I do understand that C::B is not Eclipse and C++ is not Java, and that C::B is intended to have a single c++ file with a main function per project.

Any answers, and even very creative answers would be very appreciated! Scripts, settings, how to rewrite my code, whatever - if you have a suggestion I would love to hear it so I may consider it.

In the interest of full disclosure, currently I am keeping all my tiny programs in directories and use the O/S to drill through the directories and simply double click on the .cpp file which C::B opens. I am willing to dramatically modify my code to be able to achieve the objective.

Thanks for your time.

How about using the precompiler? You can surround each main with:

int main() { return 0; } // example of one of the "mains" in one cpp

int main() { return 0; } // another "main" in an other cpp

int main() { return 0; } // yet another "main" somewhere else

And creating a header, included by all "mains" where you can define one to run:


// Uncomment one and only one


This could be a quick and dirty "build system" for your usecase.

2.8, How do I open an existing project in code blocks? Whenever in a .c file you need to use the functions defined in another .c, you will #include the corresponding header; then you'll be able to use the functions normally. All the .c and .h files must be added to your project; if the IDE asks you if they have to be compiled, you should mark only the .c for compilation.

To compile and run a single main() file in a Code::Blocks project with multiple main() files:

  1. In the "Projects" tab on the left, right-click on a file that you do not want to compile.

  2. In the menu that appears, point to Options, and uncheck both Compile file and Link file.

  3. This has to be done for all the files that you do not want to compile.

  4. Now, when the project is built and run with F9, only the desired file will be compiled; the others will be ignored.

Note: It is not necessary to create a project in Code::Blocks to compile and run single file. To compile and run single files (without creating projects)

  1. Just click on File -> Empty -> New file.

  2. Save the file with .cpp extension anywhere (not in a project).

  3. To compile and run the file, just press F9 or Build -> Build and run

  4. However, such files (without projects) cannot be debugged. The most appropriate thing would be to just have a project with multiple main() files, as explained in the early part of this answer.

Dev C++ multiple files compilation - C++ Forum, Hello World Project with multiple files, Code::Blocks - Duration: 4:22. Linux-​Buddy Page 37,246 Duration: 6:01 Posted: 29 Nov 2015 You may somewhere in your project, if it gets big, include the same header file multiple times. What it checks for is it a variable is defined, and if not, go ahead and continue, otherwise, stop dead in its tracks.

I've learned Java before, and am starting to learn C++ now.

When using Code::Blocks as my primary C++ IDE, I met the same problem as you. Although I found nothing useful on the Internet, I managed to figure it out on my own. Here is my solution.

  1. In your Management panel, find your current project and right click, choose Close Project. (We won't use project to manage our code for this purpose.)
  2. Still in the Management panel, find the Files tab, navigate to your working directory, right click on it and choose Make Root.

And we are done!

When you want to add a new code file, right click on your folder and choose New file..., enter your file name with the extension of .cpp. Then you can just use your C::B as before. Without the limit of project, C::B will just compile your current C++ file and run it on its own. Keyboard shortcuts F9 and the Run and Build button still works.

The only disadvantage is that you can see .exe and .o files in your file list, which is a little untidy. I'm still trying to find how to hide them in the list.

Hope this will help you.

Code::Blocks Student Manual, Multi-file C Program with Makefile in Eclipse - Duration: 19:13. Brian Fraser 29,258 views · 19 Duration: 4:22 Posted: 19 Aug 2014 The new file is listed on the left side of the Code::Blocks window, beneath the Sources heading where the main.c file is listed. A new tab appears in the editor window, with the alpha.c file ready for editing. Click the alpha.c tab to begin editing that file. Type the source code from The alpha.c Source Code File into the alpha.c file in Code::Blocks.

how to use code::blocks for multiple c files, (I may just use vim for these small things, and C::B for actual projects multiple main()s, and when hitting 'run' only build/run the one file I'm  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.)

Hello World Project with multiple files, Code::Blocks, You already know how to create and compile single-file projects. In Code::​Blocks, go to the File menu and choose New > File… For example: g++ main.​cpp add.cpp -o main, where main.cpp and add.cpp are the names of  In C/C++ you have header files (*.H). There you declare your functions/classes. So for example you will have to #include "second.h" to your main.cpp file. In second.h you just declare like this void yourFunction(); In second.cpp you implement it like . void yourFunction() { doSomethng(); }

Multiple main()s in project?, Is there a way i can write more than one code in one pro. c++ code coding codeblocks usualy does, consist of mutiple classes and source files but a project contains a single program. You can have multiple classes in a project and Code::Blocks will compile all of your classes into a single executable. This example includes two .CPP files and a single .H file. I’ll use the files from the second example for this post. The example works the same as the one in the book, but in this case, we’ll start with the three files and create a project around them. You need to start with a CodeBlocks project. It’s possible to use either an existing

  • did you got a better answer? I also have the same issue.
  • @pio I ended up sorting all my code in directories - each directory holds related source files. When I want to run specific .cpp and .h files I remove all the current files from the workspace, and put the files I want to work with. This system provides organization I want, permits coding via CodeBlocks, and overcomes the limitation of having only 1 main method between all the source files.
  • thx. But It's still an headache to remove the files every time you want to run. It sounds to me like there should be a way just to run specific main via the code blocksmanager?
  • Great answer Jerry. Personally - I would recommend you learn a more modern programming langauge, rather then C++. IMHO the C/C++ days are nearing their end. Go for C# or Python buddy.
  • Thanks for your kind advice. In fact, I've already used Python for my data analysis courses and currently planning to take Go as my next learning goal. The reason why I'm learning C++ is just pure curiosity.