Linking against a static library

2019-01-15 21:55发布

gcc Version: 4:4.4.4-1ubuntu2 GNU Make 3.81

I have the following library called net_api.a and some header files i.e.

network_set.h

I have include the header file in my source code in my main.c file

#include <network_set.h>

I have the following static library and header in the following directory

./tools/net/lib/net_api.a
./tools/net/inc/network_set.h

In my Makefile I have tried to link using the following, code snippet:

INC_PATH = -I tools/net/inc
LIB_PATH = -L tools/net/lib

LIBS = -lnet_api

$(TARGET): $(OBJECT_FILES)
    $(CC) $(LDFLAGS) $(CFLAGS) $(INC_PATH) $(LIB_PATH) $(LIBS) $(OBJECT_FILES) -o $(TARGET)

main.o: main.c
    $(CC) $(CFLAGS) $(INC_PATH) $(LIB_PATH) -c main.c

However, when I compile I get the following errors:

network_set.h error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘network_String’

What is going wrong here?

标签: c makefile
3条回答
我只想做你的唯一
2楼-- · 2019-01-15 22:26

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.

查看更多
女痞
3楼-- · 2019-01-15 22:31

The header network_set.h has extra dependencies that must be included first, one of which is the definition of network_String. Check the library documentation or consult the author for more details.

查看更多
虎瘦雄心在
4楼-- · 2019-01-15 22:41

You don't show your LDFLAGS; I assume they are defined but you just didn't post them. They must include "-static" if you're building against a static library.

If you don't know what they are, look at the compiler output at the start where it begins with "gcc" and see if "-static" shows up there.

查看更多
登录 后发表回答