28

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.

9
  • Do you want a single program to pop out that acts as a gateway to all the other "main" functions, or one program per main? Commented Jun 10, 2013 at 11:13
  • VisualStudio could be this single program. It could compile each main-containing file into separate EXE and run it by right-click on source CPP (for example). Commented Jun 10, 2013 at 11:19
  • 1
    C++ is not Java :) You can have multiple projects in solution, but you cannot simply run a cpp file. Commented Jun 10, 2013 at 11:20
  • I wasn't asking about the program that does the compiling and linking. I was asking whether you want the end-result to be a single executable or multiple executables. In any event, it seems that you want multiple EXEs. Commented Jun 10, 2013 at 11:22
  • 1
    You should provide more detail about what you are trying to do and why. Hopefully, it'll make it easier to provide helpful answers. Commented Jun 10, 2013 at 11:59

8 Answers 8

26

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Is it possible to convert a folder with multiple CCPs into a solution with multiple projects by one click?
Convert - in practice, not. You can prepare the whole solution with multiple projects and then run them with one click (two, actually).
There's also an option to modify CPP's parameters (in Properties window), such that only one of them will be compiled at a time (Included In Project or File Type). Note though, that this is a quick-and-dirty solution. The preferred one includes setting up a series of projects and managing them this way.
You may want to look at the following link: opentissue.org/mediawiki/index.php/Using_CMake
Actually, F1 namespace is not automatically recognized in the file The-real-main.cpp. I had to create a header file for the File1.cpp and #include it, otherwise I was getting namespace with that name does not exist error. Can you please elaborate @Spook?
10

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.

Comments

4

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.

Comments

4

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.

Comments

2

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.

Comments

1

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.

Comments

0

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

4 Comments

Can I create these projects automatically?
Alarm bells are going off in my head. Why do you have so many EXEs that you need to automate the process?
I have a library (this is OpenCV) and it has a samples directory. Anyway it is normal to put multiple cpp sample files into one directory in many cpp opensource libraries. I just want to compile them fast.
See how to set multiple startup projects to build and run a main project.
0

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.

  1. Click on Show all files
  2. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.