赞
踩
代码开发过程中会遇到很多已有的函数库,这些函数库是现有的,成熟的,可以复用的代码。现实中每个程序都要依赖很多基础的底层库,不可能每个人的代码都从零开始,因此库的存在意义非同寻常。
本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执行。库有两种:静态链接库和动态链接库。
动态链接库包含了函数所在的DLL文件和函数入口等相关信息,代码由运行时加载在进程空间中的DLL提供,显示调用不需要仅需要动态链接库的dll文件,无需头文件.h和动态链接库.lib文件,隐式调用则需要上述三种文件。
动态库特点:
静态链接库包含了函数实现本身,在编译的时候直接将代码放入程序中。在调用静态链接库的时,需要使用静态链接库的头文件.h以及静态链接库的.lib文件。
静态库特点:
首先新建工程MathLibrary_dll,在源文件中添加MathLibrary.cpp,在头文件中添加MathLibrary.h,具体代码如下:
#pragma once #ifndef MATHLIBRARY #define MATHLIBRARY #ifdef MATHLIBRARY_EXPORTS #define MATHLIBRARY_API __declspec(dellexport) #else #define MATHLIBRARY_API __declspec(dllimport) #endif // MATHLIBRARY_EXPORTS namespace MathLibrary { class MATHLIBRARY_API Arithmetic { public: Arithmetic(); ~Arithmetic(); // Returns a + b double Add(double a, double b); // Returns a - b double Subtract(double a, double b); // Returns a * b double Multiply(double a, double b); // Returns a / b double Divide(double a, double b); private: }; extern "C" MATHLIBRARY_API double power(double a, double ratio); } #endif // !MATHLIBRARY
// MathLibrary.cpp // compile with: cl /c /EHsc MathLibrary.cpp // post-build command: lib MathLibrary.obj #include "MathLibrary.h" #include<math.h> namespace MathLibrary { Arithmetic::~Arithmetic() {}; Arithmetic::Arithmetic() {}; double Arithmetic::Add(double a, double b) { return a + b; } double Arithmetic::Subtract(double a, double b) { return a - b; } double Arithmetic::Multiply(double a, double b) { return a * b; } double Arithmetic::Divide(double a, double b) { return a / b; } double power(double a, double ratio) { return pow(a, ratio); } }
#include <iostream> #include "MathLibrary.h" using namespace MathLibrary; int main() { double a = 7.0; int b = 99; double ratio = 2.0; Arithmetic *demo_class = new Arithmetic(); std::cout <<"Add结果为= " << demo_class->Add(a, b) << std::endl; std::cout << "Multiply结果为= " << demo_class->Multiply(a, b) << std::endl; std::cout << "Divide结果为= " << demo_class->Divide(a, b) << std::endl; std::cout << "Subtract结果为= " << demo_class->Subtract(a, b) << std::endl; std::cout << "power结果为= " << power(a,ratio) << std::endl; delete demo_class; return 0; }
1、属性页->C\C+±>附加包含目录,添加头文件目录
2、属性页->链接器->附加库目录中,添加lib文件目录
3、属性页->链接器->输入->附加依赖项,添加MathLibrary_dll.lib
首先新建工程MathLibrary,在源文件中添加MathLibrary.cpp,在头文件中添加MathLibrary.h,具体代码如下:
#pragma once namespace MathLibrary { class Arithmetic { public: Arithmetic(); ~Arithmetic(); // Returns a + b static double Add(double a, double b); // Returns a - b static double Subtract(double a, double b); // Returns a * b static double Multiply(double a, double b); // Returns a / b static double Divide(double a, double b); private: }; }
#include "MathLibrary.h" namespace MathLibrary { Arithmetic::~Arithmetic() {}; Arithmetic::Arithmetic() {}; double Arithmetic::Add(double a, double b) { return a + b; } double Arithmetic::Subtract(double a, double b) { return a - b; } double Arithmetic::Multiply(double a, double b) { return a * b; } double Arithmetic::Divide(double a, double b) { return a / b; } }
#include <iostream> #include "MathLibrary.h" int main() { double a = 7.4; int b = 99; std::cout << "a + b = " << MathLibrary::Arithmetic::Add(a, b) << std::endl; std::cout << "a - b = " << MathLibrary::Arithmetic::Subtract(a, b) << std::endl; std::cout << "a * b = " << MathLibrary::Arithmetic::Multiply(a, b) << std::endl; std::cout << "a / b = " << MathLibrary::Arithmetic::Divide(a, b) << std::endl; return 0; }
参考:
https://blog.csdn.net/htt789/article/details/81454832
https://www.cnblogs.com/skynet/p/3372855.html
https://learn.microsoft.com/zh-cn/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-170
——END——
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。