静态链接库(Lib)与动态链接库(DLL)
如果出于某种原因,不想将源代码暴露给别人,就需要使用到库。库有动态链接库和静态链接库。静态连接库就是把(lib)文件中用到的函数代码直接链接进目标程序,程序运行的时候不再需要其它的库文件。动态链接就是把调用的函数所在文件模块(DLL)和调用函数在文件中的位置等信息链接进目标程序,程序运行的时候再从DLL中寻找相应函数代码,因此需要相应DLL文件的支持。
为什么要使用DLL(动态链接库)?
代码复用是提高软件开发效率的重要途径。一般而言,只要某部分代码具有通用性,就可以将它构造成相对独立的功能模块并在之后的项目中重复使用。比较常见的例子是各种应用程序框架,它们都以源代码的形式发布。由于这种复用是源代码级别的,源代码完全暴露给了程序员,因而称之为“白盒复用”。白盒复用有以下三个缺点:
-
暴露源代码,多份拷贝,造成存储浪费;
-
容易与程序员的本地代码发生命名冲突;
-
更新模块功能比较困难,不利于问题的模块化实现;
为了弥补这些不足,就提出了“二进制级别”的代码复用了。使用二进制级别的代码复用一定程度上隐藏了源代码,对于“黑盒复用”的途径不只DLL一种,静态链接库,甚至更高级的COM组件都是。
使用DLL主要有以下优点:
- 使用较少的资源;当多个程序使用同一函数库时,DLL可以减少在磁盘和物理内存中加载的代码的重复量。这不仅可以大大影响在前台运行的程序,而且可以大大影响其它在Windows操作系统上运行的程序;
- 推广模块式体系结构;
- 简化部署与安装。
将C++源码打包为DLL文件步骤如下:
1. 创建动态链接库(DLL)项目
至此创建完成,包含源文件dllmain.cpp、pch.cpp,头文件framework.h、pch.h。
2. 定义宏
在头文件pch.h中定义即可,宏的作用的是允许该函数能够被外部访问,并直接调用。
// pch.h: 这是预编译标头文件。
// 下方列出的文件仅编译一次,提高了将来生成的生成性能。
// 这还将影响 IntelliSense 性能,包括代码完成和许多代码浏览功能。
// 但是,如果此处列出的文件中的任何一个在生成之间有更新,它们全部都将被重新编译。
// 请勿在此处添加要频繁更新的文件,这将使得性能优势无效。
#ifndef PCH_H
#define PCH_H
// 添加要在此处预编译的标头
#include "framework.h"
#endif //PCH_H
#ifdef IMPORT_DLL
#else
#define IMPORT_DLL extern "C" _declspec(dllimport)//指定允许给其他外部调用
#endif
IMPORT_DLL int ShowGetFrontEnd(const char* pszWavFile);
IMPORT_DLL int ShowTone(const char* pszWavFile);
IMPORT_DLL double ShowToneDuration(const char* pszWavFile);
IMPORT_DLL double ShowVoicePitch(const char* pszWavFile);
IMPORT_DLL double ShowVoiceStrength(const char* pszWavFile);
这里分别添加ShowGetFrontEnd()、ShowTone()、ShowToneDuration()、ShowVoicePitch()和ShowVoiceStrength()五个可供外部调用函数的宏定义,并在pch.cpp中添加具体实现函数:
// pch.cpp: 与预编译标头对应的源文件
#include "pch.h"
// 当使用预编译的头时,需要使用此源文件,编译才能成功。
#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
#include <assert.h>
#include "test.h"
#include "Sound.h"
using namespace std;
/* @brief 显示声调
* @param1 pszWavFile[in] 音频文件
* @return 0表示成功,非0表示失败,错误见common.h
*/
int ShowTone(const char* pszWavFile)
{
assert(pszWavFile);
int nTone = 0;
int nRet = 0;
nRet = DetectTone(pszWavFile, &nTone);
return nTone;
}
/* @brief 显示音长
* @param1 pszWavFile 音频文件
* @return 0表示成功,非0表示失败,错误见common.h
*/
double ShowToneDuration(const char* pszWavFile)
{
assert(pszWavFile);
double dwDuration = 0.0;
int nRet = 0;
nRet = DetectVoiceDuration(pszWavFile, &dwDuration);
return dwDuration;
}
/* @brief 显示音频能量(声强)
* @param1 pszWavFile 音频文件
* @return 0表示成功,非0表示失败,错误见common.h
*/
double ShowVoiceStrength(const char* pszWavFile)
{
assert(pszWavFile);
double energy = 0.0;
int nTone = 0;
int nRet = 0;
nRet = DetectVoiceStrength(pszWavFile, &energy);
return energy;
}
/* @brief 显示音频(声调)
* @param1 pszWavFile 音频文件
* @return 0表示成功,非0表示失败,错误见common.h
*/
double ShowVoicePitch(const char* pszWavFile)
{
assert(pszWavFile);
double pitch = 0.0;
int nTone = 0;
int nRet = 0;
nRet = DetectVoicePitch(pszWavFile, &pitch);
return pitch;
}
/* @brief
* @param1
* @param2
* @return
*/
int ShowGetFrontEnd(const char* pszWavFile)
{
assert(pszWavFile);
float front = 0.0;
float end = 0.0;
int nRet = 0;
nRet = GetFrontEnd(pszWavFile, &front, &end);
if (nRet == 0)
{
cout << "Detect front and end information is silence: " << endl;
cout << "front: " << front << endl;
cout << "end: " << end << endl;
}
else
{
cout << "Detect front and end information isnot silence: " << endl;
cout << "front: " << front << endl;
cout << "end: " << end << endl;
}
return nRet;
}
3. 添加其他函数源文件与头文件
由于上述5个函数的实现依赖于傅里叶变化、支持向量机(SVM)等,因此需添加相对应的.cpp源文件和.h头文件,最终项目文件组成如下:
4. 编译生成DLL文件
点击开始调试(F5),出现错误1:在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include “pch.h””?
解决:点击项目属性:
将预编译头设置为"不使用预编译头"。
重新编译,出现错误2:error C4996: ‘fopen’: This function or variable may be unsafe. Consider using fopen_s instead.
解决:在预处理器定义添加“_CRT_SECURE_NO_WARNINGS”
重新编译,在项目Debug文件夹下生成相应.dll文件:
5. DLL文件的调用
在另外一个项目中可在不知道源码的情况下直接调用DLL接口得到函数返回的结果:
#include <iostream>
#include <Windows.h>
#include <stdio.h>
using namespace std;
typedef int (*function_tone)(const char* pszWavFile);
typedef double (*function_voice)(const char* pszWavFile);
int main()
{
const char* pszWavPath = "E:/xxxx/语音测试打包/Voice/test4.wav";
HINSTANCE hDllInst;
hDllInst = LoadLibrary(L"voice.dll");
if (hDllInst) {
function_voice ShowToneDuration = (function_voice)GetProcAddress(hDllInst, "ShowToneDuration");
function_voice ShowVoiceStrength = (function_voice) GetProcAddress(hDllInst, "ShowVoiceStrength");
function_voice ShowVoicePitch = (function_voice) GetProcAddress(hDllInst, "ShowVoicePitch");
function_tone ShowTone = (function_tone)GetProcAddress(hDllInst, "ShowTone");
printf("语音文件为:%s\n", pszWavPath);
printf("音长:%.3lf(s)\n", ShowToneDuration(pszWavPath));
printf("音强:%.3lf(db)\n", ShowVoiceStrength(pszWavPath));
printf("音高:%.3lf(Hz)\n", ShowVoicePitch(pszWavPath));
printf("声调:%d\n", ShowTone(pszWavPath));
}
return 0;
}
结果: