简体   繁体   English

C:使用Makefile创建静态库和链接

[英]C: Creating static library and linking using a Makefile

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. 我想要执行以下操作来创建一个makefile,它将单独的编译和链接分开,以便在形成最终的静态可执行文件时创建和链接静态库。

I have the following code for the Makefile, but I am getting the following error 我有Makefile的以下代码,但我收到以下错误

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. 如果我在终端中的line 12之后运行它们工作的命令,而不是在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. 此外,如果您可以协助改进代码,我将非常感激。

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. 这组规则首先编译所有文件,然后使库(libmylib.a)成为目标并使用它的工件链接可执行文件。 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

A makefile is not a shell script. makefile不是shell脚本。 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. 告诉系统如果决定它需要这样做,如何制作名为“myProgram”的文件。 The parts after the colon are files that myProgram needs. 冒号后面的部分是myProgram需要的文件。 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. 如果它们不存在,或者决定它们已经过时,make会尝试找到一些可用于创建或更新它们的配方。 Once all that is done, make then executes the "gcc ..." line and assumes that will create or update myProgram. 完成所有操作后,make会执行“gcc ...”行并假定将创建或更新myProgram。

The ar and ranlib lines you have don't match the needed syntax for a makefile rule. 您拥有的ar和ranlib行与makefile规则所需的语法不匹配。 From the look of them, they appear to be a recipe for making libmylib.a. 从它们的外观来看,它们似乎是制作libmylib.a的秘诀。 If you put them into the syntax make needs to say that, you get: 如果你把它们放入语法make需要说,你得到:

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应该依赖于库本身,而不是库的内容,最好将库选项放在最后:

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在当前目录中查找库:

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: 还有makefile变量可以帮助你不必重复这么多,所以我会把第一条规则写成:

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

however, it is unlikely that main.o should actually be part of the library, so: 但是,main.o不太可能实际上是库的一部分,所以:

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. 最后,如果你想让gcc创建一个实际的静态可执行文件,而不只是使用你创建的库,你需要将'-static'选项传递给gcc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM