C++ 在 Qt Creator 中使用静态库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1361229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a static library in Qt Creator
提问by Dan O
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"). It compiles and spits out a ".a file".
我已经使用 Qt Creator 创建并编译了我的静态库(New=>Projects\C++ Library=>Set type to“Statically Linked Library”)。它编译并吐出一个“.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 文件等),但我不知道链接库或包含库中文件的正确方法。
回答by Dewfy
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]
回答by rpg
In your project that uses the library make the LIBSvariable point to your lib's path.
To include files from the library, add the library folder to the INCLUDEPATHand then do a regular #include in your code files.
在使用库的项目中,使LIBS变量指向您的库路径。
要包含库中的文件,请将库文件夹添加到INCLUDEPATH,然后在代码文件中执行常规 #include。
e.g:
例如:
# 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). The only limitation to this is the library must exist, for qmake to find which directory a -l lib lives in.
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; for example, libmath2.lib could potentially be used instead of libmathlib. To avoid this ambiguity, we recommend that you explicitly specify the library to be used by including the .lib file name suffix.
此变量包含要链接到项目中的库列表。您可以使用 Unix -l(库)和 -L(库路径)标志,qmake 将在 Windows 上对这些库执行正确的操作(即,这意味着将库的完整路径传递给链接器)。唯一的限制是库必须存在,以便 qmake 找到 -l lib 所在的目录。
注意:在 Windows 上,使用 -l 选项指定库,如上例所示,将导致使用版本号最高的库;例如,可以使用 libmath2.lib 代替 libmathlib。为避免这种歧义, 我们建议您通过包含 .lib 文件名后缀来明确指定要使用的库。
回答by Cavalieri Giovanni
..from QT project creator
..来自 QT 项目创建者
- goto projectName.pro from left hand side menu
- type LIBS +=
- rightClick AddLibrary
- 从左侧菜单转到 projectName.pro
- 类型 LIBS +=
- 右击添加库
回答by Temak
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
.
In this case on my linux with QMake v 3.0the dynamic one is linked by default.
To force linkage with staticone you need to specify it explicitly without any options.
如果您在同一文件夹中同时拥有静态库libLIBNAME.a
和动态libLIBNAME.so
库,则不起作用 PATH_TO_LIB_DIR
。
在这种情况下,在我的带有QMake v 3.0的linux 上,默认情况下链接的是动态的。
要强制与静态链接,您需要明确指定它而不带任何选项。
LIBS += PATH_TO_LIB_DIR/libLIBNAME.a
回答by Know-One
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...
这应该很容易,但由于某种原因,很难找到搜索结果,因为有很多人在论坛上寻求帮助,我已经按照我能得到的每一条提示进行操作,但没有任何帮助。 ..