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.
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++.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With