[英]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: 根据我的理解,要使用静态库完成此操作,我必须:
...and everything should be peachy. ......一切都应该是桃子的。 However, if I skip everything above and only include the source files of OtherProject , like this:
但是,如果我跳过上面的所有内容并且只包含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 问题
(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.