Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple main CPP files in VisualStudio?

Tags:

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.

like image 219
Suzan Cioc Avatar asked Jun 10 '13 11:06

Suzan Cioc


People also ask

Can you have multiple cpp files in project?

You have to make C++ aware of all the different files that are needed to actually complete your project. You have to make sure that you never define the same thing twice, in two different places (e.g., have the same definition for a function in two different files). “Multiple definition” is an error in C++.

Can you have two CPP files in C++?

You must use a tool called a "header". In a header you declare the function that you want to use. Then you include it in both files. A header is a separate file included using the #include directive.

How do I add a main CPP file in Visual Studio?

Add a new source file to the project, as follows. In Solution Explorer, right-click the Source Files folder, point to Add, and then click New Item. In the Code node, click C++ File (. cpp), type a name for the file, and then click Add.


2 Answers

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.

like image 132
Spook Avatar answered Sep 19 '22 11:09

Spook


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.

like image 26
Johnathan Enslin Avatar answered Sep 22 '22 11:09

Johnathan Enslin