使用 Makefile 编译失败但在命令行中成功
compilation with Makefile fail but success in command line
我使用 Makefile 来编译我的项目并编译成功,但是当我向 linker (-lbcm2835) 添加一个新的库 (libbcm2835.a) 时它失败了,否则当使用按照它编译的命令和 link 没有错误:
gcc -c ihome_*.c
gcc -o iHome_Start ihome*.o -lbcm2835 -lpthread
生成文件:
# project name (generate executable with this name)
TARGET = iHome_Start
CC = gcc
# compiling flags here
CFLAGS = -std=c99 -Wall -I.
LINKER = gcc -o
# linking flags here
LFLAGS = -lpthread -lbcm2835
# change these to set the proper directories where each files shoould be
SRCDIR = .
OBJDIR = .
BINDIR = .
SOURCES := $(wildcard $(SRCDIR)/*.c)
INCLUDES := $(wildcard $(SRCDIR)/*.h)
OBJECTS := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
rm = rm -f
$(BINDIR)/$(TARGET): $(OBJECTS)
@$(LINKER) $@ $(LFLAGS) $(OBJECTS)
@echo "Linking complete!"
$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
@$(CC) $(CFLAGS) -c $< -o $@
@echo "Compiled "$<" successfully!"
.PHONEY: clean
clean:
@$(rm) $(OBJECTS)
@echo "Cleanup complete!"
.PHONEY: remove
remove: clean
@$(rm) $(BINDIR)/$(TARGET)
@echo "Executable removed!"
问题出在
@$(LINKER) $@ $(LFLAGS) $(OBJECTS)
链接器按照参数出现的顺序处理参数。当它看到库时,它还没有看到任何目标文件,因此没有未解析的符号,因此它不会从库中提取任何内容。交换 $(OBJECTS)
和 $(LFLAGS)
:
@$(LINKER) $@ $(OBJECTS) $(LFLAGS)
我还建议将 LFLAGS
重命名为 LIBRARIES
。
我使用 Makefile 来编译我的项目并编译成功,但是当我向 linker (-lbcm2835) 添加一个新的库 (libbcm2835.a) 时它失败了,否则当使用按照它编译的命令和 link 没有错误:
gcc -c ihome_*.c
gcc -o iHome_Start ihome*.o -lbcm2835 -lpthread
生成文件:
# project name (generate executable with this name)
TARGET = iHome_Start
CC = gcc
# compiling flags here
CFLAGS = -std=c99 -Wall -I.
LINKER = gcc -o
# linking flags here
LFLAGS = -lpthread -lbcm2835
# change these to set the proper directories where each files shoould be
SRCDIR = .
OBJDIR = .
BINDIR = .
SOURCES := $(wildcard $(SRCDIR)/*.c)
INCLUDES := $(wildcard $(SRCDIR)/*.h)
OBJECTS := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
rm = rm -f
$(BINDIR)/$(TARGET): $(OBJECTS)
@$(LINKER) $@ $(LFLAGS) $(OBJECTS)
@echo "Linking complete!"
$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
@$(CC) $(CFLAGS) -c $< -o $@
@echo "Compiled "$<" successfully!"
.PHONEY: clean
clean:
@$(rm) $(OBJECTS)
@echo "Cleanup complete!"
.PHONEY: remove
remove: clean
@$(rm) $(BINDIR)/$(TARGET)
@echo "Executable removed!"
问题出在
@$(LINKER) $@ $(LFLAGS) $(OBJECTS)
链接器按照参数出现的顺序处理参数。当它看到库时,它还没有看到任何目标文件,因此没有未解析的符号,因此它不会从库中提取任何内容。交换 $(OBJECTS)
和 $(LFLAGS)
:
@$(LINKER) $@ $(OBJECTS) $(LFLAGS)
我还建议将 LFLAGS
重命名为 LIBRARIES
。