Multiple main CPP files in VisualStudio?

2019-02-12 19:16发布

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.

6条回答
爷的心禁止访问
2楼-- · 2019-02-12 19:34

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.

查看更多
Rolldiameter
3楼-- · 2019-02-12 19:39

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.

查看更多
看我几分像从前
4楼-- · 2019-02-12 19:42

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.

查看更多
We Are One
5楼-- · 2019-02-12 19:42

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

查看更多
地球回转人心会变
6楼-- · 2019-02-12 19:51

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.

查看更多
男人必须洒脱
7楼-- · 2019-02-12 19:51

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.

查看更多
登录 后发表回答