-L和-l命令在Makefile中不起作用 [英] -L and -l commands don't work in Makefile

查看:294
本文介绍了-L和-l命令在Makefile中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Makefile:

  TOP = ../Bank/src 
CC = gcc

CFLAGS = -g -Wall -std = c99
LDFLAGS = -L $(TOP)
LFLAGS = -lVirtualBank
INCLUDES = -I $(TOP)/ VirtualBank /
LIBS = VirtualBank.a

BANK_SOURCES = $(TOP)/bank.c
VirtualBank_SOURCES = $(TOP)/VirtualBank/bankServer.c $(TOP)/ VirtualBank / dataBase.c $(TOP)/VirtualBank/account.c

BANK_OBJECTS = $(BANK_SOURCES:.c = .o)
VirtualBank_OBJECTS = $(VirtualBank_SOURCES:.c = .o)
TARGET = bank

all:VirtualBank.a $(TARGET)

VirtualBank.a:$(VirtualBank_OBJECTS)
rm -f $ @
ar cq $ @ $(VirtualBank_OBJECTS)
mv * .a $(TOP)/

$(VirtualBank_OBJECTS):$(VirtualBank_SOURCES)
$ $(CFLAGS)$(INCLUDES)$(VirtualBank_SOURCES)
mv * .o $(TOP)/ VirtualBank /

$(TARGET):$(BANK_OBJECTS)
$ CC)$(CFLAGS)$(INCLUDES)$(BANK_OBJECTS)-o $(TARGET)$(LDFLAGS)$(LFLAGS)
chmod + x $(TARGET)

$(BANK_OBJECTS ):$(BANK_SOURCES)
$(CC)$(CFLAGS)-c $(INCLUDES)$(BANK_SOURCES)
mv * .o $(TOP)/

b $ b clean:
cd ../Bank
rm -rf * .o bank
cd $(TOP)
rm -rf * .o * .a
cd $(TOP)/ VirtualBank
rm -rf * .o

此项目: https://github.com/MihaiPro/Bank
当我运行 make 命令,commpiler找不到VirtualBank库。我认为问题是在-l命令,我再次阅读这个命令,但我没有找到什么问题。



你能帮助我吗?



感谢!



EDIT



错误讯息: p>

  gcc -g -Wall -std = c99 -I ../ Bank / src / VirtualBank /../Bank/src/bank .o -o bank -L ../ Bank / src -lVirtualBank 
/ usr / bin / ld:找不到-lVirtualBank
collect2:错误:ld返回1退出状态
make:* ** [bank]错误1


解决方案

=http://linux.die.net/man/1/ld> ld(1):

  -l namespec 
--library = namespec
将namespec指定的归档或目标文件添加到要链接的文件列表。此选项可以使用任意次数。如果namespec的格式为:filename,ld将在库路径中搜索名为filename的文件,否则将搜索库路径以查找名为libnamespec.a的文件。
在支持共享库的系统上,ld还可以搜索除libnamespec.a之外的文件。具体来说,在ELF和SunOS系统上,ld将搜索一个名为libnamespec.so的库,搜索一个名为libnamespec.a的库。 (按照惯例,.so扩展表示共享库。)请注意,此行为不适用于:filename,它始终指定一个名为filename的文件。

链接器将只在命令行上指定的位置搜索一次档案。如果归档定义了在命令行上归档之前出现的某个对象中未定义的符号,则链接器将包括归档中的相应文件。但是,稍后在命令行中显示的对象中的未定义符号不会导致链接器再次搜索归档。

查看 - (选项,强制链接器多次搜索压缩文件

你可以在命令行上多次列出相同的压缩文件

这种类型的归档搜索是Unix链接器的标准,但是如果在AIX上使用ld,请注意它与AIX链接器的行为不同



您的库称为 VirtualBank.a ,但 ld 将查找 libVirtualBank。 a


I have following Makefile:

TOP = ../Bank/src
CC = gcc

CFLAGS = -g -Wall -std=c99
LDFLAGS = -L$(TOP)
LFLAGS = -lVirtualBank
INCLUDES = -I$(TOP)/VirtualBank/ 
LIBS = VirtualBank.a

BANK_SOURCES = $(TOP)/bank.c 
VirtualBank_SOURCES = $(TOP)/VirtualBank/bankServer.c $(TOP)/VirtualBank/dataBase.c $(TOP)/VirtualBank/account.c

BANK_OBJECTS = $(BANK_SOURCES:.c=.o)
VirtualBank_OBJECTS = $(VirtualBank_SOURCES:.c=.o)
TARGET = bank

all: VirtualBank.a $(TARGET)

VirtualBank.a:$(VirtualBank_OBJECTS)
    rm -f $@
    ar cq $@ $(VirtualBank_OBJECTS)
    mv *.a $(TOP)/

$(VirtualBank_OBJECTS):$(VirtualBank_SOURCES)
    $(CC) -c $(CFLAGS) $(INCLUDES) $(VirtualBank_SOURCES)
    mv *.o $(TOP)/VirtualBank/

$(TARGET):$(BANK_OBJECTS) 
    $(CC) $(CFLAGS) $(INCLUDES) $(BANK_OBJECTS) -o $(TARGET) $(LDFLAGS) $(LFLAGS)
    chmod +x $(TARGET)

$(BANK_OBJECTS):$(BANK_SOURCES)
    $(CC) $(CFLAGS) -c $(INCLUDES) $(BANK_SOURCES)
    mv *.o $(TOP)/


clean: 
    cd ../Bank
    rm -rf *.o bank
    cd $(TOP)
    rm -rf *.o *.a
    cd $(TOP)/VirtualBank
    rm -rf *.o

It is from this project: https://github.com/MihaiPro/Bank When I run make command, commpiler doesn't find VirtualBank library. I think the problem is on -l command, I read again about this command, but I don't find what is problem.

Can you help me?

Thanks!

EDIT

Error message:

gcc -g -Wall -std=c99 -I../Bank/src/VirtualBank/  ../Bank/src/bank.o -o bank -L../Bank/src -lVirtualBank
/usr/bin/ld: cannot find -lVirtualBank
collect2: error: ld returned 1 exit status
make: *** [bank] Error 1

解决方案

From ld(1):

-l namespec
--library=namespec
Add the archive or object file specified by namespec to the list of files to link. This option may be used any number of times. If namespec is of the form :filename, ld will search the library path for a file called filename, otherwise it will search the library path for a file called libnamespec.a.
On systems which support shared libraries, ld may also search for files other than libnamespec.a. Specifically, on ELF and SunOS systems, ld will search a directory for a library called libnamespec.so before searching for one called libnamespec.a. (By convention, a ".so" extension indicates a shared library.) Note that this behavior does not apply to :filename, which always specifies a file called filename.

The linker will search an archive only once, at the location where it is specified on the command line. If the archive defines a symbol which was undefined in some object which appeared before the archive on the command line, the linker will include the appropriate file(s) from the archive. However, an undefined symbol in an object appearing later on the command line will not cause the linker to search the archive again.

See the -( option for a way to force the linker to search archives multiple times.

You may list the same archive multiple times on the command line.

This type of archive searching is standard for Unix linkers. However, if you are using ld on AIX , note that it is different from the behaviour of the AIX linker.

Your library is called VirtualBank.a, but ld will look for libVirtualBank.a.

这篇关于-L和-l命令在Makefile中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆