Qt Ceator 中加入 第三方 库 的 简便 方法

本文介绍如何在Qt Creator 1.3.1中简便地添加第三方库,避免直接编辑Makefile可能导致的问题。通过在项目文件(.pro)中设置头文件路径、库文件路径及库变量LIBS和INCLUDEPATH,可以轻松链接外部库。例如,使用LIBS添加库路径和库名,使用INCLUDEPATH指定头文件路径。

         最近在 用 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 就 没有一点问题 了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值