简体   繁体   English

Makefile 中的问题 C LINUX

[英]Makefile issue in C LINUX

I have the following files in a folder:我在一个文件夹中有以下文件:

main.c |主.c | file1.c |文件1.c | file1.h |文件1.h | file2.c |文件2.c | file2.h文件2.h

main.c is dependent on file1.h and file2.h. main.c 依赖于 file1.h 和 file2.h。 However, file1.c is also dependent on file2.h, and this is my problem.但是,file1.c 也依赖于 file2.h,这是我的问题。

Here's what I have:这是我所拥有的:

CC = gcc
CFLAGS = -Wall -g

BIN = main


all: $(BIN)


main: main.o file1.o file2.o
    $(CC) -o $@ $^

main.o: main.c file1.h
    $(CC) $(CFLAGS) -c -o $@ $<

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

file2.o: file1.c file1.h
    $(CC) $(CFLAGS) -c -o $@ $<

clean:
    rm -f *.o $(BIN)

Obviously this doesn't work.显然这是行不通的。

What can I do to make my makefile work?我该怎么做才能使我的 makefile 工作? And to improve it?并改进它?

EDIT:编辑:

In main.c:在 main.c 中:

#include "file1.h"

In file1.c:在文件 1.c 中:

#include "file1.h"

In file1.h:在文件 1.h 中:

#include "file2.h"

In file2.c:在文件 2.c 中:

#include "file2.h"

I have no includes in file2.h.我在 file2.h 中没有包含。

Am I doing this right?我这样做对吗?

Just add file2.h as a dependency of file1.o只需添加 file2.h 作为 file1.o 的依赖项

It should work.它应该工作。

I suggest using make 's wildcard matching.我建议使用make的通配符匹配。 For the demonstration, I filled the files with some simple functions.为了演示,我用一些简单的函数填充了文件。

file2.h文件2.h

void func2(void);

file2.c文件2.c

#include "file2.h"

void func2()
{
        return;
}

file1.h文件1.h

#include "file2.h"

void func1();

file1.c文件1.c

#include "file1.h"

void func1()
{
        func2();
        return;
}

main.c main.c

#include "file1.h"

int main()
{
        func1();
        return 0;
}

Now, you can use make 's wildcard matching, Ex, *.o: *.c for producing .o file from any .c file.现在,您可以使用make的通配符匹配,例如*.o: *.c从任何.c文件生成.o文件。

Makefile Makefile

CC = gcc
CFLAGS = -Wall -g
OBJS = main.o file1.o file2.o
BIN = main

all : $(BIN)

main: $(OBJS)
        $(CC) $(CFLAGS) -o $@ $^

*.o : *.c
        $(CC) $(CFLAGS) -c -o $@ $^
clean:
        rm -f *.o $(BIN)

Terminal Session端子 Session

$ ls 
file1.c  file1.h  file2.c  file2.h  main.c  Makefile
$ make 
gcc -Wall -g   -c -o main.o main.c
gcc -Wall -g   -c -o file1.o file1.c
gcc -Wall -g   -c -o file2.o file2.c
gcc -Wall -g -o main main.o file1.o file2.o
$ ./main
$ make clean 
rm -f *.o main
$ ls 
file1.c  file1.h  file2.c  file2.h  main.c  Makefile

In general, it is advised to keep .h files separately, Ex, having includes directory.一般来说,建议单独保存.h文件,例如, includes目录。 With that modification, Makefile becomes通过该修改, Makefile变为

Makefile Makefile

CC = gcc
CFLAGS = -Wall -g -I./includes
OBJS = main.o file1.o file2.o

main: $(OBJS)
        $(CC) $(CFLAGS) -o $@ $^

*.o : *.c
        $(CC) $(CFLAGS) -c -o $@ $^
clean:
        rm -f *.o $(BIN)

Terminal Session端子 Session

$ ls 
file1.c  file2.c  includes  main.c  Makefile
$ ls includes/
file1.h  file2.h
$ make 
gcc -Wall -g -I./includes   -c -o main.o main.c
gcc -Wall -g -I./includes   -c -o file1.o file1.c
gcc -Wall -g -I./includes   -c -o file2.o file2.c
gcc -Wall -g -I./includes -o main main.o file1.o file2.o
$ ./main 
$ make clean 
rm -f *.o main
$ ls
file1.c  file2.c  includes  main.c  Makefile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM