简体   繁体   English

无法从Makefile链接静态库

[英]Cannot link static library from Makefile

I build a static library. 我建立一个静态库。 The problem is that I can't link it from my Makefile 问题是我无法从我的Makefile链接它

TARGET  =       AR1020
CC      =       gcc
CFLAGS  =       -Wall -std=c99 -I./inc/
LINKER  =       gcc -o
LFLAGS  =       -Wall -static -I./inc/


SRCDIR  =       src
INCDIR  =       inc
OBJDIR  =       obj
BINDIR  =       bin

LIBDIR  =       ./lib
LIBFLAG =       -li2c


SOURCES         :=      $(wildcard $(SRCDIR)/*.c)
INCLUDES        :=      $(wildcard $(INCDIR)/*.h)
OBJECTS         :=      $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
rm              =       rm -f

$(BINDIR)/$(TARGET): $(OBJECTS)
        @$(LINKER) $@ $(LFLAGS) -L$(LIBDIR) $(LIBFLAG) $(OBJECTS)
        @echo "Linking complete!"

$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
        @$(CC) $(CFLAGS) -c $< -o $@
        @echo "Compiled "$<" successfully"

.PHONY: clean
clean:
        @$(rm) $(OBJECTS)
    @echo "Cleanup complete!"

.PHONY: remove
remove: clean
        @$(rm) $(BINDIR)/$(TARGET)
        @echo "Exacutable removed!"

My tree is: 我的树是:

|-- bin
|-- inc
|   |-- color.h
|   |-- EXT.h
|   |-- EXT.h~
|   |-- gpio_lib.h
|   `-- test.h
|-- lib
|   |-- i2c.c
|   |-- i2c.o
|   `-- libi2c.a
|-- Makefile
|-- obj
|   |-- AR1020.o
|   |-- gpio_lib.o
|   |-- gpio.o
|   `-- test.o
`-- src
    |-- AR1020.c
    |-- gpio_lib.c
    `-- libi2c.a

I want to link libi2c.a but I'm getting the error "undefined reference to '.....'. If I compile it manually like: 我想链接libi2c.a,但出现错误“未定义对'.....'的引用。如果我手动编译它,例如:

gcc src/AR1020.c lib/libi2c.a

Everything compiles as it should be. 一切都应该编译。 Can someone help me? 有人能帮我吗?

Libraries you want to link in needs to come after the object files that uses anything in those libraries, so 要链接的库需要放在使用这些库中任何内容的目标文件之后,因此

@$(LINKER) $@ $(LFLAGS) -L$(LIBDIR) $(LIBFLAG) $(OBJECTS)

should be 应该

@$(LINKER) $@ $(LFLAGS) -L$(LIBDIR)  $(OBJECTS) $(LIBFLAG)

I've found a useful article about linking libraries. 我找到了有关链接库的有用文章。 http://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html http://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html

The linker checks each file in turn. 链接器依次检查每个文件。 If it is an object file, it is being placed fully into the executable file. 如果它是一个目标文件,则将其完全放入可执行文件中。 If it is a library, the linker checks to see if any symbols referenced (ie used) in the previous object files but not defined (ie contained) in them, are in the library. 如果它是一个库,则链接器将检查库中是否存在先前对象文件中引用(即使用)但未在其中定义(即包含)的任何符号。 If such a symbol is found, the whole object file from the library that contains the symbol - is being added to the executable file. 如果找到了这样的符号,则包含该符号的库中的整个目标文件都将添加到可执行文件中。 This process continues until all object files and libraries on the command line were processed. 继续此过程,直到处理完命令行上的所有目标文件和库为止。

Note that object files found on the command line are always fully included in the executable file, so the order of mentioning them does not really matter. 请注意,在命令行上找到的目标文件始终完全包含在可执行文件中,因此提及它们的顺序并不重要。 Thus, a good rule is to always mention the libraries after all object files 因此,一个好的规则是总是在所有目标文件之后提及这些库

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

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