一、注意事项
Qt使用MSVC编译,MSVC生成 .dll 与 .lib 文件,MINGW生成 .dll 与 .a 文件。
二、生成dll库
1.选择C++ Library后按步骤创建一个工程。
2.将原先的文件添加到内部,pro需要注意用到的配置也添加进去。(我用到了串口所以也需要添加QT += serialport )
3.根据需求配置外部调用的函数以及实现功能。(因为运行完程序就会结束,如果有使用到触发时钟,需要加上exec)
4.确认代码无问题后,用🔨构建编译生成dll与lib文件。
三、VC调用dll
1.创建一个控制台应用。
2.拷贝dll、lib和两个头文件(创建时候就生成的头文件)到工程内 ,拷贝dll、lib到编译生成Release文件夹内。
3.工程内添加lib与两个头文件。
4.将两个.h所用到的Qt的头文件屏蔽,并把global.h结尾的文件里添加内容如下:
#if defined(_MSC_VER) || defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
# define Q_DECL_EXPORT __declspec(dllexport)
# define Q_DECL_IMPORT __declspec(dllimport)
#else
# define Q_DECL_EXPORT __attribute__((visibility("default")))
# define Q_DECL_IMPORT __attribute__((visibility("default")))
#endif
5.编写调用制行。(注意VC与Qt编译的MSVC的32位与64位需要对应)
参考文章: