C++ VisualStudio 中的多个主要 CPP 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17022538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:50:09  来源:igfitidea点击:

Multiple main CPP files in VisualStudio?

c++visual-studiomain

提问by Suzan Cioc

I have a sample directory of some software, which contains multiple files with multiple mainfunctions. May I assemble all of these files into single project, compile them and then run specific ones without getting main already definederror? Suppose I don't want to create separate project for each cpp file.

我有一些软件的示例目录,其中包含多个具有多种main功能的文件。我可以将所有这些文件组合到一个项目中,编译它们然后运行特定的文件而不会main already defined出错吗?假设我不想为每个 cpp 文件创建单独的项目。

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?

我需要简单的一键两次单击解决方案(如果存在)。我不想在文件夹之间分发文件或重构文件内容。例如,在 Eclipse/Java 中,您可以使用 main 右键单击​​任何文件并运行它。一个项目中可以有许多主文件。这对 VisualStudio/CPP 有可能吗?

UPDATE 2

更新 2

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

我知道 C++ 不是 Java,Visual Studio 也不是 Eclipse。我的问题是关于一些手动操作的自动化。

采纳答案by Marcelo Cantos

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.

我没有使用过 OpenCV,但它使用 cmake,并且在示例目录中有一个 CMakeLists.txt。有一个关于使用cmake构建样本一些讨论在这里

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.

Cmake 本身不构建任何东西,它为目标平台生成构建脚本,并且应该能够创建可以加载到 Visual Studio 中的解决方案和项目文件。

回答by Spook

Put those mainfunctions in separate namespaces and then define, which one do you want to run, eg.

将这些main函数放在单独的命名空间中,然后定义您要运行哪个函数,例如。

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 Startbutton to run it.

C++ 不是 Java,VS 也不是 Eclipse :) 在 VS 中同时维护多个程序的自然方法是将多个项目(每个可执行文件或库一个)放在一个解决方案中。如果要运行项目,只需在 中右键单击它Solution Explorer,选择Set as Startup Project,然后单击Start按钮运行它。

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

要将项目添加到解决方案,请右键单击解决方案并选择Add| New project...Add| Existing project.

回答by Johnathan Enslin

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.

此功能对于学习/组织目的非常有用,其中将源文件分组在一个公共“文件夹”中以便于搜索和访问,同时仍然单独编译/调试。据我所知,主要优点是可以使用解决方案资源管理器轻松浏览项目。

回答by Jabberwocky

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.

可能最简单的解决方案是使用多个构建配置。只需创建一些构建配置,为每个配置定义一个入口点。

回答by winterlight

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.

如果只需要构建一个文件,您可以使用“属性”中的“从构建中排除文件”选项来排除其余文件。选择多个源文件并一次排除它们。显然,如果您跨文件访问函数/符号,则此解决方案不起作用。

回答by Dirac Wu

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.

是的,有一个简单的方法可以做到这一点:右键单击这些包含主函数且您不想在解决方案资源管理器窗口中运行的源文件,然后转到属性-> 配置-> 常规-> 从构建中排除,选择是。因此,当您构建和运行项目时,这些源文件不会编译和运行。当您想运行另一个源文件时,只需包含它并排除其他源文件。

回答by Marcelo Cantos

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

在 Visual Studio 中,您必须为每个要创建的可执行文件创建一个项目。