C: Creating static library and linking using a Makefile

白昼怎懂夜的黑 提交于 2019-12-03 11:37:59

问题


I am trying to understand static and shared Libraries.

I want to do the following to create a makefile that does separate compilation and linking such that a static library is created and linked in forming the final static executable.

I have the following code for the Makefile, but I am getting the following error

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

But I am also trying to understand 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 assist in improving the code, I would really appreciate it.


回答1:


Try this:

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's 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 set of rules first compiles all files, then it makes library (libmylib.a) target and uses it's artifact to link the executable. I also added separate redundant target form making libs only. Needed files:

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



回答2:


A makefile is not a shell script. It's a configuration file for an expert system. Specifically an expert system that knows, if you tell it, how to efficiently create files and their dependencies with a minimum of re-making files that don't need to be remade.

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

that tells the system how to make a file called "myProgram" if it decides that it needs to do that. The parts after the colon are files that myProgram needs. If they aren't there, or make decides they are out of date, make will try to find some recipe that can be used to create or update them. Once all that is done, make then executes the "gcc ..." line and assumes that will create or update myProgram.

The ar and ranlib lines you have don't match the needed syntax for a makefile rule. From the look of them, they appear to be a recipe for making libmylib.a. If you put them into the syntax make needs to say that, 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, rather than the contents of the library, and it is best to put the library options at the end:

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

if you like, you can use an option to tell gcc to look for libraries in the current directory:

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

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

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

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

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

and the library rule as:

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

the $+ here means "all of the dependency file names".

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



来源:https://stackoverflow.com/questions/31421616/c-creating-static-library-and-linking-using-a-makefile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!