简体   繁体   English

在Qt Creator中使用静态库

[英]Using a static library in Qt Creator

I'm having a hell of a time finding documentation which clearly explains how to use a static library in Qt Creator. 我真不愿意花时间寻找文档,该文档清楚地说明了如何在Qt Creator中使用静态库。

I've created and compiled my static library using Qt Creator (New=>Projects\\C++ Library=>Set type to "Statically Linked Library"). 我已经使用Qt Creator创建并编译了我的静态库(New => Projects \\ C ++ Library =>将类型设置为“静态链接库”)。 It compiles and spits out a ".a file". 它编译并弹出一个“ .a文件”。

The problem I encounter is when I try to use the library. 我遇到的问题是当我尝试使用该库时。 I have another project that would like to use it (#include files in the library, etc) but I don't know the proper way to link with the library or include files from the library. 我有另一个项目想要使用它(库中的#include文件等),但我不知道与库链接或包含库中文件的正确方法。

LIBS += -L[path to lib] -l[name of lib]

Note! 注意! that filename of lib: lib[nameOfLib].a and you have to pass only original part -l[nameOfLib] lib的文件名:lib [nameOfLib] .a,您只需传递原始部分-l [nameOfLib]

In your project that uses the library make the LIBS variable point to your lib's path. 在使用该库的项目中,使LIBS变量指向您的lib路径。
To include files from the library, add the library folder to the INCLUDEPATH and then do a regular #include in your code files. 要包括库中的文件,请将库文件夹添加到INCLUDEPATH中 ,然后在代码文件中执行常规的#include。

eg: 例如:

# the binary's .pro  
LIBS += c:/mylibs/math.lib
INCLUDEPATH += c:/mylibs

Edited: 编辑:
-L tells qmake that the path is a directory that it can search for libraries -l tells it that the path is a file, but take note of the observation below. -L告诉qmake该路径是它可以搜索库的目录-l告诉它该路径是一个文件,但请注意以下注意事项。

From the qmake docs: 从qmake文档:

This variable contains a list of libraries to be linked into the project. 此变量包含要链接到项目中的库的列表。 You can use the Unix -l (library) and -L (library path) flags and qmake will do the correct thing with these libraries on Windows (namely this means passing the full path of the library to the linker). 您可以使用Unix -l(库)和-L(库路径)标志,qmake将在Windows上使用这些库执行正确的操作(即,这意味着将库的完整路径传递给链接器)。 The only limitation to this is the library must exist, for qmake to find which directory a -l lib lives in. 对此的唯一限制是必须存在该库,以便qmake查找-l lib所在的目录。

Note: On Windows, specifying libraries with the -l option, as in the above example, will cause the library with the highest version number to be used; 注意:在Windows上,如上例所示,使用-l选项指定库将导致使用版本号最高的库; for example, libmath2.lib could potentially be used instead of libmathlib. 例如,可以使用libmath2.lib代替libmathlib。 To avoid this ambiguity, we recommend that you explicitly specify the library to be used by including the .lib file name suffix. 为避免这种歧义, 建议您通过包括.lib文件名后缀来明确指定要使用的库。

..from QT project creator ..来自QT项目创建者

  1. goto projectName.pro from left hand side menu 从左侧菜单转到projectName.pro
  2. type LIBS += 输入LIBS + =
  3. rightClick AddLibrary 用鼠标右键单击AddLibrary

The variant 变体

 LIBS += -L[PATH_TO_LIB_DIR] -l[LIBNAME] 

doesn't work if you have both static libLIBNAME.a and dynamic libLIBNAME.so libs in the same folder PATH_TO_LIB_DIR . 如果您在同一文件夹PATH_TO_LIB_DIR中同时具有静态libLIBNAME.a和动态libLIBNAME.so ,则PATH_TO_LIB_DIR
In this case on my linux with QMake v 3.0 the dynamic one is linked by default. 在这种情况下,在装有QMake v 3.0的Linux上,默认情况下动态链接为动态链接。
To force linkage with static one you need to specify it explicitly without any options. 强制与静态链接建立链接,您需要显式指定它而无需任何选择。

LIBS += PATH_TO_LIB_DIR/libLIBNAME.a

Is it 是吗

LIBS += -L"/some path" -l"somename.a"

or 要么

LIBS += -L/somepath -lsomename.a

or 要么

LIBS += -L/somepath -lsomename"

This should be as easy as it gets but for some reason it is EXTREMELY hard to pull up a search result for because there are so many hits of forums of people asking for help and I've followed every tip I can get but no help... 这应该很容易,但是由于某种原因,要找到一个搜索结果是非常困难的,因为在论坛上有很多人寻求帮助,而我一直在追踪我能得到的每条提示,但没有帮助。 ..

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

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