By Donotalo


2009-04-05 06:08:14 8 Comments

How can I add external library into a project built by Qt Creator RC1 (version 0.9.2)? For example, the win32 function EnumProcesses() requires Psapi.lib to be added in the project to build.

8 comments

@Dnyaneshwar 2018-12-14 13:55:55

in .pro: LIBS += Ole32.lib OleAut32.lib Psapi.lib advapi32.lib

in .h/.cpp: #pragma comment(lib,"user32.lib")

#pragma comment(lib,"psapi.lib")

@zar 2016-05-25 21:45:00

I would like to add for the sake of completeness that you can also add just the LIBRARY PATH where it will look for a dependent library (which may not be directly referenced in your code but a library you use may need it).

For comparison, this would correspond to what LIBPATH environment does but its kind of obscure in Qt Creator and not well documented.

The way i came around this is following:

LIBS += -L"$$_PRO_FILE_PWD_/Path_to_Psapi_lib/"

Essentially if you don't provide the actual library name, it adds the path to where it will search dependent libraries. The difference in syntax is small but this is very useful to supply just the PATH where to look for dependent libraries. It sometime is just a pain to supply each path individual library where you know they are all in certain folder and Qt Creator will pick them up.

@dirkgently 2009-04-05 07:37:43

Are you using qmake projects? If so, you can add an external library using the LIBS variable. E.g:

win32:LIBS += path/to/Psapi.lib

@Dfr 2013-07-16 19:06:38

Sorry for lame question, but should psapi library be already prebuilt or QtCreator will take care of building it ?

@adlag 2015-08-19 21:26:39

If you want to deploy your application on machines of customers, rather than using your application only yourself, we find that the LIBS+= -Lxxx -lyyy method can lead to confusion if not problems.

We develop applications for Linux, Mac and Windows using Qt. We ship complete, stand-alone applications. So all non-system libraries should be included in the deployment package. We want our customers to be able to run the application from the same USB stick for all OSs. For reasons of platform compatibility the USB stick must then be formatted as FAT32, which does not support (Linux) symlinks.

We found the LIBS+= -Lxxx -lyyy idiom too much of a black box:

  1. We do not exactly know what the filepath is of the (static or dynamic) library that has been found by the linker. This is inconvenient. Our Mac linker regularly found libs different from the ones we thought that should be used. This happened several times with OpenSSL libraries where the Mac linker found and used its own - older, incompatible - OpenSSL version rather than our requested version.

  2. We cannot afford that the linker uses symlinks to libraries as this would break the deployment package.

  3. We want to see from the name of the library whether we link a static or a dynamic library.

So for our particular case we use only absolute filepaths and check whether they exist. We remove all symlinks.

First we find out what operating system we are using and put this in the CONFIG variable. And, for instance for Linux 64bit, then:

linux64 {
    LIBSSL= $$OPENSSLPATH/linux64/lib/libssl.a
    !exists($$LIBSSL): error ("Not existing $$LIBSSL")
    LIBS+= $$LIBSSL
    LIBCRYPTO= $$OPENSSLPATH/linux64/lib/libcrypto.a
    !exists($$LIBCRYPTO): error ("Not existing $$LIBCRYPTO")
    LIBS+= $$LIBCRYPTO
}

All the dependencies can be copied into deployment package as we know their filepaths.

@QT-ITK-VTK-Help 2012-12-26 10:30:13

And to add multiple library files you can write as below:

INCLUDEPATH *= E:/DebugLibrary/VTK E:/DebugLibrary/VTK/Common E:/DebugLibrary/VTK/Filtering E:/DebugLibrary/VTK/GenericFiltering E:/DebugLibrary/VTK/Graphics E:/DebugLibrary/VTK/GUISupport/Qt E:/DebugLibrary/VTK/Hybrid E:/DebugLibrary/VTK/Imaging E:/DebugLibrary/VTK/IO E:/DebugLibrary/VTK/Parallel E:/DebugLibrary/VTK/Rendering E:/DebugLibrary/VTK/Utilities E:/DebugLibrary/VTK/VolumeRendering E:/DebugLibrary/VTK/Widgets E:/DebugLibrary/VTK/Wrapping

LIBS *= -LE:/DebugLibrary/VTKBin/bin/release -lvtkCommon -lvtksys -lQVTK -lvtkWidgets -lvtkRendering -lvtkGraphics -lvtkImaging -lvtkIO -lvtkFiltering -lvtkDICOMParser -lvtkpng -lvtktiff -lvtkzlib -lvtkjpeg -lvtkexpat -lvtkNetCDF -lvtkexoIIc -lvtkftgl -lvtkfreetype -lvtkHybrid -lvtkVolumeRendering -lQVTKWidgetPlugin -lvtkGenericFiltering

@Ben 2009-04-25 14:22:01

The proper way to do this is like this:

LIBS += -L/path/to -lpsapi

This way it will work on all platforms supported by Qt. The idea is that you have to separate the directory from the library name (without the extension and without any 'lib' prefix). Of course, if you are including a Windows specific lib, this really doesn't matter.

In case you want to store your lib files in the project directory, you can reference them with the $$_PRO_FILE_PWD_ variable, e.g.:

LIBS += -L"$$_PRO_FILE_PWD_/3rdparty/libs/" -lpsapi

@antred 2015-05-20 09:56:18

Is it possible to specify an environment variable as a library path? I'm asking because writing hardcoded path names into the .pro file would break things if a project is developed by multiple people who may not have all the libraries installed in the same locations.

@Summer Sun 2017-09-19 01:59:58

This did work for me, but when I generate a vs project with qmake -tp vc, I find no libs names in Additional Dependencies of the project, but the vs project do run well. Does that mean there is some other methods to add Additional Dependencies in vs?

@Dan Korn 2018-04-02 16:45:10

I don't think you need to specify a path for a built-in Windows library. Our .pro files have lines like this in the win32 section: LIBS += -lGdi32.

@bur 2018-10-15 15:28:51

As @DanKorn noted, for built-in Windows libraries you don't need to (and shouldn't) specify a path.

@GeneCode 2018-11-08 05:40:23

Why can't they just make a GUI to add a library smh.

@martin 2009-11-19 11:11:31

LIBS += C:\Program Files\OpenCV\lib

won't work because you're using white-spaces in Program Files. In this case you have to add quotes, so the result will look like this: LIBS += "C:\Program Files\OpenCV\lib". I recommend placing libraries in non white-space locations ;-)

@adlag 2015-08-19 20:37:18

The newest versions of Qt (Creator) always want (single) forward slashes as directory separator. The only exception is when you use the "system" command under Windows. Then you need to feed the system an escaped backward slash, that is to say two backward slashes. To replace all forward slashes with two backward slashes can be done as follows: WINDIR = $$DIR , WINDIR ~=s,/,\\,g

@fernando 2009-04-21 16:27:45

The error you mean is due to missing additional include path. Try adding it with: INCLUDEPATH += C:\path\to\include\files\ Hope it works. Regards.

Related Questions

Sponsored Content

13 Answered Questions

[SOLVED] What is the effect of extern "C" in C++?

32 Answered Questions

4 Answered Questions

[SOLVED] How to add include path in Qt Creator?

2 Answered Questions

linking libraries in qt creator for c++ project

  • 2012-10-10 17:00:47
  • Ramesh
  • 2656 View
  • 0 Score
  • 2 Answer
  • Tags:   qt-creator

3 Answered Questions

[SOLVED] Adding files inside a Creator project

  • 2013-10-09 05:17:26
  • johnbakers
  • 28016 View
  • 5 Score
  • 3 Answer
  • Tags:   qt qt-creator

1 Answered Questions

[SOLVED] How to add an external .a library in Qt Creator project via GUI?

  • 2012-01-04 13:03:38
  • Dmitriy
  • 7311 View
  • 4 Score
  • 1 Answer
  • Tags:   qt qt-creator

1 Answered Questions

[SOLVED] Using External Lib/DLLs in Qt Creator?

  • 2010-10-04 19:21:45
  • George Geankins
  • 23497 View
  • 4 Score
  • 1 Answer
  • Tags:   c++ qt

Sponsored Content