简体   繁体   English

与GCC Makefile斗争

[英]Struggling with a GCC Makefile

I am trying to write what I thought would be quite a simple Makefile and I'm just baffled! 我试图写出我认为将是一个简单的Makefile的文件,但我感到莫名其妙! I'm not a makefile writer, but I thought I understood them enough to be able to get a simple one working. 我不是Makefile编写者,但我认为我对它们的理解足以使他们能够简单地工作。

Okay, I have a small project in a directory and also in this directory is a libs directory containing many .c files. 好的,我在目录中有一个小项目,并且在此目录中还有一个包含许多.c文件的libs目录。 What I'm trying to do is write a makefile that will build the contents of the /libs directory into a static lib file in the /libs directory and then compile a few source files in the / directory and link it against the built .a file. 我想做的是编写一个makefile,它将/ libs目录的内容构建到/ libs目录中的静态lib文件中,然后编译/目录中的一些源文件,并将其链接到已构建的.a上。文件。

I'm sure someone's going to suggest "why not use cmake", but that's not answer I'm looking for (waves hand like a Jedi.. ehehehehe) 我确定有人会建议“为什么不使用cmake”,但这并不是我正在寻找的答案(像绝地武士一样挥舞着手.. ehehehehe)

CC = gcc
CFLAGS = -Wall
SOURCES = lzx.c csum.c dirs.c listner.c tree.c
OBJECTS = $(SOURCES:.c=.o)
TARGETLIB = libs/mylib.a
TARGET = TestApp

libs/%.o : libs/%.c
    $(CC) $CFLAGS -c $<

$(TARGETLIB) : $(OBJECTS)
    ar rcs $@ $^

$(TARGET) :
    $(CC) $CFLAGS Source1.cpp Source2.cpp -llibs/mylib.a -o $@

My understanding was that the first recipe, would compile all the .c files into objects, but it seems to compile the first .c file and then stop. 我的理解是,第一个配方会将所有.c文件编译成对象,但似乎先编译第一个.c文件然后停止。

Any help anyone could give me would be appreciated. 任何人都可以给我的帮助将不胜感激。

Since Your final app is TARGET, You should make it first Makefile rule. 由于您的最终应用程序是TARGET,因此应首先使其成为Makefile规则。 And since it also depends on TARGETLIB it should be given as dependency, like so: 并且由于它也依赖于TARGETLIB,因此应将其作为依赖项,例如:

$(TARGET): $(TARGETLIB)
    $(CC) $(CFLAGS) Source1.cpp Source2.cpp -Lmylib -o $@

next I assume that *.c files You mentioned are lib files. 接下来,我假设您提到的* .c文件是lib文件。 Thus You will need a prefix to them, since You want to specify them by hand, not via wildcard or rule. 因此,您将需要为其添加前缀,因为您希望手动指定它们,而不是通配符或规则。

OBJECTS = $(addprefix(libs, $(SOURCES)):.c=.o)

and last thing that comes to my mind is library name, which supposed to be libSOMENAME.a (well, linker searches for this name in path and -Lotherpaths). 最后我想到的是库名,它应该是libSOMENAME.a(好吧,链接程序在path和-Lotherpaths中搜索该名称)。 So we have: 因此,我们有:

TARGETLIB = libs/libmylib.a

summing it all up: 总结一下:

CC = gcc
CFLAGS = -Wall
SOURCES = lzx.c csum.c dirs.c listner.c tree.c
OBJECTS = $(addprefix(libs, $(SOURCES)):.c=.o)
TARGETLIB = libs/libmylib.a
TARGET = TestApp

$(TARGET) : $(TARGETLIB)
    $(CC) $(CFLAGS) Source1.cpp Source2.cpp -static -L./libs -lmylib -o $@

$(TARGETLIB) : $(OBJECTS)
    ar rcs $@ $^

And yes, this could be written much better, but I assume if You wanted to learn more about Makefiles or linker, and not just shown where You made mistakes, You'd know how to find manual pages. 是的,这可以写得更好,但是我想如果您想了解更多关于Makefiles或链接器的信息,而不仅仅是显示您在哪里出错,您将知道如何找到手册页。

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

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