Linking to a Static Library

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 a header file in my source code in main.c file

 #include <network_set.h> 

I have the following static library and a title in the following directory

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

In my Makefile, I tried linking 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 compiling, I get the following errors:

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

What's going on here?

+1
c makefile
source share
3 answers

Compilation

The first problem you have to deal with is why the code does not compile. There is a problem in your network_set.h header; this is not self-sufficient in some way, so you need to enable something else before you enable it, or you need to explicitly configure it somehow. You should strive to ensure that your headings are autonomous and idempotent.

  • standalone may be included without any other headers preceding it
  • idempotent can be enabled multiple times without causing chaos

Self-restraint is achieved by ensuring that it can be the first header included in the source file and then compiled cleanly. This means that if it uses a function (e.g. size_t ), then it includes a header that defines the function (e.g. <stddef.h> ).

Idempotency is achieved by enabling header protection:

 #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 the headers are autonomous 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 

Communication

Over time, after fixing compilation problems, you will encounter communication problems. The -lnet_api option -lnet_api for a library named libnet_api.so or libnet_api.a .

To establish a connection with net_api.a , you need to transfer the file path to the communication 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 entire library. Notice how I redefined LIB_PATH in terms of the LIB_DIR macro.

+2
source share

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

+2
source share

You do not show your LDFLAGS; I suppose they are defined, but you just did not publish them. They should include "-static" if you are creating a static library.

If you don’t know what it is, look at the output of the compiler at the beginning, where it starts with "gcc", and see if "-static" appears there.

+1
source share

All Articles