动态编译和静态编译的区别
动态编译决定了在程序运行时才会连接库文件(需要部署的坏境安装对应库,程序体积小)
静态编译在编译时就连接好库文件了(所有库文件都打包进程序了,所以体积大,不过移植性好)
demo
静态编译
test.h
#ifndef _TEST_H_
#define _TEST_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int add(int num1, int num2);
#endif //_RYAN_CONFIG_H_
test.cpp
#include "test.h"
int add(int num1, int num2)
{
return num1+num2;
}
将之编译成静态库
g++ -c test.cpp
ar crv libtest.a test.o
使用静态库demo
server.cpp
#include <stdio.h>
#include <string>
#include <iostream>
#include "test.h"
int main()
{
int a = 10;
int b = 10;
int c = add(10,10);
printf("%d\n",c);
return 0;
}
编译
g++ server.cpp -o server -ltest