我们此处为了说明静态库和动态库的编译,会使用示例代码。创建 test.h, hello.cpp 和 world.cpp, 在两个源文件中分别实现两个函数( 此处都是示例代码,文件命名不规范,理解意思即可 )。
test.h (静态库头文件)
#ifndef _TEST_
#define _TEST_
void hello();
void world();
#endif
hello.cpp:
#include <iostream>
#include "test.h"
using namespace std;
void hello()
{
cout << "this is hello" << endl;
}
world.cpp:
#include <iostream>
#include "test.h"
using namespace std;
void world()
{
cout << "this is world" << endl;
}
静态库编译:
linux 下静态库的命名规则为 lib+库名+.a1、首先将源文件夹编译成目标文件 ( hello.o, world.o )
g++ -c hello.cpp world.cpp
注意 -c 参数是必须带,生成目标代码