标题
标题
当我们有多个源程序时,用gcc每个都编译,这样我们没有修改过的源文件也得重新编译一次,很麻烦,这时候写makefile就派上了用场,可以大大的提高我们的编码和调试速度。( 注意:头文件并不参加链接和编译。编译器第一步要做的就是简单的把头文件在包含它的源文件中展开。不知你是否能理解这句话。也就是头文件里面有什么内容,通通把它移到包含这个头文件的源文件里 )为此:
1. 先用编辑器vim编辑三个函数及两个头文件
main.c
#include "sum.h"
#include "max.h"
#include<stdio.h>
int main(int argc,char **agrv)
{ int a,b; sum_print(); printf("Please input two number:/n"); scanf("%d,%d",&a,&b); max_print(a,b);
}
sum.h
#ifndef SUM_H
#define SUM_H
#include<stdio.h>
void sum_print();
#endif
sum.c
#include "sum.h"
#include<stdio.h>
void sum_print()
{int i, sum = 0;for (i = 0; i<10; i++){sum += i;}printf("Sum=%d/n", sum);
}
max.h
#ifndef MAX_H
#define MAX_H
#include<stdio.h>
void max_print(int a,int b);
#endif
max.c
#include "max.h"
#include<stdio.h>
void max_print(int a,int b)
{ if(a>b) { printf("The Max Number is:%d/n",a); } else { printf("The Max Number is:%d/n",b); }
}
2. 生成Makefile文件,利用编辑器vim编辑Makefile文件
sunbin@sunbin-virtual-machine:~$ vim makefile
makefile文件内容如下:
main: main.o sum.o max.o gcc -o main main.o sum.o max.o //命令必须以TAB键开头
main.o: main.c sum.h max.h gcc -c main.c
sum.o: sum.c sum.h gcc -c sum.c
max.o: max.c max.h gcc -c max.c
所有文件若下:
3. 编译与运行:
sunbin@sunbin-virtual-machine:~$ make
gcc -c main.c
gcc -c sum.c
gcc -c max.c
gcc -o main main.o sum.o max.o
sunbin@sunbin-virtual-machine:~$ ./main
Sum=45/nPlease input two number:/n
形成文件如下:
黑体`#include “line1.h”
#include “line2.h”
int main(int argc,char **argv)
{
line1_print(“hello runfarther”);
line2_print(“hello runfarther”);
return 0;
}
#include "line1.h"
#include "line2.h"int main(int argc,char **argv)
{line1_print("hello runfarther");line2_print("hello runfarther");return 0;
}
//server.c
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/epoll.h>#define SERV_PROT 9000
#define MAXLINE 10 int main()
{struct sockaddr_in servaddr, cliaddr;sockelen_t cliaddr_len;char buf[MAXLINE];char str[INET_ADDRSTRLEN];int efd;listen = socket(AF_INET, SOCK_STREAM, 0);bero(&servaddr, sizef(servaddr));servaddr.sin_family = AF_INET;servaddr.sin_addr.s_addr = htonl(SERV_PROT);serv.sin_port = htons(SERV_PROT);bind(listenfd, (struct sockaddr*))&servaddr, sizeof(servaddr));listen(listenfd, 20);struct epoll_event event;struct epoll_event resevent[20];int res, leb;efd = epoll_creat(10);event.events = EPOLLIN | EPOLLET; //ET边沿触发//even.events = EPOLLIN; // LT水平触发(默认)printf("Accept connections .....\n");cliaddr_len = sizeof(cliaddr);connfd = accept(listenfd, (strcut sockaddr *)&cliaddr, &cliaddr_len);printf("receive from %s at PROT %d\n", inet_ntop(AF_INET, &cliaddr.sin_addr.s_addr, str, sizeof(str)), ntohs(clie_addr.sin_port));event.data.fd = connfd;epoll_ctl(efd, EPOLL_CTL_ADD, &event);while(1) {res = epoll_wait(efd, resevent, 10, -1);printf("res %d\n", res);if (resevent[0].data.fd == connfd){len = read(connfd, buf, MAXLINE/2);write(STDOUT_FILENO, buf, len);}}}return 0;
}
#include<signal.h>
#include<stdio.h>
#include<unistd.h>void sig_usr(int signo)
{if(signo == SIGUSR1)printf("receive SIGUSR1\n");else if(signo == SIGUSR2)printf("receive SIGUSR2\n");elseperror("signal error");
}int main()
{if(signal(SIGUSR1, sig_usr) == SIG_ERR)perror("can't catch SIGUSR1");if(signal(SIGUSR2, sig_usr) == SIG_ERR)perror("can't catch SIGUSR2");for( ; ; )sleep(1);return 0;
}