cc: error: this_example.o: No such file or directory

cc: error: this_example.o: No such file or directory

我有一个带有一些示例的 SDK,但是当我想编译这些示例中的任何一个时,我收到此错误:

cc -g -I -o this_example.o -c this_example.c
cc: error: this_example.o: No such file or directory
make: *** [Makefile:15: this_example.o] Error 1

这是生成文件:

CC ?= gcc
INC_PATH ?= $(realpath ../inc)
LIB_PATH ?= $(realpath ../lib)
LIBS ?= $(wildcard $(LIB_PATH)/*.a) -pthread -lrt -lm
LDFLAGS :=-g -L$(LIB_PATH)
CFLAGS +=-g -I$(INC_PATH)

EXAMPLES=sdk_this_example_folder

.PHONY: all

all: $(EXAMPLES)

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

this_example: this_example.o  frozen.o
    $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) -Wl,-rpath,'$$ORIGIN/../../lib'

clean:
    rm -f *~ *.o $(EXAMPLES)

在 this_example_folder 中,我有四个文件:Makefile、this_example.c 和 frozen.c 以及 frozen.h。问题出在哪里?谢谢大家的帮助!

realpath 函数 returns 如果您要扩展的路径不存在,则为空字符串。

你有:

INC_PATH ?= $(realpath ../inc)

然后:

CFLAGS +=-g -I$(INC_PATH)

你的命令输出是:

cc -g -I -o this_example.o -c this_example.c

显然,INC_PATH 的值为空(因为命令行中 -I 之后没有任何内容)这意味着目录 ../inc 不存在。

至于为什么会失败:-I 选项之后需要一个文件名。由于您没有,它使用 -o(命令行中的下一个词)作为文件名。这意味着 this_example.o 被视为要编译的文件,而不是输出文件,当然它不存在。