简体   繁体   English

如何使我的makefile自动链接库?

[英]How do I make my makefile automatically link libraries?

#include <stdio.h>
#include <math.h>

int main(void)
{
    double test=23.4;
    test=sqrt(test);
    printf("%f",test);
}

In order to compile this I use: gcc -o test test.c -L/path/to/libs -lm 为了进行编译,我使用了:gcc -o test test.c -L / path / to / libs -lm

however, I would like to use my makefile in order to automatically link the math library and whatever other standard libraries I use in the future. 但是,我想使用我的makefile来自动链接数学库和将来使用的任何其他标准库。

This is the makefile I am using: 这是我正在使用的makefile:

CC=gcc
CFLAGS=-Wall
LDFLAGS=-lm

OBJECTS=client.o
SOURCE=client.c

client: $(OBJECTS)
    $(CC) $(CFLAGS) $(OBJECTS) -o client $(LDFLAGS) $(LIBS)

all:client

.PHONY: clean
clean:
    rm -f *~ *.o client

how do I modify this makefile in order to automatically link the libraries? 如何修改此Makefile以便自动链接库?

From the comments in the other answer, it appears you are using make test to build a different program than what is specified in the makefile (what you get when you just type make ) If you want that to work, you need to use the correct variable names for the default rules: 从另一个答案的注释中可以看出,您正在使用make test来构建与makefile中指定的程序不同的程序(仅键入make时会得到的内容)。如果要使其正常工作,则需要使用正确的程序。默认规则的变量名称:

CC=gcc
CFLAGS=-Wall
LDFLAGS=-L/path/to/libs
LDLIBS=-lm

with just that in your makefile, you can use make test to compile test.c into an executable named test (or make client to compile client.c). 只需在makefile中添加该代码,就可以使用make test将test.c编译为名为test的可执行文件(或make client编译client.c)。

If you want to compile multiple files into a single executable, you need to add rules like you have in your makefile (but instead using the standard variable names): 如果要将多个文件编译为单个可执行文件,则需要添加makefile中的规则(但要使用标准变量名):

OBJECTS=client.o otherfile.o extra.o

client: $(OBJECTS)
        $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

edit 编辑

Just to be clear, the default rule that GNU-make applies to .c files to produce executables when there's no other rule that matches the executable is: 需要明确的是,当没有其他与可执行文件匹配的规则时,GNU make适用于.c文件以生成可执行文件的默认规则是:

.c:
         $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $^ $(LOADLIBES) $(LDLIBS) -o $@

Other versions of make do something similar (the $(TARGET_ARCH) and $(LOADLIBES) are specific to GNU make; many makes also don't have $(CPPFLAGS) ). 其他版本的make也执行类似的操作( $(TARGET_ARCH)$(LOADLIBES)特定于GNU make;许多make也没有$(CPPFLAGS) )。 You can see the default set of rules by running make -p in a directory with no Makefile. 您可以通过在没有Makefile的目录中运行make -p来查看默认规则集。

You can replace the above pattern rule with something different in the Makefile if you wish, in which case make will do whatever you tell it to do. 如果需要,可以用Makefile中的其他内容替换上述模式规则,在这种情况下,make会执行您要执行的任何操作。

CC=gcc
CFLAGS=-Wall
LDFLAGS=-lm
LIBPATH=-L/path/to/libs

OBJECTS=client.o
SOURCE=client.c

client: $(OBJECTS)
    $(CC) $(CFLAGS) $(OBJECTS) -o client $(LIBPATH) $(LDFLAGS) $(LIBS)

all:client

.PHONY: clean
clean:
rm -f *~ *.o client

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

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