Linux研发入门篇(一)

一:前言

对于想要在Linux下C/C++下开发稍微大型的程序的程序员们都知道,在Linux下处理头文件是一件很头大的事情。

比如使用printf函数这种C函数库则需要引用#include <stdio.h>,使用C++中的STL的map容器则要#include <map>,使用socket网络编程则要#include <sys/socket.h>,

我们思考下uint32_t是在哪个头文件定义的呢?sleep函数又是在哪个头文件?open和close是在同一个头文件里吗?

这些问题对很多刚刚入门Linux下C/C++的程序员造成困扰,下面总结一下。

按照类别可以把头文件分为以下三类:

  • C标准头文件:一共29个头文件。 http://en.cppreference.com/w/c/header点击打开链接
  • C++标准头文件:除C标准库外,还包括STL标准库等10+个头文件 http://www.cplusplus.com/reference/stl/ 。把C标准头文件放到std的命名空间里,文件名统一加上c前缀,如#include <string.h> 改成#include <cstring>外,
  • linux系统头文件:操作系统相关,如socket网络、共享内存、信号量等,常用的就10+左右。http://pubs.opengroup.org/onlinepubs/7908799/headix.html

二:C标准库

  • #include <string.h> 字符串操作相关
memcpy /strcpy 区别?
memset
strncpy
  • #include <stdio.h> 标准输入输出
fopen/fwrite
printf/scanf
  • #include <stdio.h> 标准输入输出
fopen/fwrite
printf/scanf
  • #include <stdlib.h> 常用的一些函数库
strtol/atoi
malloc/free
rand
qsort
abs/div
size_t

  • #include <math.h> 函数库
sin/cos
pow/sqrt
ceil/floor
  • #include <stdint.h> 
uint32_t
SIZE_MAX
  • #include <ctype.h>
islower
toupper
  • #include <time.h> 
time
mktime
  • #include <setjmp.h>...

三、C++标准库

  • 容器类:<map>、<vector>、<set>、<unordered_map>
  • #include <memory>
    share_ptr
  • #include <algorithm>
    min/max
    merge
    lower_bound/upper_bound
    sort
    unique
    remove
    copy
    find
  • #include <setjmp.h>...

四、Linux系统函数库

  • #include <unistd.h>
    chown()
    close()/write()/read()
    fsync()
    sleep()/usleep()
    getpid()
  • #include <fcntl.h>
    open()
    create()
    fcntl()
  • #include <pthread.h>
  • #include <fcntl.h>
    open()
    create()
    fcntl()
  • #include <pthread.h>
  • sys目录下
<sys/shm.h>
<sys/msg.h>
<sys/socket.h>
<sys/sem.h>
<sys/stat.h>
<sys/time.h>
<sys/select.h> 
<sys/epoll.h>
<sys/types.h>

五、结语

学习在Linux下编写C/C++大型程序的时候需要多思考多总结,养成良好的编码习惯,比如头文件的引用顺序,最好按照0、.CPP对应的头文件。1、C标准库。2、C++标准库。3、其他库。4、自己项目中的.h文件。如果是系统提供的标准库使用#include<***>,其他库使用#include"***"。每一节内的#include顺序按照字母序。这样会极大地提高头文件相关的错误定位时间,也能有效地提高代码的可读性。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值