一、Qt工程配置
建立工程,选择New Project -> Library -> C++ Library:
去除Qt库依赖,取消选择QtCore:
配置完成后,因为没有选择Qt相关的库,直接编译会报错,可以删除global.h文件,其余.cpp、.h文件的内容也可以换成自己的。除了文件中没有main()函数,其余操作和一般的编程实现功能没有区别。编译完成后可以生成动态库。
本文测试的动态库源码文件内容如下:
//**version.h**
#ifndef VERSION_H
#define VERSION_H
extern int outputVersion();
#endif // VERSION_H
//**version.cpp**
#include "version.h"
#include <stdio.h>
int outputVersion()
{
float version = 3.0;
printf("software version: %.1f \n", version);
return 0;
}
二、.pro文件设置
TARGET = version表示生成的目标文件名为version;
设置TEMPLATE = lib,表示模板为动态库;
加上CONFIG += plugin,只生成一个.so文件,如果不添加,则会出现.so.1 .so.1.0 .so.1.0.0这类带版本号的软链接;
本文不依赖于Qt库,因此在.pro文件中设置了CONFIG -= qt;
qmake添加DEFINE +=变量的值作为编译器C预处理器宏(-D选项),与代码中加上#define的功能类似;
#-------------------------------------------------
#
# Project created by QtCreator 2022-08-30T10:09:54
#
#-------------------------------------------------
TARGET = version
TEMPLATE = lib
CONFIG += plugin
CONFIG -= qt
DEFINES += VERSION_LIBRARY
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as 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 you use 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 += version.cpp
HEADERS += version.h
unix {
target.path = /usr/lib
INSTALLS += target
}
进行编译后,生成动态库libversion.so:
三、查看可执行程序的依赖库
如果是用于PC端的程序,查看可执行文件的依赖库可以使用ldd(list dynamic dependencies)命令:
ldd <可执行文件名>
对于交叉编译生成的可执行文件以及动态库文件,ldd无法识别,可以使用objdump命令或者readelf命令查看文件的依赖库:
objdump -x image_process | grep NEEDED
readelf -a image_process | grep NEEDED
显示结果如下:
0x00000001 (NEEDED) Shared library: [libopencv_core.so.3.4]
0x00000001 (NEEDED) Shared library: [libopencv_features2d.so.3.4]
0x00000001 (NEEDED) Shared library: [libopencv_imgcodecs.so.3.4]
0x00000001 (NEEDED) Shared library: [libopencv_imgproc.so.3.4]
0x00000001 (NEEDED) Shared library: [libopencv_objdetect.so.3.4]
0x00000001 (NEEDED) Shared library: [libopencv_calib3d.so.3.4]
0x00000001 (NEEDED) Shared library: [libQt5Network.so.5]
0x00000001 (NEEDED) Shared library: [libQt5Core.so.5]
0x00000001 (NEEDED) Shared library: [libpthread.so.0]
0x00000001 (NEEDED) Shared library: [libstdc++.so.6]
0x00000001 (NEEDED) Shared library: [libm.so.6]
0x00000001 (NEEDED) Shared library: [libgcc_s.so.1]
0x00000001 (NEEDED) Shared library: [libc.so.6]
0x00000001 (NEEDED) Shared library: [ld-linux-armhf.so.3]
参考链接
【1】https://blog.csdn.net/whatday/article/details/88561944