简单C项目的Makefile
我有一个C迷你项目,我需要创建一个makefile来构建它 src代码的结构如下所示简单C项目的Makefile,c,linux,gcc,makefile,C,Linux,Gcc,Makefile,我有一个C迷你项目,我需要创建一个makefile来构建它 src代码的结构如下所示 。 ├── 解放党 │ ├── 富科 │ └── 福安 ├── main.c └── 生成文件 main.c #包括“foo.h” int main(){ //调用另一个文件中的函数 myPrintHelloMake(); 返回0; } 富科 #包括 #包括“foo.h” 作废myPrintHelloMake(作废){ printf(“Hello makefiles!\n”); 返回; }
。
├── 解放党
│ ├── 富科
│ └── 福安
├── main.c
└── 生成文件
main.c
#包括“foo.h”
int main(){
//调用另一个文件中的函数
myPrintHelloMake();
返回0;
}
富科
#包括
#包括“foo.h”
作废myPrintHelloMake(作废){
printf(“Hello makefiles!\n”);
返回;
}
福安
\ifndef\u FOO\H_
#定义\u FOO\u H_
作废myPrintHelloMake(作废);
#(完)_
这是我试图创建的Makefile
CC = gcc
CFLAGS = -I.
RM = rm -f
DEP = ./lib/foo.h
# Object files depend on c file and DEP
%.o: %.c $(DEP)
all : main foo
# Generate output and link object files
foo: ./lib/foo.o
$(CC) ./lib/foo.o -o ./lib/foo
main: main.o ./lib/foo.o
$(CC) main.o ./lib/foo.o -o main
# Generate object files without linking
foo.o: ./lib/foo.c
$(CC) -Wall -c ./lib/foo.c
main.o: main.c ./lib/foo.c
$(CC) -Wall -c main.c ./lib/foo.c
clean:
$(RM) *.o *.out
$(RM) main ./lib/foo
这是错误日志
gcc -Wall -c main.c ./lib/foo.c
gcc main.o ./lib/foo.o -o main
gcc ./lib/foo.o -o ./lib/foo
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [foo] Error 1
我需要的是
- 在
main.c
- 将对象文件(
,foo.o
)放入指定目录main.o
- 编译成功
感谢您的帮助。此Makefile应该可以工作:
CC = gcc
CPPFLAGS = -Ilib
CFLAGS = -Wall -O2
RM = rm -f
MAINEXE = main
MAINDEP = lib/foo.h
MAINOBJS = main.o lib/foo.o
all : $(MAINEXE)
# Generate output and link object files
$(MAINEXE): $(MAINOBJS)
$(CC) $(MAINOBJS) -o $@
$(MAINOBJS): $(MAINDEP)
clean:
$(RM) $(MAINEXE) $(MAINOBJS) *.out
.PHONY: all clean
由于要尝试从库中生成可执行文件(
gcc./lib/foo.o-o./lib/foo
),因此引发了ld
中的错误ld
找不到main
功能。你确定要从你的库中生成可执行文件吗?@krjdev不,我不想从我的库中生成可执行文件,我只需要生成对象文件。那么你就不需要生成文件中的foo:./lib/foo.o
规则了。只需编译main.c
和lib/foo.c
,然后将它们链接到gcc main.o lib/foo.o-o main
@krjdev我现在可以成功构建,但是foo.o
出现在//code>中(与main.o
的级别相同)。您显然已经更改了发布代码中的一些文件名,但并不一致。“lib.c”对“lib/foo.c”,“lib.h”对“lib/foo.h”,包括“hellomake.h”
。