C: Creating a static library and linking using Makefile

I am trying to understand static and shared libraries .

I want to do the following to create a make file that separates compilation and linking so that a static library is created and linked in the formation of the final static executable.

I have the following code for a Makefile, but I get the following error

Makefile:13: *** missing separator. Stop.

But I'm also trying to figure out how to actually link / create libraries.

If I run the commands after line 12 in the terminal, they work, but not in the makefile.

 myProgram: main.o addSorted.o freeLinks.o gcc -lm -o myProgram main.o addSorted.o freeLinks.o main.o: main.c gcc -O -c -lm main.c main.h addSorted.o: addSorted.c addSorted.h gcc -O -c -lm addSorted.c freeLinks.o: freeLinks.c freeLinks.h gcc -O -c -lm freeLinks.c ar rc libmylib.a main.o addSorted.o freeLinks.o //Error Line ranlib libmylib.a gcc -o foo -L. -lmylib foo.o clean: rm -f myProgram main.o addSorted.o freeLinks.o 

Also, if you can help improve the code, I would really appreciate it.

+6
source share
2 answers

Try the following:

 all: myProgram myProgram: main.o libmylib.a #libmylib.a is the dependency for the executable gcc -lm -o myProgram main.o -L. -lmylib main.o: main.c gcc -O -c main.c main.h addSorted.o: addSorted.c addSorted.h gcc -O -c addSorted.c freeLinks.o: freeLinks.c freeLinks.h gcc -O -c freeLinks.c libmylib.a: addSorted.o freeLinks.o #let link library files into a static library ar rcs libmylib.a addSorted.o freeLinks.o libs: libmylib.a clean: rm -f myProgram *.o *.a *.gch #This way is cleaner than your clean 

This rule set compiles all files first, then creates a library (libmylib.a) and uses an artifact to link the executable. I also added a separate redundant target form creating only libs. Required Files:

 user@host > ls addSorted.c addSorted.h freeLinks.c freeLinks.h main.c main.h Makefile 
+9
source

The make file is not a shell script. This is the configuration file for the expert system. In particular, an expert system that knows if you tell how to efficiently create files and their dependencies with a minimum of re-creating files that are not needed to be redone.

If you look at the first rule you have:

 myProgram: main.o addSorted.o freeLinks.o gcc -lm -o myProgram main.o addSorted.o freeLinks.o 

which tells the system how to make a file called myProgram if it decides what to do. Parts after the colon are the files that I need. If they are not there or decide that they are out of date, make will try to find some recipe that can be used to create or update. once everything is done, then it will execute the line "gcc ..." and assume that it will create or update myProgram.

The ar and ranlib lines that you do not match the required syntax for the makefile rule. In appearance, they seem to be a recipe for creating libmylib.a. If you put them in the syntax, make should say what you get:

 libmylib.a: main.o addSorted.o freeLinks.o ar rcu libmylib.a main.o addSorted.o freeLinks.o ranlib libmylib.a 

myProgram should depend on the library itself, and not on the contents of the library, and it is best to place the library parameters at the end:

 myProgram: libmylib.a gcc -o myProgram libmylib.a -lm 

if you want, you can use the gcc option to search for libraries in the current directory:

 gcc -L. -o myProgram main.o -lmylib -lm 

There are also makefile variables that can help you not to repeat so I would write the first rule as:

 myProgram: libmylib.a gcc -L. -o $@ -lmylib -lm 

however, it is unlikely that main.o should really be part of the library, like so:

 myProgram: main.o libmylib.a gcc -L. -o $@ $< -lmylib -lm 

and library rule:

 libmylib.a: addSorted.o freeLinks.o ar rcu $@ $+ ranlib $@ 

$ + here means "all dependency file names."

Finally, if you want gcc to make the actual static executable, and not just use the library you created, you need to pass the -static option to gcc.

+5
source

All Articles