Compiling
The first problem you have to deal with is why the code is not compiling. There is a problem in your network_set.h
header; it is not self-contained in some way, so you have to include something else before including it, or you have to explicitly configure it in some way. You should aim to have your headers both self-contained and idempotent.
- self-contained can be included without any other headers preceding it
- idempotent can be included multiple times without causing chaos
Self-containment is achieved by ensuring it can be the first header included in a source file and then compiles cleanly. It means that if it uses a feature (for example, size_t
) then it includes a header that defines the feature (for example, <stddef.h>
).
Idempotence is achieved by including a header guard:
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
...main body of header...
#endif /* HEADER_H_INCLUDED */
I use the following script, called chkhdr
, to ensure that headers are self-contained and idempotent.
#!/bin/ksh
#
# @(#)$Id: chkhdr.sh,v 1.2 2010/04/24 16:52:59 jleffler Exp $
#
# Check whether a header can be compiled standalone
tmp=chkhdr-$$
trap 'rm -f $tmp.?; exit 1' 0 1 2 3 13 15
cat >$tmp.c <<EOF
#include HEADER /* Check self-containment */
#include HEADER /* Check idempotency */
int main(void){return 0;}
EOF
options=
for file in "$@"
do
case "$file" in
(-*) options="$options $file";;
(*) echo "$file:"
gcc $options -DHEADER="\"$file\"" -c $tmp.c
;;
esac
done
rm -f $tmp.?
trap 0
For example:
chkhdr -Itools/net/inc tools/net/inc/network_set.h
Linking
In due course, after you've fixed the compilation problems, you will run into linking problems. The option -lnet_api
looks for a library named libnet_api.so
or libnet_api.a
.
To link with net_api.a
, you will have to pass the pathname to the file to the link command:
LIB_DIR = ./tools/net/lib
LIB_NET_API = net_api.a
LIB_PATH = -L ${LIB_DIR}
${CC} ... ${LIB_DIR}/${LIB_NET_API} ...
Obviously, you could define a macro for the path to the whole library. Note how I redefined LIB_PATH in terms of the macro LIB_DIR.