Multiple core CPP files in VisualStudio?

I have a sample directory of some software that contains several files with several main functions. Can I collect all these files into one project, compile them, and then run the specific ones without receiving the main already defined error? Suppose I do not want to create a separate project for each cpp file.

UPDATE

I need a simple one-click solution (if one exists). I do not want to distribute files among folders or contents of refactoring files. For example, in Eclipse / Java, you can right-click any file with main and run it. And in one project there can be many main files. Is this possible for VisualStudio / CPP?

UPDATE 2

I know that C ++ is not Java and Visual Studio is not Eclipse. My question is about the automation of some manual operations.

+6
source share
5 answers

I did not work OpenCV, but it uses cmake and has CMakeLists.txt in the examples directory. It discusses creating samples using cmake here .

Cmake does not create anything, it generates build scripts for the target platform and should be able to create solution and project files that can be loaded into Visual Studio.

+2
source

Put these main functions in separate namespaces and then determine which one you want to run, for example.

 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 :) A natural way to simultaneously service several programs in VS is to place several projects (one for each executable file or library) in one solution. If you want to start a project, simply right-click on it in Solution Explorer , select Set as Startup Project , and then click the Start button to start it.

To add a project to the solution, right-click it and select Add | New project... or Add | Existing project .

+12
source

In Visual studio:

Create one โ€œsolutionโ€ - within the solution you can create several โ€œprojectsโ€. Each project will be compiled separately into an executable file. Compilation is performed as usual, except for "unloading" unnecessary projects. To reopen one of the other projects, simply select "reload project" from the solution explorer.

This function is useful for educational / organizational purposes, where source files are grouped into a common "folder" for easy search and access when compiling / debugging separately. The main advantage of what I can say is that you can easily navigate projects using the solution explorer.

+3
source

Maybe the easiest solution is to use multiple build configurations. Just create several build configurations to define an entry point for each of them.

+2
source

In Visual Studio, you must create one project for the executable that you want to create.

0
source

Source: https://habr.com/ru/post/946945/


All Articles