使用库时如何修复 "error C1083: Cannot open include file :'simpleclass.h' :no such file or directory"?

How to fix "error C1083: Cannot open include file :'simpleclass.h' :no such file or directory" when using a library?

我有一个非常非常简单的 C++ class,工具是 QT Creator。 我创建了一个 非 QT 项目 。 具有如下所示的 SimpleClass class

simpleclass.h

#ifndef SIMPLECLASS_H
#define SIMPLECLASS_H

class SimpleClass
{
public:
    SimpleClass();
    void printLine();
};

#endif // SIMPLECLASS_H

simpleclass.cpp

#include "simpleclass.h"
#include <iostream>

SimpleClass::SimpleClass()
{
    printf("Hello , This is SimpleClass constructor\n");
}

void SimpleClass::printLine()
{
    printf(" Printing a Line\n");
}

main.cpp

#include <iostream>
#include "simpleclass.h"
using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    SimpleClass sc;
    sc.printLine();
    return 0;
}

一切都很好!

问题和困惑现在开始了! 现在我的目标是创建一个简单的库 testlibrary.lib 和 .dll 并在另一个项目中使用它。

我创建了一个名为 TestLibrary 的 C++ 库项目,并尝试构建一个包含该简单库的库 class(SimpleClass)。

文件如下所示

TestLibrary.pro

QT       += widgets

TARGET = TestLibrary
TEMPLATE = lib

DEFINES += TESTLIBRARY_LIBRARY

DEFINES += QT_DEPRECATED_WARNINGS


SOURCES += \
    simpleclass.cpp

HEADERS += \
        testlibrary_global.h \ 
    simpleclass.h

unix {
    target.path = /usr/lib
    INSTALLS += target
}

testlibrary_global.h

#ifndef TESTLIBRARY_GLOBAL_H
#define TESTLIBRARY_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(TESTLIBRARY_LIBRARY)
#  define TESTLIBRARYSHARED_EXPORT Q_DECL_EXPORT
#else
#  define TESTLIBRARYSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // TESTLIBRARY_GLOBAL_H

simpleclass.h

#ifndef TESTLIBRARY_H
#define TESTLIBRARY_H

#include "testlibrary_global.h"

class TESTLIBRARYSHARED_EXPORT SimpleClass
{

public:
    SimpleClass();
    void printLine();
};

#endif // TESTLIBRARY_H

同样,我有 simpleclass.cpp .

在构建时,确实创建了库 TestLibrary.lib 和 TestLibrary.dll,但我无法在另一个名为 UseLibrary 的项目中使用它。 我在一个新项目 UseLibrary 下创建了一个目录 libs 并在那里复制了 TestLibrary.lib 和 TestLibrary.dll。

UseLibrary.pro 文件如下所示

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += \
        main.cpp \
    main.cpp

win32: LIBS += -L$$PWD/libs/ -lTestLibrary

INCLUDEPATH += $$PWD/libs
DEPENDPATH += $$PWD/libs

main.cpp(在项目UseLibrary中)

#include <iostream>
#include "simpleclass.h" ---> error C1083: Cannot open include file :'simpleclass.h' :no such file or directory. 

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    SimpleClass sc; --> Unknown Type name 'Simpleclass', # include header itself giving error so obvious issue
    sc.printLine();
    return 0;
}

#include "simpleclass.h" ---> error C1083: Cannot open include file :'simpleclass.h' :no such file or directory. 为什么我会收到此错误? 我阅读了很多与库创建相关的帖子,但我很困惑为什么大多数帖子都有

#include <QWidget>#include <QtGlobal>为什么我们需要这些??

至少我从来没有在我的代码中使用过这些框架 class!这些真的是图书馆创建所必需的吗?为什么? 据我了解,我的项目是一个非 QT 项目,所以我不需要这些。谁能指出我的理解是否正确? 我错过了什么?最后,为什么没有包含 simpleclass.h 头文件?

原因

您的包含路径设置为 $$PWD/libs:

INCLUDEPATH += $$PWD/libs

但屏幕截图并未在该位置显示 simpleclass.h

解决方案

  1. UseLibrary.pro删除:

     win32: LIBS += -L$$PWD/libs/ -lTestLibrary
    
     INCLUDEPATH += $$PWD/libs
     DEPENDPATH += $$PWD/libs
    
  2. 在QtCreator的项目浏览器中右击UseLibrary项目,selectAdd library

  3. Select External library

  4. 重要:首先点击浏览“包含路径”行和select路径,其中simpleclass.h

  5. 现在,单击浏览“库文件”行和 select 路径,其中 TestLibrary.lib

注意:如果您首先select一个库文件路径,QtCreator 会误导性地用相同的路径填写包含路径框。按照上述步骤应该可以帮助您解决这个问题。