依赖于其他静态库的静态库

时间:2018-01-01 14:35:19

标签: c++ visual-studio-2015 static-libraries header-files

我正在写一个游戏,同时为它和我将来可能制作的其他游戏构建引擎。在测试期间,游戏逻辑和引擎(单独)运行良好。但是,当我尝试将它们链接在一起时,我遇到了包含头文件的一些问题。

具体来说,这就是我所做的:

引擎构建为静态库(.lib),并依赖于GLFW静态库(glfw3.lib)。它包含一个Window.h文件:

// Window.h
#pragma once
#include <glfw3.h> // include other library's header

#include <iostream>
//test if the linking success
void test() {
    std::cout << "this is Window.h";
}    

class Window
{
    // Window class code (declaration, function prototype, etc...)
}

*请注意,GLFW库的位置与引擎/游戏项目位置分开。

游戏项目与引擎项目在同一个解决方案下托管。在游戏的属性中,我将引擎添加为依赖项以及引擎的.lib并包含位置。然后我尝试以下方法:

// game's main file
#include <Window.h> 

void main()
{
    test(); // call the test function from Window.h
}

在构建游戏时,我得到一个错误,说它找不到名为“glfw3.h”的文件。但是,如果我在Window.h文件中注释掉了glfw3.h:

# Window.h
#pragma once
//#include <glfw3.h>

然后游戏构建并正常运行,并在测试功能中打印出测试线。

我想将引擎构建为带有头文件的单个.lib,头文件为其类创建函数原型;所以在游戏项目中我只需要包含/依赖引擎库而不需要关心GLFW等。我怎么能做到这一点? 我找到了similar question,但似乎答案无法解决我的问题。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下相关帖子中的建议。基本上按照您的方式构建库,然后将其与glfw静态库静态合并。

How to merge two windows vc static library into one