文章目录[隐藏]
第二天主要讲了用gcc和Makefile实现多文件编译,不是很难,但很实用!通常我们都是在IDE环境下敲代码的,IDE让我们变懒了,因为我们只用编写代码就行了,至于编译和执行的过程IDE都帮我们做好了。而在linux中,更多的是用终端编写代码,自然的也要自己用命令去编译代码了。gcc就是编译C语言代码的一个编译命令,g++则是编译C++代码的,还有nasm是编译汇编语言的。
一、用gcc编译多文件
首先先用vim在同一个文件夹下写5个C语言文件,如下
源代码:
/*************************************************************************
> File Name: main.c
> Author: Sunriseydy
> Mail: sunriseydy@139.com
> Created Time: 2017年06月20日 星期二 10时11分02秒
************************************************************************/
#include<stdio.h>
#include"mytool1.h"
#include"mytool2.h"
int main(int argc, char **argv)
{
mytool1_print("hello 1");
mytool2_print("hello 2");
return 0;
}
/*-------------------------------------------------------------------------------------*/
/*************************************************************************
> File Name: mytool1.c
> Author: Sunriseydy
> Mail: sunriseydy@139.com
> Created Time: 2017年06月20日 星期二 10时02分41秒
************************************************************************/
#include<stdio.h>
#include"mytool1.h"
void mytool1_print(char* print_str)
{
printf("This is mytool1 print %s\n", print_str);
}
/*----------------------------------------------------------------------------------------*/
/*************************************************************************
> File Name: mytool1.h
> Author: Sunriseydy
> Mail: sunriseydy@139.com
> Created Time: 2017年06月20日 星期二 09时38分52秒
************************************************************************/
#ifndef _MYTOOL_1_H
#define _MYTOOL_1_H
void mytool1_print(char*);
#endif
/*
上面这几句代码意思是:如果_MYTOOL_1_H没有被声明过(if not define)
就声明这个变量,并且执行下面的语句,最后结束if。
这样写的好处是可以防止在多文件的编译中某一个声明被执行多次。
*/
/*-------------------------------------------------------------------------------*/
/*************************************************************************
> File Name: mytool2.c
> Author: Sunriseydy
> Mail: sunriseydy@139.com
> Created Time: 2017年06月20日 星期二 09时53分52秒
************************************************************************/
#include<stdio.h>
#include"mytool2.h"
void mytool2_print(char* print_str)
{
printf("This is mytool2 print %s\n", print_str);
}
/*-------------------------------------------------------------------------------*/
/*************************************************************************
> File Name: mytool2.h
> Author: Sunriseydy
> Mail: sunriseydy@139.com
> Created Time: 2017年06月20日 星期二 10时05分40秒
************************************************************************/
#ifndef _MYTOOL_2_H
#define _MYTOOL_2_H
void mytool2_print(char* );
#endif
/*-------------------------------------------------------------------------------------*/
注意:如果用了老师第一天发的那个vimrc文件来自定义vim的话,新建一个.h文件会自动加上cpp的语句
你可以把第8、9两行手动删去,也可以更改Home目录下的.vimrc文件,具体更改的方法如图:


修改后的效果如图:
这5个文件写完后就该编译了,用gcc编译的语句为:
gcc -c mytool1.c //生成mytool1.o文件
gcc -c mytool2.c //生成mytool2.o文件
gcc -c main.c //生成main.o文件
gcc -o main main.o mytool1.o mytool2.o //将mytool1.o,mytool2.o,main.o链接为一个名为main的可执行文件
编译后执行生成的可执行文件
./main
如图:
版权说明:
本作品由 sunriseydy 采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
文章内容如未说明均为原创,欢迎转载,但请注明原作者(sunriseydy)和原文链接(https://blog.sunriseydy.top/technology/code/second-day/)
部分来自互联网的文章,如有侵权,请联系我,24小时内删除,谢谢
本作品由 sunriseydy 采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
文章内容如未说明均为原创,欢迎转载,但请注明原作者(sunriseydy)和原文链接(https://blog.sunriseydy.top/technology/code/second-day/)
部分来自互联网的文章,如有侵权,请联系我,24小时内删除,谢谢
手机打开扫一扫即可访问本页面
感谢您的支持,SunriseYDY 会继续努力的!

扫码打赏,你说多少就多少


打开支付宝扫一扫,即可进行扫码打赏哦
日出一点一 | 在探索的路上永不止步