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.
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.
Put those
main
functions in separate namespaces and then define, which one do you want to run, eg.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
, selectSet as Startup Project
, and then click theStart
button to run it.To add a project to solution, right-click the solution and choose
Add
|New project...
orAdd
|Existing project
.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.
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.
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.