What is the difference between -I and -L in makefile? and the means of -d.

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.


-----------------------------------------------------------------------------------------

在Makefile中进行宏定义-D(原)


关键词: 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 请点击这里查看原文

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值