I can't get this Makefile to work. This is the simplest example. Below is the code and error:
main.c
#include <stdio.h>
#include <stdlib.h>
#include "hello.h"
int main(){
hello();
return EXIT_SUCCESS;
}
hello.c
#include<stdio.h>
#include "hello.h"
void hello(void){
printf("Hello World\n");
}
hello.h
#ifndef _HELLO_H
#define _HELLO_H
void hello(void);
#endif
Makefile
CC=gcc -Wall -pedantic
prog: main.o hello.o
${CC} -o prog main.o hello.o
main.o: main.c hello.h
${CC} -o main.o main.c
hello.o: hello.c hello.h
${CC} -o hello.o hello.c
Error:
/tmp/ccuNUbQK.o: In function `main':
main.c:(.text+0x5): undefined reference to `hello'
collect2: error: ld returned 1 exit status
make: *** [main.o] Error 1
Simply writing
gcc -o prog main.c hello.c
works.
您忘记告诉编译器在生成目标文件时只希望进行部分编译(即不进行链接)。
${CC} -c -o xxx.o xxx.c
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.