【问题标题】:Makefile C Linux ERRORMakefile C Linux 错误
【发布时间】:2016-10-30 02:52:40
【问题描述】:

我似乎无法弄清楚为什么我的 makefile 没有正确执行。我收到以下错误:

gcc hr_timer.c -o hr
gcc   hr_timer.o   -o hr_timer
gcc: error: hr_timer.o: No such file or directory
gcc: fatal error: no input files
compilation terminated.
make: *** [hr_timer] Error 4

这是我的生成文件:

CC = gcc
CFLAGS = -pthread

all: hr_timer q2q3 process_switching thread_switching

hr_timer.o: hr_timer.c
    $(CC) hr_timer.c -o hr

q2q3.o: q2q3.c
    $(CC) q2q3.c -o qq 

process_switching.o: process_switching.c
    $(CC) process_switching.c -o pr

thread_switching.o: thread_switching.c
    $(CC) thread_switching.c -o th

这是它所在的目录:

所有 .c 文件都可以在没有 makefile 的情况下正常编译。谢谢!

编辑:

新的生成文件:

CC = gcc
CFLAGS = -pthread

all: hr_timer q2q3 process_switching thread_switching

hr_timer.o: hr_timer.c
    $(CC) hr_timer.c -o hr hr_timer.o

q2q3.o: q2q3.c
    $(CC) q2q3.c -o qq q2q3.o

process_switching.o: process_switching.c
    $(CC) process_switching.c -o pr process_switching.o

thread_switching.o: thread_switching.c
    $(CC) $(CFLAGS) thread_switching.c -o th thread_switching.o

错误:

gcc hr_timer.c -o hr hr_timer.o
gcc: error: hr_timer.o: No such file or directory
make: *** [hr_timer.o] Error 1

EDIT2(修复):

CC = gcc
CFLAGS = -pthread

all: hr qq pr th

hr: hr_timer.c
    $(CC) hr_timer.c -o hr

qq: q2q3.c
    $(CC) q2q3.c -o qq

pr: process_switching.c
    $(CC) process_switching.c -o pr

th: thread_switching.c
    $(CC) $(CFLAGS) thread_switching.c -o th

【问题讨论】:

    标签: c linux makefile


    【解决方案1】:

    你在这里遇到的主要问题是你没有达到你所说的目标!规则

    hr_timer.o: hr_timer.c
        $(CC) hr_timer.c -o hr
    

    创建一个名为 hr 的目标文件(这就是 -o hr 所做的)。相反,它应该是:

    hr_timer.o: hr_timer.c
        $(CC) hr_timer.c -o hr_timer.o
    

    此文件中的其余目标也是如此。简而言之,Makefile 具有目标和依赖项。它们遵循以下语法:

    target: dependency1 dependency2 dependency3 ...
        command that makes target from the dependencies
    

    每条规则都告诉make,如果所有依赖项都存在,则可以make target,然后执行以下命令。这允许 make 通过首先使其成为依赖项来尝试使您的最终可执行文件,如果这些依赖项具有依赖项,它也会使这些依赖项(等等)。但是,如果在执行规则后,冒号左边列出的目标没有生成,那么后面引用的时候就会出现问题,文件就不存在了!

    另外,值得注意的是,您有一个变量CFLAGS 定义为具有-pthread。您可能希望在每个规则中将其传递给编译器,如下所示:

    hr_timer.o: hr_timer.c
        $(CC) hr_timer.c $(CFLAGS) -o hr_timer.o
    

    【讨论】:

    • 感谢您的回复。我进行了您提到的更改,但仍然出现错误。请检查编辑。
    • 不应该是-o hr hr_timer.o。它应该只是-o hr_timer.o,它表示输出应该在文件 hr_timer.o 中。中间不需要hr。仔细阅读
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多