unix卷一去年暑假买的到现在才开始看无比惭愧,而且惭愧第一个程序就断断续续弄了几天,要好好写程序了,马上要找工作了,下面介绍下把本书第一个程序跑起来的过程:

搜各种博客

我用系统的是ubuntu 13.04

感谢风无语大神的blog:传送门 以后也要坚持记录自己遇到的各种问题以及解决他们的方法,当我们新接触一个领域时,往往在入门问题上耽误了好多时间,我按照风无语大神的方法一切准备好后,还是有问题

kapop@kapop:~/cpp$ g++ a.cpp -o a -lunp
/tmp/cczLHmnw.o:在函数‘main’中:
a.cpp:(.text+0x30):对‘err_quit(char const*, ...)’未定义的引用
a.cpp:(.text+0x67):对‘err_sys(char const*, ...)’未定义的引用
a.cpp:(.text+0xd9):对‘err_quit(char const*, ...)’未定义的引用
a.cpp:(.text+0x108):对‘err_sys(char const*, ...)’未定义的引用
a.cpp:(.text+0x142):对‘err_sys(char const*, ...)’未定义的引用
a.cpp:(.text+0x181):对‘err_sys(char const*, ...)’未定义的引用
collect2: 错误: ld 返回 1
kapop@kapop:~/cpp$ g++ a.cpp unp.c -o a -lunp
kapop@kapop:~/cpp$ ./a 127.0.0.1
03 AUG 2013 15:53:32 CST



unp.c文件内容如下

 

 

#include  <errno.h>   /* for definition of errno */
#include <stdarg.h> /* ANSI C header file */
//#include "ourhdr.h"
#include "unp.h"

static void err_doit(int, const char *, va_list);

char *pname = NULL; /* caller can set this from argv[0] */

/* Nonfatal error related to a system call.
* Print a message and return. */

void
/* $f err_ret $ */
err_ret(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
err_doit(1, fmt, ap);
va_end(ap);
return;
}

/* Fatal error related to a system call.
* Print a message and terminate. */

void
/* $f err_sys $ */
err_sys(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
err_doit(1, fmt, ap);
va_end(ap);
exit(1);
}

/* Fatal error related to a system call.
* Print a message, dump core, and terminate. */

void
/* $f err_dump $ */
err_dump(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
err_doit(1, fmt, ap);
va_end(ap);
abort(); /* dump core and terminate */
exit(1); /* shouldn't get here */
}

/* Nonfatal error unrelated to a system call.
* Print a message and return. */

void
/* $f err_msg $ */
err_msg(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
err_doit(0, fmt, ap);
va_end(ap);
return;
}

/* Fatal error unrelated to a system call.
* Print a message and terminate. */

void
/* $f err_quit $ */
err_quit(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
err_doit(0, fmt, ap);
va_end(ap);
exit(1);
}

/* Print a message and return to caller.
* Caller specifies "errnoflag". */

static void
err_doit(int errnoflag, const char *fmt, va_list ap)
{
int errno_save;
char buf[MAXLINE];

errno_save = errno; /* value caller might want printed */
vsprintf(buf, fmt, ap);
if (errnoflag)
sprintf(buf+strlen(buf), ": %s", strerror(errno_save));
strcat(buf, "\n");
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(stderr); /* SunOS 4.1.* doesn't grok NULL argument */
return;
}


a.cpp如下所示

 

 

#include "unp.h"
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
using namespace std;
int main(int argc, char **argv){
int sockfd, n;
char recvline[MAXLINE + 1];
struct sockaddr_in servaddr;
if (argc != 2) {
// cout << "ni mei can shu shao le!" << endl;
err_quit("usage: a.out <IPaddress>");
}
if ( (sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0) {
// cout << "Fuck ing life ! " << endl;
err_sys("socket error");
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(13);
if (inet_pton(AF_INET,argv[1],&servaddr.sin_addr) <= 0) {
//cout << "inet_pton error for" <<endl;
err_quit("inet_pton error for %s",argv[1]);
}
if (connect(sockfd,(SA *) &servaddr,sizeof(servaddr)) <0) {
/*cout << "connect error"<< endl ;*/
err_sys("connect error");
}
while( (n = read(sockfd, recvline, MAXLINE)) >0 ){
recvline[n]=0;
if (fputs(recvline,stdout) == EOF) {
//cout << "Fputs error" << endl ;
err_sys("fputss error");
}
}
if (n < 0 ) {
//cout << "read error" <<endl;
err_sys("read error");
}
exit(0);
//return 0;
}


感谢互联网的共享精神,感谢记录问题并提供解决方法的大神,让我等*nix菜鸟取得进步。

 

 



unix 网路编程(卷一)第一个程序编译过程_未定义