简体   繁体   English

在Visual Studio中使用子项目

[英]Using sub projects in Visual Studio

I am quite used to Linux development and Makefiles, and started using (Windows and) Visual Studio not so long ago. 我很习惯Linux开发和Makefiles,并且很久以前就开始使用(Windows和)Visual Studio。

What I want to do is (I think) quite simple, but I can't seem to find how to do it using Visual Studio. 我想做的是(我认为)非常简单,但我似乎无法找到如何使用Visual Studio来做到这一点。

I have to write an application, which I can divide into several independent sub-parts. 我必须编写一个应用程序,我可以分成几个独立的子部分。 I want to work incrementally, and create several projects that together with a main file will end up with my full project. 我想以增量方式工作,并创建几个项目,与主文件一起最终将完成我的整个项目。

What I basically want is to be able to write a small project, have a main for it so that I can fully test it, and use it as a dependency for the next project. 我基本上想要的是能够编写一个小项目,有一个主要项目,以便我可以完全测试它,并将其用作下一个项目的依赖项。 In this case, the smaller main would be deactivated (or I can comment it), and I would just change the startup project. 在这种情况下,较小的main将被停用(或者我可以对其进行注释),我只会更改启动项目。 If I find a bug in a subset while writing my bigger software, I could just change the startup project and solve it at a smaller scale. 如果我在编写更大的软件时发现子集中的错误,我可以更改启动项目并以较小的规模解决它。

Well, that's what I do all day long in Python and Java. 嗯,这就是我整天用Python和Java做的事情。

I tried to create new projects into my project, but I always end up having linking problems, where my main projects knows about the files in the sub projects, but not the smaller ones, etc. . 我试图在我的项目中创建新项目,但我总是遇到链接问题,我的主要项目知道子项目中的文件,而不是较小的项目等。 .

So, is there a guide somewhere I can find to work this way ? 那么,我可以找到以这种方式工作的指南吗?

Thank you 谢谢

For individual projects: 对于个别项目:

Every individual project property sheet has a C++ options page. 每个单独的项目属性表都有一个C ++选项页面。 Here you can specify the 'Addional Include Directories' in a comma separated form. 您可以在此处以逗号分隔的形式指定“Addional Include Directories”。

Similarly, there should be a property sheet for Linker where you can specify the 'Addional Include Dependencies' and the names of the libraries it depends on. 同样,Linker应该有一个属性表,您可以在其中指定“Addional Include Dependencies”以及它所依赖的库的名称。

For linker dependencies of the main executable: 对于主可执行文件的链接器依赖项:

Go to that main project, go to its properties, go to common properties and select 'Framework and References'. 转到该主项目,转到其属性,转到公共属性并选择“框架和参考”。 This should give you a list of all the projecs that are in your solution. 这应该为您提供解决方案中所有项目的列表。 Keep adding them and Visual Studio should add the right linker flags automatically for you. 继续添加它们,Visual Studio应自动为您添加正确的链接器标志。

Sorry, have no access to the computer now else would have provided exact steps. 抱歉,现在无法访问计算机,否则会提供确切的步骤。 Visual Studio can get tricky sometimes but once you use it, you'll be amazed by what it can do for you. Visual Studio有时会变得棘手,但是一旦你使用它,你会惊讶于它能为你做些什么。 Personally, I love it. 就个人而言,我喜欢它。

Hope this helps. 希望这可以帮助。

Thanks to Vaibhav, I was able to find a solution: 感谢Vaibhav,我找到了一个解决方案:

I had to : 我不得不 :

  • change subproject type to lib instead of exe 将子项目类型更改为lib而不是exe
  • Add the subprojects as project dependencies in the main project (this just sets the build order) 在子项目中添加子项目作为项目依赖项(这只是设置构建顺序)
  • Comment out the main of my subprojects, to keep only one active. 注释掉我的子项目的主要部分,只保留一个活动的子项目。
  • Add each subproject include directory in the include repos of the main project, so that the compiler can find the header files 在主项目的include repos中添加每个子项目include目录,以便编译器可以找到头文件
  • Add the general directory as a dependency for the linker (in this case, it is not the debug/release folder of the subprojects, but the output directory of the complete project). 添加通用目录作为链接器的依赖项(在这种情况下,它不是子项目的调试/发布文件夹,而是整个项目的输出目录)。
  • Add the names of the lib files of the subprojects in additional dependencies of the linker of the main project. 在主项目的链接器的其他依赖项中添加子项目的lib文件的名称。

To make it simple, the project dependencies capability of VS2010 just changes the order in which the projects are built. 为简单起见,VS2010的项目依赖性功能只是改变了项目的构建顺序。 . . I miss Eclipse. 我想念Eclipse。

If I find a bug and want to test on of the subprojects, I have to : 如果我发现了一个错误并希望测试子项目,我必须:

  • change the startup project to be the subproject I want to change. 将启动项目更改为我想要更改的子项目。
  • uncomment the corresponding main 取消注释相应的主要内容
  • change the project type to be exe instead of lib to be able to compile it. 将项目类型更改为exe而不是lib以便能够编译它。

Debug, and do everything back again to continue working on my main project. 调试,然后再做一切以继续处理我的主项目。 Quite boring, don't you think ? 很无聊,你不觉得吗?

Looks like you trying to do manual unit testing. 看起来你试图进行手动单元测试。 Use something like Google.Test . 使用像Google.Test这样的东西 You need to make test project for every lib. 你需要为每个lib做一个测试项目。

We have directory with static libs projects. 我们有静态库项目的目录。 Another directory with tests projects. 另一个带测试项目的目录。 Every test solution contains one exe project and few existing lib projects. 每个测试解决方案都包含一个exe项目和几个现有的lib项目。 Every project have configured dependencies. 每个项目都配置了依赖项。 You dont need to set additional dependencies of the linker manually (paths are evil, out dir for the lib file will be taken from project settings), open project properties with right mouse button, Common properties, Add new reference and select lib project. 您不需要手动设置链接器的其他依赖项(路径是邪恶的,lib文件的dir将从项目设置中获取),使用鼠标右键打开项目属性,公共属性,添加新引用并选择lib项目。 You only need to set additional include dirs. 您只需要设置其他包含目录。

When you find new bug - just open test project for the library with bug, add code which cause the bug, fix it, and be happy (and sometimes run all test). 当您发现新的bug时 - 只需打开带有bug的库的测试项目,添加导致bug,修复它并且开心的代码(有时运行所有测试)。 And even better - use TDD . 甚至更好 - 使用TDD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM