[英]Makefile fails on ubuntu 14.04 - undefined reference
我無法使此Makefile正常工作。 這是最簡單的例子。 下面是代碼和錯誤:
main.c
#include <stdio.h>
#include <stdlib.h>
#include "hello.h"
int main(){
hello();
return EXIT_SUCCESS;
}
你好ç
#include<stdio.h>
#include "hello.h"
void hello(void){
printf("Hello World\n");
}
你好
#ifndef _HELLO_H
#define _HELLO_H
void hello(void);
#endif
生成文件
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
錯誤:
/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
簡單地寫
gcc -o prog main.c hello.c
作品。
您忘記告訴編譯器在生成目標文件時只希望進行部分編譯(即不進行鏈接)。
${CC} -c -o xxx.o xxx.c
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.