如何将第三方库添加到QtCreator?

时间:2019-06-18 06:51:23

标签: c++ windows qt qt-creator qmake

我正在尝试将OpenXLSX添加到我的QtCreator项目中,但是在this guide之后,我似乎无法让QtCreator找到头文件。

QtCreator手册中提到了该库未使用的.lib文件,所以我对该指南有些迷惑。我四处搜寻,并尝试将OpenXLSX/@library/@openxlsx/interfaces/c++/中的所有标头和源添加到项目树中的标头和源目录中。但是我仍然得到

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

第3行是

#include "OpenXLSX.h"

我也尝试过

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

3rdparty目录与exceltest.pro处于同一位置

我也尝试过使用尖括号。

我不需要OpenXLSX的任何高级功能,只需对我指定给.xlsx或.xls的单元格读写值。 我也不喜欢使用OpenXLSX的想法,因此,如果有人知道excel更好的库,我会接受的。

编辑:因此,在将标题和源添加到项目树之后,我的exceltest.pro看起来像this。 我试过把这条线

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

进入exceltest.h而不是exceltest.cpp,我得到了不同的错误。 QtCreator似乎找到了库文件,但是库有问题吗?这些是错误:

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>
          ^~~~~~~~~~~~~~~~~~~

1 个答案:

答案 0 :(得分:0)

首先,您必须构建OpenXLSX项目来获取该库。该项目使用cmake进行生成。您需要首先生成工作区:

列出所有可用的生成器

cmake --help

选择要使用的那个,然后:

cmake . -G "Your generator"

根据生成器构建项目。库和标头将复制到 install 目录中。

在.pro文件中,添加以下行:

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

第一个允许您包括OpenXLXS标头。链接器将使用第二行将库链接到您的应用。

如果要在Windows或Linux上构建项目,则可能需要使用其他版本的库。您可以使用以下语法:

# 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

在Qt Creator中,如果右键单击项目,则可以使用专用向导添加库(上下文菜单中的Add Library选项)。它将在* .pro

中添加您需要的所有内容
相关问题