简体   繁体   English

Makefile 用于简单的 C 项目

[英]Makefile for simple C project

I have a C mini-project and I need to create a makefile to build it.我有一个 C 迷你项目,我需要创建一个 makefile 来构建它。

The structure of the src code looks like this src 代码的结构如下所示

.
├── lib
│   ├── foo.c
│   └── foo.h
├── main.c
└── Makefile

main.c main.c

#include "foo.h"

int main() {
  // call a function in another file
  myPrintHelloMake();

  return 0;
}

foo.c foo.c

#include <stdio.h>
#include "foo.h"

void myPrintHelloMake(void) {

  printf("Hello makefiles!\n");

  return;
}

foo.h foo.h

#ifndef _FOO_H_
#define _FOO_H_

void myPrintHelloMake(void);

#endif // _FOO_H_

This is the Makefile I tried to create这是我尝试创建的 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

This is the error log这是错误日志

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

What I need is我需要的是

  • Include "foo.h" instead of include "./lib/foo.h" in main.c在 main.c 中包含“foo.h”而不是包含“ main.c
  • Put object files ( foo.o , main.o ) into a specified directory.将 object 文件( foo.omain.o )放入指定目录。
  • Compile successfully.编译成功。

Thanks for your help.谢谢你的帮助。

This Makefile should work:这个 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

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

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