【问题标题】:Makefile for simple C project简单 C 项目的 Makefile
【发布时间】:2021-07-20 13:19:20
【问题描述】:

我有一个 C 迷你项目,我需要创建一个 makefile 来构建它。

src代码的结构是这样的

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

main.c

#include "foo.h"

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

  return 0;
}

foo.c

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

void myPrintHelloMake(void) {

  printf("Hello makefiles!\n");

  return;
}

foo.h

#ifndef _FOO_H_
#define _FOO_H_

void myPrintHelloMake(void);

#endif // _FOO_H_

这是我尝试创建的 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.h”而不是包含“./lib/foo.h”
  • 将目标文件(foo.omain.o)放入指定目录。
  • 编译成功。

感谢您的帮助。

【问题讨论】:

  • ld 的错误被抛出,因为您想尝试从您的库 (gcc ./lib/foo.o -o ./lib/foo) 中创建可执行文件。 ld 找不到 main 函数。您确定要从您的库中创建一个可执行文件吗?
  • @krjdev 不,我不想从我的库中制作可执行文件,我只需要制作目标文件。
  • 那么你的 Makefile 中就不需要 foo: ./lib/foo.o 规则了。只需编译main.clib/foo.c,然后将它们与gcc main.o lib/foo.o -o main 链接。
  • @krjdev 我现在可以成功构建,但foo.o 出现在./ 中(与main.o 相同级别)。
  • 您显然更改了发布代码中的一些文件名,但不一致。 “lib.c”与“lib/foo.c”、“lib.h”与“lib/foo.h”、#include "hellomake.h".

标签: c linux gcc makefile


【解决方案1】:

这个 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 2018-11-24
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多