C++进阶5:动态库与静态库--类篇
0.前提(文件内容)
(1)test.h
#pragma once
class Test{
public:
Test();
~Test();
void Func(int i);
};
(2)test.cpp
#include <iostream>
#include "test.h"
using namespace std;
Test::Test(){
cout << __func__ << "()" << endl;
}
Test::~Test(){
cout << __func__ << "()" << endl;
}
void Test::Func(int i){
cout << __func__ << "(" << i << ")" << endl;
}
(3)main.cpp
#include "test.h"
int main(){
Test t;
t.Func(100);
}
1、静态库的制作与使用
1.1 创建
(1)编译源文件
g++ -c -o test.o test.cpp
(2)生成静态库
ar -rcs libtest.a test.o
1.2 使用
(1)链接静态库
g++ -o main main.cpp -L. -ltest
或者
g++