c++动静编译的区别

动态编译和静态编译的区别

动态编译决定了在程序运行时才会连接库文件(需要部署的坏境安装对应库,程序体积小)
静态编译在编译时就连接好库文件了(所有库文件都打包进程序了,所以体积大,不过移植性好)

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

请关注我的订阅号

订阅号.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码哥说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值