最近在 用 QtCreator 1.3.1的 时候 ,发现 不知道 从 那里 加载 第三方库 ,在 网上 收了 ,大多 说 的 是 改 Makefile文件 ,Makefile 文件 里面 一大堆 东西 看起来 很 麻烦 ,而且 一不小心改 错了 地方 ,会 导致 Makefile 崩溃 的 。其实 ,Qt Creator1.3.1用到的是qmake 来产生 Makefile 文件 。我们 可以 在我们到 项目 文件 (.pro)中添加我们 需要到 头文件 路径 和 库文件路径 、库文件 。
先看看qmake到 文档相关说明吧 。
Declaring Other Libraries
If you are using other libraries in your project in addition to those supplied with Qt, you need to specify them in your project file.
The paths that qmake searches for libraries and the specific libraries to link against can be added to the list of values in the LIBS variable. The paths to the libraries themselves can be given, or the familiar Unix-style notation for specifying libraries and paths can be used if preferred.
For example, the following lines show how a library can be specified:
LIBS += -L/usr/local/lib -lmath
The paths containing header files can also be specified in a similar way using the INCLUDEPATH variable.
For example, it is possible to add several paths to be searched for header files:
INCLUDEPATH = c:/msdev/include d:/stl/include
假设 我们的工程创建一个 project.pro 的 qmake 工程文件,例如:
# Input
SOURCES
= main.cpp Dialog.cpp Emitter.cpp
HEADERS
= Dialog.hpp Emitter.hpp
CONFIG
+= qt
TEMPLATE
= app
TARGET
=
DEPENDPATH
+= .
INCLUDEPATH
+= .
我们为了 用boost_thread库,其中:
头文件 在:/usr/local/include/boost/thread.hpp
库文件 在:/usr/local/lib/boost/libboost_thread.so 和
/usr/local/lib/boost/libboost_thread.a
则 可以 在 项目文件(project.pro)中把 他们(路径) 分别加入到 qmake的 变量 INCLUDEPATH
和 LIBS
中去
如 下:
INCLUDEPATH
+= /usr/local/include/boost
LIBS
+= -L/usr/local/lib/boost/ -lboost_thread
这样在用qmake生成到 Makefile文件中就有了:
####### Compiler, tools and options
CC = gcc
CXX = g++
DEFINES = -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED
CFLAGS = -m64 -pipe -g -Wall -W -D_REENTRANT $(DEFINES)
CXXFLAGS = -m64 -pipe -g -Wall -W -D_REENTRANT $(DEFINES)
INCPATH = -I/usr/local/Trolltech/Qt-4.6.2/mkspecs/linux-g++-64 -I. -I/usr/local/Trolltech/Qt-4.6.2/include/QtCore -I/usr/local/Trolltech/Qt-4.6.2/include/QtGui -I/usr/local/Trolltech/Qt-4.6.2/include -I. -I/usr/local/include/boost
-I.
LINK = g++
LFLAGS = -m64 -Wl,-rpath,/usr/local/Trolltech/Qt-4.6.2/lib
LIBS = $(SUBLIBS) -L/usr/local/Trolltech/Qt-4.6.2/lib -L/usr/local/bin/boost
-lboost_thread
-lQtGui -L/usr/local/Trolltech/Qt-4.6.2/lib -L/usr/X11R6/lib64 -lQtCore -lpthread
AR = ar cqs
RANLIB =
QMAKE = /usr/local/Trolltech/Qt-4.6.2/bin/qmake
TAR = tar -cf
COMPRESS = gzip -9f
COPY = cp -f
SED = sed
COPY_FILE = $(COPY)
COPY_DIR = $(COPY) -r
STRIP = strip
INSTALL_FILE = install -m 644 -p
####### Output directory
OBJECTS_DIR = ./
。。。。。。
相关的boost_thread 库就加入 到项目 中去了,用 make产生 exe 就 没有一点问题 了!
本文介绍如何在Qt Creator 1.3.1中简便地添加第三方库,避免直接编辑Makefile可能导致的问题。通过在项目文件(.pro)中设置头文件路径、库文件路径及库变量LIBS和INCLUDEPATH,可以轻松链接外部库。例如,使用LIBS添加库路径和库名,使用INCLUDEPATH指定头文件路径。
1666

被折叠的 条评论
为什么被折叠?



