逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::

Using a third-party library with Qt is a simple process. Suppose you know of a cross-platform library that accepts audio samples of a cat's meows and translates them into English words. This library is named CatWhisperer, and has several files that it provides as part of its library. Your project, MyQtApp, stores these files in a folder named 3rdparty:

  • MyQtApp/
    • MyQtApp.pro
    • src/
      • main.cpp
    • 3rdparty/
      • CatWhisperer
        • include/
          • CatWhisperer.h
        • lib/
          • libCatWhisperer.so
          • CatWhisperer.lib
        • bin/
          • CatWhisperer.dll

To use the CatWhisperer library in MyQtAppqmake requires the location and names of the CatWhisperer libraries. Optionally, you can also:

  • Provide the location of the CatWhisperer source code so that you don't have to type out the full path to each file when you include them in your own code.
  • Choose the destination in which the MyQtApp executable will be created.

The information above is provided in the .pro file, so that qmake can parse it and produce makefiles. Makefiles contain all the information needed by your compiler and linker to produce output, whether it is an executable, another library file, etc. The next sections explain the syntax with which qmakeexpects you to provide this information.

Source code

To be able to write

#include <CatWhisperer.h>

instead of

#include <3rdparty/CatWhisperer/include/CatWhisperer.h>

you can provide the path to the CatWhisperer include directory, using the INCLUDEPATH variable:

INCLUDEPATH += 3rdparty/CatWhisperer/include

Library files

To let qmake know where to find the CatWhisperer library files, use the LIBS variable:

LIBS += -L"3rdparty/CatWhisperer/lib" -lCatWhisperer

The first part of the expression lets the linker know in which directory it should look for the library files. The double quotes are only necessary when the path contains spaces, so we could have omitted them in this example.

The second part tells the linker which libraries to link against. We have two different library files for UNIX platforms and Windows, respectively:libCatWhisperer.so and CatWhisperer.lib. It is not necessary to specify the .lib extension, nor the lib prefix (on UNIX platforms).

Destination directory

By default, qmake creates the executable in the same directory as the .pro file. We can choose our own directory using the DESTDIR variable:

DESTDIR = bin

That's it! You can now use the CatWhisperer library in your project. The final .pro file looks like this:

TARGET = MyQtApp
TEMPLATE = app
INCLUDEPATH += 3rdparty/CatWhisperer/include
SOURCES += src/main.cpp
LIBS += -L"3rdparty/CatWhisperer/lib" -lCatWhisperer

See also qmake Manual and Adding Libraries to Projects.

@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
posted on 2013-06-28 07:31 逛奔的蜗牛 阅读(939) 评论(0)  编辑 收藏 引用 所属分类: Qt

只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理