[英]Qt link library error: “cannot find -lmylib”
大家好,我正在学习 qt5 并尝试制作自己的共享库; 我遵循了我可以在网上找到的大多数教程,但这是一个我无法解决的链接错误。 这是文件结构:
我创建了一个名为 QTest2 的子目录项目,并添加了一个测试项目“mytest”和库项目“mylib”; 然后通过单击“mytest”上的“添加库”->“内部库”,该库应该链接到它。 请看一下代码:
mylib.pro:
QT -= gui
TEMPLATE = lib
DEFINES += MYLIB_LIBRARY
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
mylib.cpp
HEADERS += \
mylib_global.h \
mylib.h
# Default rules for deployment.
unix {
target.path = /usr/lib
}
!isEmpty(target.path): INSTALLS += target
mylib.h:
#ifndef MYLIB_H
#define MYLIB_H
#include "mylib_global.h"
#include <QDebug>
class MYLIB_EXPORT Mylib
{
public:
Mylib();
int test();
};
#endif // MYLIB_H
mylib_global.h:
#ifndef MYLIB_GLOBAL_H
#define MYLIB_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(MYLIB_LIBRARY)
# define MYLIB_EXPORT Q_DECL_EXPORT
#else
# define MYLIB_EXPORT Q_DECL_IMPORT
#endif
#endif // MYLIB_GLOBAL_H
mylib.cpp:
#include "mylib.h"
Mylib::Mylib()
{
}
int Mylib::test()
{
qDebug()<<"Hello World"<<endl;
int sum = 0;
for(int i = 0; i < 10; i++) {
sum += i;
}
qDebug()<<sum<<endl;
return sum;
}
mytest.pro
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../mylib/release/ -lmylib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../mylib/debug/ -lmylib
INCLUDEPATH += $$PWD/../mylib
DEPENDPATH += $$PWD/../mylib
主.cpp:
#include <QCoreApplication>
#include <QDebug>
#include "../mylib/mylib_global.h"
#include "../mylib/mylib.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Mylib mylib;
int result = mylib.test();
qInfo() << result << endl;
return a.exec();
}
我很确定我没有错过教程中的任何步骤,并且还尝试重建项目,但每次我刚得到
:-1: error: cannot find -lmylib
:-1: error: collect2.exe: error: ld returned 1 exit status
平台:Qt 5.14、Qt Creator 4.11.0、Windows 10
任何建议将不胜感激。 我已经被困了好几天了:(
我建议你不要使用 Qt 的导出/导入宏。 您可以在不使用宏的情况下导出每个符号(类、方法...)。 从class MYLIB_EXPORT Mylib
和#ifndef MYLIB_H
和#define MYLIB_H
中删除mylib_global.h
、 MYLIB_EXPORT
。
首先,您必须创建共享库:
#library.pro
TARGET = libraryName
TEMPLATE = lib
CONFIG += c++11 warn_off
DESTDIR = $$PWD/lib
INCLUDEPATH += path/to/source
include(path/to/source/source.pri)
source.pri 可能具有以下结构:
HEADERS += \
$$PWD/mylib.h
SOURCE += \
$$PWD/mylib.cpp
重建library.pro
和共享库将在DESTDIR
中创建。 然后,您必须像在mytest.pro
中那样链接库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.