Dev-C++中使用静态链接库

Dev-C++中使用静态链接库

在Dev-C++中,静态链接库的后缀是.a,这点和VS的lib不一样。

一、首先,我们建立静态链接库项目,
新建一个CPP文件square.cpp

code:

class Square{
public:
float Area(float width,float height);
};

float Square::Area(float width,float height){
return width * height;
}

这个类定义了计算长方形面积的方法。
编译无误即生成了和项目名相同的SquArea.a库文件。注意,这里不是代码文件名而是项目名。

二、使用静态库
新建一个控制台项目,新建一个头文件square.h
这里把上面定义的类粘贴过来:

code:

class Square{
public:
float Area(float width,float height);
};

在入口main.cpp里引用该头文件,

code:

#include <iostream>
#include "area.h"
using namespace std;

int main(int argc, char** argv) {
float width=0;
float height=0;
cout << "请输入长方形的宽:" << endl;
cin >> width;
cout << "请输入长方形的高:" << endl;
cin >> height;
Square square;
cout << "面积=" << square.Area(width,height) << endl;

system("pause");
return 0;

}

三、设置链接参数
进入项目属性参数设置,在链接栏增加下面
./Squarea.a

这里的意思是链接的时候把当前目录下Squarea静态链接库加入进来。

最后编译运行,完成。

猜你喜欢

转载自blog.51cto.com/1685766/2479173
今日推荐