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?
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-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:
I use the following script, called
chkhdr
, to ensure that headers are self-contained and idempotent.For example:
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 namedlibnet_api.so
orlibnet_api.a
.To link with
net_api.a
, you will have to pass the pathname to the file to the link command: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.
The header
network_set.h
has extra dependencies that must be included first, one of which is the definition ofnetwork_String
. Check the library documentation or consult the author for more details.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.