【问题标题】:Makefile .c + .as with ncurses [closed]Makefile .c + .as 与 ncurses [关闭]
【发布时间】:2018-05-23 18:11:14
【问题描述】:

我在编译我的程序以使用 ncurses 时遇到了一些问题。我是创建 makefile 的新手,所以:

这是我的生成文件:

test_reservation: reservation.o service.o
    gcc -g -m32 service.o reservation.o -o test_reservation 
service.o: service.c
    gcc -g -m32 -c -o service.o service.c -std=c99 -lncurses
reservation.o: reservation.s
    as -gdbb --32 -o reservation.o reservation.s

我已经安装并包含在我的 c 文件中

当我努力做到时:

gcc -g -m32 service.o reservation.o -o test_reseration
service.o: In function `main':
/home/xxx/reservationService/service.c:23: undefined reference to `initscr'
collect2: error: ld returned 1 exit status
makefile:2: recipe for target 'test_reservation' failed
make: *** [test_reservation] Error 1

这个makefile有什么问题?应该怎么编译?

【问题讨论】:

  • 您需要将-lncurses 添加到链接命令(第一条规则)而不是编译。

标签: c linux assembly makefile


【解决方案1】:

这里有一个更适合你的 Makefile 框架:

PKGS    := ncursesw
CC      := gcc
CFLAGS  := -Wall -m32 -O2 -std=c99 $(shell pkg-config --cflags $(PKGS))
LDFLAGS := $(shell pkg-config --libs $(PKGS))
AS      := as
ASFLAGS := -gdbb --32
PROGS   := test-reservation

.PHONY: all clean

all: $(PROGS)

clean:
    $(RM) *.o $(PROGS)

%.o: %.c
    $(CC) $(CFLAGS) -c $^ -o $@

%.o: %.s
    $(AS) $(ASFLAGS) $^ -o $@

test-reservation: reservation.o service.o
    $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@

如果您复制粘贴以上内容,请记住运行sed -e 's|^ *|\t|' -i Makefile 来修复缩进。

大多数 Linux 发行版使用pkg-config 来跟踪编译特定库所需的选项。您只需将所需的包添加到PKGS 变量中,以空格分隔。 ncursesw 是支持宽字符的 ncurses 版本(您需要 Unicode 字符输出)。 $(shell pkg-config ... $(PKGS)) 成语使用 pkg-config shell 命令来拉入正确的标志 (--cflags) 和库文件 (--libs)。

如果您使用<math.h>,您需要在-lm 前面加上LDFLAGS;即LDFLAGS := -lm $(shell pkg-config --libs $(PKGS))

如果您在该目录中编译多个二进制文件,只需将最终二进制名称添加到PROGS,并添加新规则,例如

foo-bin: foo.o bar.o baz.o
    $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@

规则动作没有改变,只有目标和先决条件改变。

使用all: $(PROGS) 规则,您只需运行make 即可编译所有以PROGS 命名的二进制文件。如果你修改service.c,那么service.otest-reservation 都将被重新编译。运行make clean all 删除所有已编译的文件,然后重新编译它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多