unp.hプログラミング環境の構成
Unp H Programming Environment Configuration
ソースコードをダウンロードする
www.unpbook.com
解凍する
tar -zxvf unpv13e.tar.gz
解凍後、unpv13eディレクトリにREADMEファイルがあることがわかります。
内部のプロンプトコンテンツは次のとおりです
QUICK AND DIRTY =============== Execute the following from the src/ directory: #Configuration script, used to read machine information to change various Makefile files ./configure # try to figure out all implementation differences #Compile cd lib # build the basic library that all programs need make # use 'gmake' everywhere on BSD/OS systems # cd ../libfree # continue building the basic library make #These two steps can be omitted cd ../libroute # only if your system supports 4.4BSD style routing sockets make # only if your system supports 4.4BSD style routing sockets cd ../libxti # only if your system supports XTI make # only if your system supports XTI #Enter the intro directory, compile the daytimetcpcli program, and run cd ../intro # build and test a basic client program make daytimetcpcli ./daytimetcpcli 127.0.0.1 If all that works, you're all set to start compiling individual programs.
次に、READMEをステップバイステップで実行します
1.構成スクリプト
#Under the unpv13e directory ./configure
構成後、ディレクトリにさらに2つのファイルMake.definesとMakefileがあります。これらの2つのファイルは、configureのアクションの下で、接尾辞.inが付いた同じ名前のファイルによって生成されます。
2.lib make
cd lib vim Makefile ESC :q make
このMakefileの内容は次のとおりです。
include ../Make.defines#Include the definition file generated by configure in the previous step all: ${LIB_OBJS} #This is a series of .o files ar rv ${LIBUNP_NAME} $? #LIBUNP_NAME = ../libunp.a ${RANLIB} ${LIBUNP_NAME} #RANLIB = ranlib means to update the symbol index table of the static library clean: rm -f ${PROGS} ${CLEANFILES}
3.libfree make
cd libfree make
Makefileの内容は次のとおりです。
include ../Make.defines all: ${LIBFREE_OBJS}#LIBFREE_OBJS = in_cksum.o inet_ntop.o inet_pton.o ar rv ${LIBUNP_NAME} $? ${RANLIB} ${LIBUNP_NAME} test_inet_pton: test_inet_pton.o ${CC} ${CFLAGS} -o root@xxxxx test_inet_pton.o ${LIBS} clean: rm -f ${PROGS} ${CLEANFILES}
unpv13e / libとunpv13e / libfreeのMakefileの違いは、静的ライブラリの構築に使用されるオブジェクトファイルにのみあることがわかります。
4.ルーチンをコンパイルして実行します
linux> cd unpv13e/intro linux> make daytimetcpcli linux> ./daytimetcpcli 127.0.0.1 connect error: Connection refused#Although the connection is wrong, but the program has been compiled successfully
5.unp.hをどこでも利用できるように構成します
/ usrはユーザーを意味すると思っていましたが、後でUnixシステムリソースであることがわかりました。それらがすべてlib、bin、およびincludeに関するものであるのも不思議ではありません。
linux> ls /usr bin include lib lib32 lib64 libexec libx32 local sbin share src
binの下に実行可能なバイナリファイルがあります
ほとんどのインクルードC言語ヘッダーファイル
主にlibの下のいくつかの共有ライブラリファイル
したがって、unp.hを/ usr / includeに移動する必要があります。また、unp.hにはconfig.hが含まれているため、config.hも/ usr / includeに移動し、unp.hを変更して#Include 'に変更する必要があります。 …/ config.h 'は#includeに変更されます
同時に、手順2と3で生成されたlibunp.a静的ライブラリファイルを/ usr / libに移動する必要があります。
linux> sudo cp unpv13e/lib/unp.h /usr/include linux> sudo cp unpv13e/config.h /usr/include linux> sudo cp unpv13e/libunp.a /usr/lib linux> sudo vim /usr/include/unp.h #Modify#include '../config.h' to #include
次に、unp.hで定義された関数をどこでも呼び出すプログラムを作成できます。
次のプログラムは、unp.hで定義されたforkラッパー関数Forkを呼び出します。
//unp1.c #include #include #include int main(int argc,char* argv[]) { pid_t p = Fork() if(p==0) { printf('Fork success!
') exit(0) } printf('parent process!') return 0 }
Makefile
unp1: unp1.o gcc -o unp1 unp1.o -lunp clean: rm unp1
linux> make unp1 linux> ./unp1 parent process!Fork success!
成功しました!