Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile C code with the "Unix networking programming book" library?

Introduction

I've started following the book Unix Network programming 2003. I've downloaded the source code from the official website here: source code. This includes a README file, which tells you how to build/compile all the files included for the book.

I followed these instructions on this GitHub page (which also has the source code) github And managed to use the make files and configure the source successfully from what it seems.

Makefile to compile the examples provided

The first chapter of the program examples are in directory highlighted below. My program is '[BOOK]daytimetcpcli.c' and placed it inside the Makefile provided. Makefile Inside the target directory:

unpv13e/intro$

(includes all the other book example programs)

include ../Make.defines

PROGS = [BOOK]daytimetcpcli.c daytimetcpcli1 daytimetcpcli2 daytimetcpcli3 \
    daytimetcpsrv daytimetcpsrv1 daytimetcpsrv2 daytimetcpsrv3 \
    daytimetcpcliv6 daytimetcpsrvv6 \
    byteorder

all:    ${PROGS}




[BOOK]daytimetcpcli:    [BOOK]daytimetcpcli.o
    ${CC} ${CFLAGS} -o $@ [BOOK]daytimetcpcli.o ${LIBS}

daytimetcpcli1: daytimetcpcli1.o
    ${CC} ${CFLAGS} -o $@ daytimetcpcli1.o ${LIBS}

daytimetcpcli2: daytimetcpcli2.o
    ${CC} ${CFLAGS} -o $@ daytimetcpcli2.o ${LIBS}

daytimetcpcli3: daytimetcpcli3.o
    ${CC} ${CFLAGS} -o $@ daytimetcpcli3.o ${LIBS}

daytimetcpsrv:  daytimetcpsrv.o
    ${CC} ${CFLAGS} -o $@ daytimetcpsrv.o ${LIBS}

daytimetcpsrv1: daytimetcpsrv1.o
    ${CC} ${CFLAGS} -o $@ daytimetcpsrv1.o ${LIBS}

daytimetcpsrv2: daytimetcpsrv2.o
    ${CC} ${CFLAGS} -o $@ daytimetcpsrv2.o ${LIBS}

daytimetcpsrv3: daytimetcpsrv3.o
    ${CC} ${CFLAGS} -o $@ daytimetcpsrv3.o ${LIBS}

daytimetcpcliv6:    daytimetcpcliv6.o
    ${CC} ${CFLAGS} -o $@ daytimetcpcliv6.o ${LIBS}

daytimetcpsrvv6:    daytimetcpsrvv6.o
    ${CC} ${CFLAGS} -o $@ daytimetcpsrvv6.o ${LIBS}

byteorder:  byteorder.o
    ${CC} ${CFLAGS} -o $@ byteorder.o ${LIBS}

clean:
    rm -f ${PROGS} ${CLEANFILES}

The Problem

I placed my file inside the Makefile. However when i run 'make' again. This occurs:

make: Nothing to be done for 'all'.

I also tried just normal compiling with GCC:

gcc [BOOK]daytimetcpcli.c

fatal error: unp.h: No such file or directory

So my question is how do I compile my program with the Makefile provided? is what I've done correct? And I know I can compile with GCC, however I do not know the syntax to include the header files needed with GCC. Which is why I've attempted to do it with the Makefile.

Which follows into my second question, how do I compile without the makefile? (using gcc with the correct -I headers)? gcc program.c -I unp.h ?

*Note '[BOOK]daytimetcpcli.c' = daytimetcpcli.c in the source code provided on official website, in the source.

like image 971
digitalXmage Avatar asked Oct 17 '22 05:10

digitalXmage


1 Answers

First your mistake in the Makefile:

make: Nothing to be done for 'all'.

This means that all make targets (the content of the PROGS variable) exist and are more recent than their dependencies (i.e. what must be used to build them, IOW the source).

It is unexpected for you because you didn't add the resulting binary to the PROGS variable, but the source file. Since it exists, Make considers it has nothing more to do. To correct this, change:

PROGS = [BOOK]daytimetcpcli.c ...

to

PROGS = [BOOK]daytimetcpcli ...

BTW, it's not a great idea to use special shell characters like [ and ] in your file name, don't forget to escape or quote them when using the file in shell commands.


Now your mistake in the compilation command:

fatal error: unp.h: No such file or directory

It means that you have not told gcc where to find the header files. Why would it work with make? Because the command in the Makefile includes additional options: ${CFLAGS} and ${LIBS}, two variables that are defined in Make.defines (see the include line).

You correct compilation command should be:

gcc -o "[BOOK]daytimetcpcli" -I../lib -g -O2 -D_REENTRANT -Wall ../libunp.a -lpthread "[BOOK]daytimetcpcli.c"

(actually, the lines in the Makefile above only call the linker and CFLAGS is not necessary there; it is only used when creating the .o files whose compilation is implicit)

like image 115
xhienne Avatar answered Nov 01 '22 09:11

xhienne