我的makefile写法(一)
生活随笔
收集整理的這篇文章主要介紹了
我的makefile写法(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?程序演示了一個通過tcp/ip通訊的server/client程序,都是簡單的C程序。源碼:
server.c
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <sys/types.h> #include <errno.h> #include<string.h>int readline(int fd, void *pbuf, int maxlen);int main(int argc,char **argv) {int fd,client_sockfd;int len;int ret;struct sockaddr_in remoteaddr;struct sockaddr_in localaddr;char buf[1024];//// 建立套接口// fd = socket(AF_INET, SOCK_STREAM, 0);if(fd == -1){printf("socket() error %d\n",errno);return -1;}//// 綁定地址和端口//localaddr.sin_family = AF_INET;localaddr.sin_addr.s_addr = htonl(INADDR_ANY); localaddr.sin_port = htons(5000); len = sizeof(localaddr); if(bind(fd, (struct sockaddr *)&localaddr, len) == -1){printf("bind() error\n");return -1;}//// 建立套接口隊列//if(listen(fd, 5) == -1){printf("listen() error\n");return -1;}//// 等待 // len = sizeof(remoteaddr); printf("server recieve connect!--\n");int i = 0;while(1){printf("\nWaiting for ...\n"); // fflush(stdout);client_sockfd = accept(fd,(struct sockaddr *)&remoteaddr, &len);// // 接收數據//ret = recv(client_sockfd, (void *)buf, 1024, 0);if(ret <= 0){printf("server recieve data failed!--\n");} else{printf("server read line:%s\n", buf); }memset(buf,0x00,sizeof(buf));// strcpy(buf,"How are you?");sprintf(buf, "%d: How are you?", ++i);sleep(3);ret = send(client_sockfd, (void *)buf, strlen(buf),0);if(ret <= 0){printf("server send() error\n");}memset(buf,0x00,sizeof(buf));//關閉聯接 close(client_sockfd);printf("close client\n"); }return 0; }?
client.c
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <sys/types.h> #include <errno.h> #include <string.h> #include <stdlib.h>int main(int argc,char **argv) {int fd;int len,ret;struct sockaddr_in remoteaddr;char data[1024];int times = 1;//// 建立套接口// fd = socket(AF_INET, SOCK_STREAM, 0);//// 連接//remoteaddr.sin_family = AF_INET;remoteaddr.sin_addr.s_addr = inet_addr("127.0.0.1");remoteaddr.sin_port = htons(5000);len = sizeof(remoteaddr);ret = connect(fd, (struct sockaddr *)&remoteaddr, len);if(ret == -1) {printf("connect() error\n");return -1;}//while(1){//發送數據printf("The %d times run\n", times++);sprintf(data,"%s","hello world");ret = send(fd, (void *)data, strlen(data),0);if(ret <= 0){printf("send() error\n");}printf("sent line:%s\n", data);bzero(data,1024);sleep(1);ret = recv(fd, (void *)data, 1024, 0);if(ret <= 0){printf("server recieve data failed!--\n");} else{int id = atoi(data);printf("client[id=%d] read str:[%s]\n", id, data);}}printf("client exited.\n");//// 關閉// finish: close(fd);fd = -1;return 0; }?
再掛上我的Makefile:
install: myserver myclientmyserver: server.ogcc -o myserver server.o#server.o: server.cmyclient: client.ogcc -o myclient client.o#client.o: client.cclean:rm -f *.o my*?
掛上我的這個makefile,主要是為了說明:
1. 我這個文件相互之間沒有依賴性。
2. server.o依賴于server.c,所以這個依賴規則在makefile文件中可以不寫,它默認已經存在的。
總結
以上是生活随笔為你收集整理的我的makefile写法(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 学习和教训
- 下一篇: 机顶盒刷成普通android,移动网络电