简体   繁体   English

C ++ Visual Studio - 多项目解决方案 - “静态库”与“包含目录”

[英]C++ Visual Studio - multi-project solution - “static lib” vs “include directories”

I am trying to set up a multi-project solution using static libraries in Visual Studio . 我正在尝试使用Visual Studio中的静态库来设置多项目解决方案。

As an example, the project OtherProject contains the class Foo situated in Foo.h , which I would like to use in the project MyProject . 作为一个例子,OtherProject包含类Foo位于foo.h中 ,我想在这个项目MyProject的使用项目。

From what I have understood, to accomplish this using static libraries I have to: 根据我的理解,要使用静态库完成此操作,我必须:

  1. In OtherProject change "Configuration Type" to "Static library (.lib)". OtherProject中,将“Configuration Type”更改为“Static library(.lib)”。
  2. In MyProject . MyProject中
    1. In "Common Properties -> Framwork and Referencees" Add New Reference to OtherProject 在“Common Properties - > Framwork and Referencees”中添加对OtherProject的新引用
    2. In "VC++ Directories" 在“VC ++目录”中
      1. In "Include Directories" add path to OtherProject 's header files. 在“包含目录”中添加OtherProject的头文件的路径。
      2. In "Library Directories" add path from OtherProject to the created "OtherProject.lib". 在“库目录”中,将路径从OtherProject添加到创建的“OtherProject.lib”。
    3. In "Input>Additional Dependencies" add "OtherProject.lib". 在“输入>附加依赖项”中,添加“OtherProject.lib”。 (Edit) This step appears to be unnecessary and is made implicitly during 2.1 (thanks to @JBentley for pointing it out). (编辑)此步骤似乎是不必要的,并且在2.1期间隐式进行(感谢@JBentley将其指出)。
  3. " Solution Properties -> Common Properites -> Project Dependencies", chose MyProject in dropdown and add dependency to OtherProject . 解决方案属性 - > Common Properites - > Project Dependencies”,在下拉列表中选择MyProject并将依赖添加到OtherProject (Edit) This is unnecessary, and is made implicitly in step 2.1 (also @JBentley). (编辑)这是不必要的,并且在步骤2.1(也是@JBentley)中隐式地进行。

...and everything should be peachy. ......一切都应该是桃子的。 However, if I skip everything above and only include the source files of OtherProject , like this: 但是,如果我跳过上面的所有内容并且只包含OtherProject的源文件,如下所示:

  1. In MyProject "VC++ Directories -> Include Directories" add path to OtherProject header files (and possible source files as well?). MyProject “VC ++目录 - >包含目录”中添加OtherProject头文件的路径(以及可能的源文件?)。

...everything appears to be working like before, and with much less work. ......一切似乎都像以前一样工作,工作少得多。

My test file in MyProject looks like this: 我在MyProject中的测试文件如下所示:

#include <OtherProject/Foo.h>

int main()
{
    Foo foo;
    return 0;
}

Questions 问题

  • Does my "Static Library" approach appear to be valid, or may I run into problems later on? 我的“静态库”方法是否有效,或者我以后可能会遇到问题?
  • Is the second approach valid? 第二种方法有效吗? Will it create problems later? 它会在以后产生问题吗? I guess one cons is that I cannot hide part of the implementation in a lib file, and have to supply both source and header instead of lib and header. 我想一个缺点是我无法隐藏lib文件中的部分实现,并且必须提供源代码和头文件而不是lib和头文件。
  • Is there any pros and cons of each methods? 每种方法都有利弊吗?

(Edit) The following question is about a similar problem: Link libraries with dependencies in Visual C++ without getting LNK4006 . (编辑)以下问题是关于类似的问题: 在不获取LNK4006的情况下,在Visual C ++中链接具有依赖项的库 It appears Visual Studio is smart enough to take care of some of some of the steps I listed, and avoiding the unnecessary steps appears to get rid of "LNK4006 second definition ignored" I was having further down the line: 看来Visual Studio很聪明,可以处理我列出的一些步骤,避免不必要的步骤似乎摆脱了“LNK4006第二个定义被忽略”我进一步下线了:

Does my "Static Library" approach appear to be valid, or may I run into problems later on? 我的“静态库”方法是否有效,或者我以后可能会遇到问题?

Looks about right. 看起来是对的。 Can't see anything that would cause a problem (other than if you start moving directories around, etc). 看不到会导致问题的任何事情(除非您开始移动目录等)。

Debugging may be hindered by the lack of source code for Foo 's implementation, making it harder to use. Foo的实现缺乏源代码可能会妨碍调试,使其更难使用。 Of course, you can always supply the source file and a debug build of the library, in which case this particular problem "goes away". 当然,您始终可以提供源文件和库的调试版本,在这种情况下,此特定问题“消失”。

Is the second approach valid? 第二种方法有效吗? Will it create problems later? 它会在以后产生问题吗? I guess one cons is that I cannot hide part of the implementation in a lib file, and have to supply both source and header instead of lib and header. 我想一个缺点是我无法隐藏lib文件中的部分实现,并且必须提供源代码和头文件而不是lib和头文件。

Would be valid (in the sense that you can include other source code from another project into your current project - it's no longer, technically, a "static library"), yes (the user may need to add the Foo.cpp file to the project as well - and any other file that the project includes. If Foo and it's parts is a lot of files, this could be a major drawback. 是有效的(从某种意义上说,你可以将其他项目中的其他源代码包含到当前项目中 - 它不再是技术上的“静态库”),是的(用户可能需要将Foo.cpp文件添加到项目以及项目包含的任何其他文件。如果Foo和它的部分是很多文件,这可能是一个主要的缺点。

Aside from having to supply sources, you also increase the compile time for the entire project (at least when built from scratch). 除了必须提供源之外,还可以增加整个项目的编译时间(至少从头开始构建)。 This may not be an issue for a small project, but larger projects can take quite some time to build. 对于小型项目而言,这可能不是问题,但较大的项目可能需要相当长的时间来构建。

A plus is of course that the user of the functinality in class Foo can step into the source and debug it when something goes wrong. 当然,加上Foo类功能的用户可以进入源代码并在出现问题时对其进行调试。

 Is there any pros and cons of each methods? 

Answered above, I think. 我想,上面已回答。

Your second approach is not quite valid in the sense that you are not really creating a static library. 在你没有真正创建静态库的意义上,你的第二种方法并不完全有效。 You just have your OtherProject sources in a different directory but being built as part of MyProject . 您只需将OtherProject源放在不同的目录中,但它是作为MyProject一部分构建的。

So if you truly want to create a static library to hide the implementation, you have to do the "extra" steps. 因此,如果您真的想要创建一个静态库来隐藏实现,那么您必须执行“额外”步骤。 There is no shortcut. 没有捷径。

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

相关问题 Visual Studio C ++多项目解决方案 - Visual Studio C++ multi-project solution 在Visual Studio 2005中调试多项目(C ++)解决方案 - Debugging Multi-Project (C++) Solutions in Visual Studio 2005 C ++ / VS 2012-多项目解决方案:一个项目中出现链接器错误,而另一个项目中没有 - C++ / VS 2012 - Multi-project solution: Linker errors in one project but not another Visual Studio 中用于 C++ 的其他包含目录 - Additional include directories in Visual Studio for C++ 在 Visual Studio 中为 C++ 设置 UnitTest 项目并在主项目中包含其他目录的正确方法是什么? - What is the correct way to set up a UnitTest project for C++ in Visual Studio with additional include directories in the main Project? 如何在C ++中设置多项目解决方案? - How do I set up a multi-project solution in C++? 在VS C ++项目中包含静态库 - Include static library in VS C++ project Visual Studio:项目参考与链接 static lib - Visual Studio: project reference vs link static lib C ++ Windows,使用mingw编译为静态库,并包含在Visual Studio C ++项目中 - c++ windows, compile with mingw as static library and include in visual studio c++ project Visual Studio 2008-在C ++ / C#多项目解决方案中共享数据 - Visual Studio 2008 - Sharing Data in C++/C# Multi Project Solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM