繁体   English   中英

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

[英]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.

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