简体   繁体   中英

How do I go about adding a third party library to QtCreator?

I'm trying to add OpenXLSX to my QtCreator project but following this guide I can't seem to get QtCreator to find the header file.

The QtCreator manual mentions .lib files which this library doesn't use so I'm kinda lost with that guide. I googled around and tried adding all the headers and sources from OpenXLSX/@library/@openxlsx/interfaces/c++/ to my headers and sources directories in the project tree. Yet I still get

exceltest.cpp:3: error: 'OpenXLSX.h' file not found

Line 3 is

#include "OpenXLSX.h"

I've also tried

#include "3rdparty/OpenXLSX/@library/@openxlsx/interfaces/c++/headers/OpenXLSX.h"

The 3rdparty directory being in the same location as exceltest.pro

I've also tried both with angle brackets.

I don't need any advanced functionality from OpenXLSX, just reading and writing values to cells I specify to either .xlsx or .xls. I'm also not married to the idea of using OpenXLSX so if anyone knows excel any libraries that would work better I'm open to the idea.

EDIT: So after I added my headers and sources to the project tree, my exceltest.pro looks like this . I tried putting this line

#include "3rdparty/OpenXLSX/@library/@openxlsx/interfaces/c++/headers/OpenXLSX.h"

into exceltest.h instead of exceltest.cpp and I'm getting different errors. QtCreator seems to find the library files but is something wrong with the library? These are the errors:

In file included from J:/George/Coding/Qt/Test/exceltest/3rdparty/OpenXLSX/@library/@openxlsx/interfaces/c++/headers/XLCell.h:49:0,
                 from ..\exceltest\3rdparty\OpenXLSX\@library\@openxlsx\interfaces\c++\sources\XLCell.cpp:5:
J:/George/Coding/Qt/Test/exceltest/3rdparty/OpenXLSX/@library/@openxlsx/interfaces/c++/headers/XLDefinitions.h:57:35: warning: multi-character character constant [-Wmultichar]
     constexpr uint32_t maxRows = 1'048'576;
                                   ^~~~~
J:/George/Coding/Qt/Test/exceltest/3rdparty/OpenXLSX/@library/@openxlsx/interfaces/c++/headers/XLDefinitions.h:59:36: warning: missing terminating ' character
     constexpr uint16_t maxCols = 16'384;
                                    ^
J:/George/Coding/Qt/Test/exceltest/3rdparty/OpenXLSX/@library/@openxlsx/interfaces/c++/headers/XLDefinitions.h:59:36: error: missing terminating ' character
     constexpr uint16_t maxCols = 16'384;
                                    ^~~~~
..\exceltest\3rdparty\OpenXLSX\@library\@openxlsx\interfaces\c++\sources\XLCellRange.cpp:5:10: fatal error: XLCellRange.h: No such file or directory
 #include <XLCellRange.h>
          ^~~~~~~~~~~~~~~
compilation terminated.
..\exceltest\3rdparty\OpenXLSX\@library\@openxlsx\interfaces\c++\sources\XLCellReference.cpp:5:10: fatal error: XLCellReference.h: No such file or directory
 #include <XLCellReference.h>
          ^~~~~~~~~~~~~~~~~~~

First, you have to build the OpenXLSX project to get the library. The project uses cmake for generation. You need to generate the worksapce, first:

List all the available generators with

cmake --help

Chose the one you want to use, then:

cmake . -G "Your generator"

Build your project according to your generator. The libraries and headers will be copied in the install directory.

In your .pro file, add the following lines:

INCLUDEPATH += /path/to/OpenXLSX/include
LIBS += -L/path/to/OpenXLSX/lib -lopenxlsx.lib

The first one allows you to include the OpenXLXS headers. The second line will be used by the linker to link the library to your app.

You may need to use a different version of your library if you want to build your project on Windows or Linux. You can use this syntax:

# On Windows in release mode
win32:CONFIG(release, debug|release): LIBS += -L/path/to/OpenXLSX/lib -lopenxlsx.dll

#On Windows debug mode
else:win32:CONFIG(debug, debug|release): LIBS +=  -L/path/to/OpenXLSX/lib -lopenxlsx_debug.dll

#On Linux debug and release
else:unix: LIBS +=  -L/path/to/OpenXLSX/lib -lopenxlsx.so

In Qt Creator, if you right-click on your project, you can use the dedicated wizard to add a library ( Add Library option in the context menu). It will add everything you need in your *.pro

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM