简体   繁体   English

Makefile在Ubuntu 14.04上失败-未定义参考

[英]Makefile fails on ubuntu 14.04 - undefined reference

I can't get this Makefile to work. 我无法使此Makefile正常工作。 This is the simplest example. 这是最简单的例子。 Below is the code and error: 下面是代码和错误:

main.c 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

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

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