Makefile C Linux错误
我似乎不明白为什么我的makefile没有正确执行。我得到以下错误:Makefile C Linux错误,c,linux,makefile,C,Linux,Makefile,我似乎不明白为什么我的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 这是我的makefile: CC = gcc CFL
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
这是我的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
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
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
这是它所在的目录:
没有makefile,所有的.c文件都可以正常编译。谢谢
编辑:
新生成文件:
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
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
编辑2(修复):
你在这里遇到的主要问题是你没有达到你所说的目标!规则
hr_timer.o: hr_timer.c
$(CC) hr_timer.c -o hr
创建一个名为hr的对象文件(这就是-ohr
所做的)。相反,它应该是:
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通过首先创建依赖项来尝试创建最终的可执行文件,如果这些依赖项有依赖项,它也会创建这些依赖项(等等)。但是,如果在执行规则后,冒号左侧列出的目标没有生成,那么稍后引用它时会出现问题,文件将不存在
另外,值得注意的是,您定义了一个变量CFLAGS
,该变量具有-pthread
。您可能希望在每个规则中将其传递给编译器,如下所示:
hr_timer.o: hr_timer.c
$(CC) hr_timer.c $(CFLAGS) -o hr_timer.o
谢谢你的回复。我做了你提到的更改,但仍然有一个错误。请检查编辑。它不应该是
-o hr hr\u timer.o
。它应该是-o hr\u timer.o
,这表示输出应该被放入文件hr\u timer.o中。中间不需要人力资源。仔细阅读