简体   繁体   English

链接静态库会导致链接库中的错误

[英]Linking a static library causes errors in the linked library

I have a small library project that uses OpenGL (glfw and glew). 我有一个使用OpenGL(glfw和glew)的小型库项目。 Now, the project compiles fine, but when I create a new project and statically link the library project, VS starts to throw errors in the library project. 现在,项目编译得很好,但是当我创建一个新项目并静态链接库项目时,VS开始在库项目中抛出错误。 Why is that? 这是为什么?

More specifically, I get this error: 更具体地说,我收到此错误:

error C1083: Cannot open include file 'GL/glew.h': No such file or directory (file: trenums3d.h)

The project setup is like this: There's the library project 'Foo', which is compiled into a static library ('Foo.lib'). 项目设置如下:库项目'Foo',它被编译成静态库('Foo.lib')。 The application project 'Bar' links 'Foo' (I added the folder where Foo.lib resides to Bar's 'Additional Library Directories', as well as the source folder of 'Foo' to Bar's 'Additional Include Directories'). 应用程序项目“Bar”链接'Foo'(我添加了Foo.lib驻留在Bar的'其他库目录'的文件夹,以及'Foo'的源文件夹到Bar的'其他包含目录')。 If I compile only the library project, everything works just fine, but compiling the whole solution give me the aforementioned error. 如果我只编译库项目,一切正常,但编译整个解决方案会给我上述错误。

This isn't a proper answer to your question, but just an explanation of the steps required for building an application in a compiled language. 这不是您的问题的正确答案,而只是解释以编译语言构建应用程序所需的步骤。

Building a project containing multiple files is a three-step process: 构建包含多个文件的项目分为三个步骤:

  1. Creation and editing of source and header files 创建和编辑源文件和头文件
  2. Compilation of the source files (this step contains many sub-steps ). 编译源文件(此步骤包含许多子步骤 )。 This step creates object files of all translation units 此步骤创建所有翻译单元的目标文件
  3. Linking of all object files and libraries to form the final executable 链接所有目标文件和库以形成最终的可执行文件

Error like the one shown in your question is emitted in the second step. 像第二步中发出的问题中显示的错误。 Linking with libraries happens in a completely different step, and is usually done by a different program than the compiler. 与库的链接发生在完全不同的步骤中,并且通常由与编译器不同的程序完成。


To answer your question, if linking with a static library also requires linking with the other libraries that the static library depend on, then the answer is normally yes. 要回答您的问题,如果与静态库链接还需要链接静态库所依赖的其他库,那么答案通常是肯定的。 Static libraries only contain the function in the actual libraries, you can look at a static library more as a collection or archive of object files. 静态库只包含实际库中的函数,您可以将静态库更多地视为对象文件的集合或归档。 Static libraries does not contain any information about other libraries they depend on. 静态库不包含有关它们所依赖的其他库的任何信息。


And as for your problem, with the pre-processor error, it's because you include a header file from your static library, and that header file in turn includes some header files. 至于你的问题,由于预处理器错误,这是因为你包含了静态库中的头文件,而该头文件又包含一些头文件。 But the pre-processor doesn't have the secondary included header files in its default search path, so you need to add it. 但预处理器在其默认搜索路径中没有辅助包含头文件,因此您需要添加它。

This still have nothing to do with linking any library, this is a pure pre-processor issue, and is handled in step two in my list above. 这仍然与链接任何库无关,这是一个纯粹的预处理器问题,并在上​​面的列表中的第二步处理。

I suspect the header files of your static library look like somewhat this: 我怀疑你的静态库的头文件有点像这样:

#ifndef SOMECLASS_H
#define SOMECLASS_H

#include "GL/glew.h"

// ...

#endif

If you include this header file from another library or application, the compiler will open this file and will see that it needs to open GL/glew.h as well in order to be able to "understand" the definition of your class. 如果您从其他库或应用程序中包含此头文件,编译器将打开此文件,并且将看到它还需要打开GL/glew.h以便能够“理解”您的类的定义。

This means you need to supply at least the header files of glew. 这意味着您至少需要提供glew的头文件。 The only way to get rid of this is if you manage to only reference glew files from your .cpp files but not from your .h files. 摆脱这种情况的唯一方法是,如果你设法只引用.cpp文件中的glew文件,而不是你的.h文件。 In some cases, forward declarations can be used, but not sure if this will work for glew. 在某些情况下,可以使用前向声明,但不确定这是否适用于glew。

Concerning the linker settings: In case your glew library is built statically as well, you may or may not have to supply that library file and link to it from your project. 关于链接器设置:如果您的glew库也是静态构建的,您可能也可能不必提供该库文件并从项目链接到它。 This depends on how you setup your linker for your own static library. 这取决于您为自己的静态库设置链接器的方式。 If you have troubles in this step, create a new question. 如果您在此步骤中遇到问题,请创建一个新问题。

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

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