Multiple main CPP files in VisualStudio? - c++

I have a sample directory of some software, which contains multiple files with multiple main functions. May I assemble all of these files into single project, compile them and then run specific ones without getting main already defined error? Suppose I don't want to create separate project for each cpp file.
UPDATE
I need simple one-two-click solution (if it exists). I don't want to distribute files among folders or refactor files content. For example in Eclipse/Java you can right-click any file with main and run it. And there can be many of main files in one project. Is this possible for VisualStudio/CPP?
UPDATE 2
I know that C++ is not Java and that Visual Studio is not Eclipse. My question is about automation of some manual operations.

Put those main functions in separate namespaces and then define, which one do you want to run, eg.
File1.cpp
namespace F1
{
int main(int argc, char * argv[])
{
// ...
}
}
The-real-main.cpp
int main(int argc, char * argv[])
{
if (whatever)
return F1::main(argc, argv);
}
Edit: In response to additional information.
C++ is not Java and VS is not Eclipse :) The natural way to maintain multiple programs at once in VS is to put multiple projects (one for each executable or library) in a single solution. If you want to run a project, simply right-click it in Solution Explorer, select Set as Startup Project, and then click the Start button to run it.
To add a project to solution, right-click the solution and choose Add | New project... or Add | Existing project.

In Visual studio:
Create one "Solution" - under the solution one can create multiple "projects". Each project will compile separately into an executable. Compiling is done as normal other than "unloading" the unneeded projects. In order to reopen one of the other projects simply choose "reload project" from the solutions explorer.
This function is useful for study/organizational purposes where one is grouping source files in a common "folder" for easy search and access while still compiling/debugging separately. The main advantage from what I can tell is that one can easily navigate ones projects using the solution explorer.

I haven't worked OpenCV, but it uses cmake, and has a CMakeLists.txt in the sample directory. There's some discussion about building the samples using cmake here.
Cmake doesn't build anything itself, it generates build scripts for the target platform, and should be able to create Solution and Project files that you can load into Visual Studio.

May be the most simple solution is to use multiple build configurations. Just create a number of build configurations define an entry point for each of them.

In Visual Studio, you must create one project per executable you want to create.

If only one file needs to be built, you can use 'Exclude Files From Build' option in Properties to exclude the rest. Select multiple source files and exclude them at once. Obviously, this solution does not work if you are accessing functions/symbols across files.

Yes, there is a simple way to do this:
Right click these source files which contain a main function and you don't want to run in the solution explorer window, then go to property->configuration->general->excluded from build, select yes.
So these source files won't compile and run when you build and run the project. When you want to run another source file just include it and exclude other source files.

Just a few basic steps to compile & run multiple main files in each c++ project:
Suppose you have c1.cpp, c2.cpp ... c100.cpp files.
Click on Show all files
Exclude all the cpp files except the one you want to compile and run.
So in each instances you project only have one main file but you can see all the files under project. So each time you want to run any file just exclude other files and include this file only.

Related

Multiple C++ Main Files Within a Single Visual Studio Project

I am just beginning my journey in C++ programming (coming from many other languages). Because I am learning the basics, I would like to store many simple C++ programs in a single project. Each of these simple console type applications will contain a main function.
When creating an empty C++ project, Visual Studio creates 15 files which comes to 546KB. To save room on my computer, it would be best to have sub-folders within this single project for each small C++ program for learning purposes.
Is it possible to break different console based programs up into separate folders within the same Visual Studio C++ project? I am using VS 2017 Community Edition.
Edit:
I understand that you can create a VS Solution with multiple projects within. The reason why I don't want to do this is explained above. 546KB is a lot of space to allocate just for running a small console application. It would be best to have many programs that can run independently within a single project. Is there a way in VS to choose which main function to call out of all of the main C++ files?
You can use separate project Configuration for each main function that you've got in your project (e.g. create project Configurations named Main1 and Main2). Then you can conditionally include the .cpp file that implements the relevant main function by editing your .vcxproj file, e.g.:
<ClCompile Include="main1.cpp" Condition="'$(Configuration)' == 'Main1'" />
<ClCompile Include="main2.cpp" Condition="'$(Configuration)' == 'Main2'" />
That will include main1.cpp only for a Configuration named Main1 and main2.cpp only for a Configuration named Main2.
I'd not recommend that though because Visual Studio 2017 does not update the Solution Explorer correctly when you switch the current configuration and you'll need to reload the project to see the current files.
Instead of conditional inclusion use the Excluded from build property to exclude files that shouldn't be compiled as part of the current configuration. This will show all files in solution explorer but only include the appropriate files when the build is actually performed. This is a bit backwards but works nicely with the UI. It has the additional benefit of being something the VS property editor can do without needing to open and manually edit the vcproj file.
The Excluded from build property is found on the file properties Configuration properties -> General. Open solution explorer (View->Solution explorer) right click on the file and that pane should be the default.

Having several projects on VS2010 and several "main" functions

I'm working on a game which has different "modules" that I am developping on separate projets for now, with VS2010.
So far I have each project in its own solution, and the main.cpp file is used to basically initialize and do some quick tests on my project, that I change very often.
Is it possible (and how) to have 1 solution regrouping several projects while at the same time having also one "main" function per project that I could launch independently of the other projects to test one project specifically?
Here's what I would do:
Put all your projects in one solution. There is some button or menu option somewhere to Add a Project to a solution. The advantage of this is that you can be editing multiple projects at once and Visual Studio will automatically rebuild everything that needs to be rebuilt when you compile.
All your reusable code (code used in more than on executable) should be divided up into projects that compile to DLLs.
For each executable you want to generate, you should have a project that compiles to an executable and references/links to the DLLs it depends on. Each executable project will have its own main function which runs when you run the exe.
If you want to have a way to test your DLLs without generating an executable, you can make an entry point in your DLL and run it using rundll32. This would be good for developers testing your DLLs, but I would never tell a user to use rundll32.
The build configurations are specific of each project, including which class contains the main method -IIRC, you can define several main in your project and define which one should be called through project configuration-.

gTest and multiple main()

I have an Eclipse project. All testcases are in one *.cpp file. The problem is that this way I end up with two main() functions. One for the app itself and one for the testcases. And Eclipse, of course, refuses to build... I would like to keep everything together under one project (and avoid having multiple configurations, SVN repositories etc). Ideally, I would want to force Eclipse to build two executables (one for the app and one for the testcases). I had a quick look at the Eclipse Makefile, but to be honest, I don't quite understand how exactly it works. It is possible to always exclude Main.cpp/Testcases.cpp and build one executable, but it is not very elegant...
Anybody with similar experience?
I stumbled over this link which was very helpfull: http://pezad-games.googlecode.com/svn/trunk/GTestTest/HOWTO.
The author is describing how to setup gtest with one project in eclipse whilst having two source files with main() methods: one in a directory called "src" and the other in a directory called "test".
Therefor he introduces a new configuration (besides DEBUG/RELEASE) called "GTEST" - in this configuration all linker/header includes are set to gtest and also an exclude to the src/[main].cpp:
Properties for main.cpp >> C/C++ Build >> Settings: Exclude resource
from build
On the other side DEBUG & RELEASE configs exclude the test/[main_test].cpp.
Are you linking with libgtest_main in addition to libgtest? If you don't link in the libgtest_main you should be good.
If you want to create two executables with Eclipse CDT the easiest way is to have each executable have a separate project. You can have one project reference another if you have common code.

Visual Studio: how to create a project that would compile 2 exe files?

So I have main.cpp and main2.cpp with int main in each. I want to get 2 exes out of it. Is it possible and what would be instruction to create such project?
Nope, Visual Studio's project model is rigidly built around the assumption that "one project generates one output".
If you need two executables, you have to create two projects. You can keep them in the same solution to make things easier for yourself, but they have to be separate projects.
Edit
Ok, as other answers have pointed out, it can of course be done, if you're desperate. You can add a custom build step, which does anything you like, including building another executable. (However, the build system won't understand that this file should be considered project output, and so some scenarios may fail: for example the file won't be automatically copied to the output folder, and when checking dependencies before a rebuild, it might not be able to understand which files to check, and what (or how) to rebuild.)
Visual Studio (at least up to 2008, not sure about 2010) also allows the use of nmake files, but then I think you're stretching the definition of "a Visual Studio project".
But under "normal" circumstances, one project implies one output. And in order to get two executables, you'd normally create two projects.
You need a solution which includes two projects. Have a read of the Visual Studio documentation on solutions and projects.
Here's my solution, since nobody in a Google search seems to suggest this method. It's quite simple and I've used/seen it used in other IDEs (like Code::Blocks).
Within your project, create a configuration for each output that you want. Then, only include one main source file in each configuration.
In VS, this means for each source file with main: right-click -> Properties -> Excluded From Build = Yes. So, once you're done, only one main source is built for each configuration. You can then specify a different output for each configuration in the Project Properties. I did this on VS 2010, but it should probably work with other versions.
I'm using this approach so that I can have several tests for one project, without cluttering the solution with more test projects than actual code projects.
I don't know if it can be done ,but the only change you have ,to do this ,is with custom build step.
EDIT: Since someone downvoted this ,i did a test making a dummy configuration.
In the custom build step I two Link-cmds (copied form original link-cmdline and modified it a bit) taking as input main1.obj resp. main2.obj and outputting App1.exe resp. App2.exe.
It's executed after Compiling and before linking.
It worked !
The downside is I cannot prevent (yet) the linking ot the orinal exe (which errors on duplicate main function).
Solution to this could be to have a lib project excluding the sources with main()from build and build them in the custum-step too.
So the answer to the question should : Yes ,it can be done!
You can't have more than one main() function in a single visual studio project. So you can't compile 2 executables, the only way is to make two different project in the same solution
You can create a solution with two project one for each output you want. Then head to Build menu and select Batch Build.. and select both projects.
If you want both exe files to be in one place you can specify a custom Post-build action:
For both project:
view the project properties page and in Build events select Post-Build events, then in the Command line field enter the command that will copy the output to the location you want, something like:
copy $(TargetPath) c:\yourlocation /Y
Then after you build the solution the output files will be copied to that location.
Another option you have is to use conditional compilation with sth like
main()
{
#ifdef VERSION1
call_main_logic();
#else
call_main2_logic();
#endif
}
and then define different configurations within the project. For each configuration you will then need to define preprocessor symbols appropriately (in: project settings -> configuration properties -> C/C++ -> preprocessor).
Please note, that it will be only one executable created at a time, but with switching configurations you'll get the one that does what you want at the moment.
... which may suit your needs or not, depending on more specific situation that you are in.
Edit: In addition, since different configurations create their separate output folders, you will have your both execs as outputs.

How to create two mains in an eclipse C++ project

We've got a program which runs separately, executed with an execvp command. So it needs a main method, but I believe that poses a problem to eclipse with a managed make. Do we have to keep this code segregated into a separate project, or is there a way to incorporate it into the same eclipse project?
Create a project for each executable that has a main() function, and create an additional project to represent the software as a whole (a "container" project of sorts). Eclipse allows you to specify projects as dependencies of other projects, and in this case you will want to set up the container project to list the other projects as "Referenced Projects".
To do this, create the container project, then right-click on the project in the left-hand column (project explorer) and click "Properties". A dialog box will appear. Select the "Project References" item in the list on the left-hand side and you will see a list of all projects that Eclipse is currently working with. Check the boxes next to the projects for your individual executables, then click OK. Now, when you perform a build on the container project, Eclipse should automatically perform a build on these dependent projects as well.
When using sub-projects in this manner, I have (personally) found it useful to create a working set that includes the container project and all of the sub-projects (this can make searching the entire software project easier).
Keep it in the same project and use preprocessor defines which you define differently depending on what kind of main you want to include in the current project. Here the mains are in the same file, but they can of course reside in different files.
#if defined(MAIN_ONE)
int main()
{
// Do stuff
}
#elif defined(MAIN_TWO)
int main()
{
// Do some other stuff
}
#endif
If the makefile being invoked doesn't compile the 2 main() methods into the same executable, it won't cause a problem. I don't know how eclipse projects are handled - if it's like VS, where "project" means a single executable or library, and "solution" is a group of "projects", then it would seem you'd need more than one project. If, OTOH, a "project" can contain different "subprojects" where a "subproject" is an executable or library, you should be able to handle that easily.
I am not aware of any easy way to build two mains using Eclipse build system. The smallest change you need to do might be to move to makefiles and use makefile targets to build.
Instead, I'd advise you to move to using CMake. CMake can be used to generate makefiles to be used with eclipse. The advantage you get from using CMake is that you can easily state how to build the libraries and link the libraries to form the executables. CMake can generate builds for Eclipse, Visual Studio, Code Blocks, or makefiles (so you can use command prompt).
This is built in the C++ language. You would have to modify it to get your result. There is something to do 2 things at once if that is what you want.