These are typically part of the linker command line, and are either supplied directly in a target action, or more commonly assigned to a make
variable that will be expanded to form link command. In that case:
-L
is the path to the directories containing the libraries. A search path for libraries.
-l
is the name of the library you want to link to.
For instance, if you want to link to the library ~/libs/libabc.a
you'd add:
-L$(HOME)/libs -labc
To take advantage of the default implicit rule for linking, add these flags to the variable LDFLAGS
, as in
LDFLAGS+=-L$(HOME)/libs -labc
It's a good habit to separate LDFLAGS
and LIBS
, for example
# LDFLAGS contains flags passed to the compiler for use during linking
LDFLAGS = -Wl,--hash-style=both
# LIBS contains libraries to link with
LIBS = -L$(HOME)/libs -labc
program: a.o b.o c.o
$(CC) $(LDFLAGS) $^ $(LIBS) -o $@
# or if you really want to call ld directly,
# $(LD) $(LDFLAGS:-Wl,%=%) $^ $(LIBS) -o $@
Even if it may work otherwise, the -l...
directives are supposed to go after the objects that reference those symbols. Some optimizations (-Wl,--as-needed
is the most obvious) will fail if linking is done in the wrong order.
-----------------------------------------------------------------------------------------
关键词: Make宏定义 Make传递宏定义 Makefile中添加宏定义 Makefile -D
在Makefile中我们可以通过宏定义来控制源程序的编译。只要在Makefile中的CFLAGS中通过选项-D来指定你于定义的宏即可。
如:
CFLAGS += -D _YUQIANG
在编译的时候加上此选项就可以了: $(CC) $(CFLAGS) $^ -o $@
下面是我写的一个测试文件:
例如:
Makefile文件内容为:
CC = gcc
RM = rm
CFLAGS += -D _YUQIANG
TARGETS := myapp
all:$(TARGETS)
$(TARGETS):main.c
$(CC) $(CFLAGS) $^ -o $@
clean:
-$(RM) -f *.o
-$(RM) -f $(TARGETS)
main.c文件的内容为:
#include <stdio.h>
int main()
{
#ifdef _YUQIANG
printf("Hello Yu Qiang, How are you?\n");
#else
printf("Sorry to lost you. \n");
#endif
return 0;
}
在端口中输入 make clean all
然后输入 ./myapp
结果 Hello Yu Qiang, How are you?
本文来源:『』 有水的地方就有余文章转载自20065562's Blog
请点击这里查看原文