【问题标题】:Makefile fails on ubuntu 14.04 - undefined referenceMakefile 在 ubuntu 14.04 上失败 - 未定义的参考
【发布时间】:2015-10-24 14:07:36
【问题描述】:

我无法让这个 Makefile 工作。这是最简单的例子。下面是代码和错误:

main.c

#include <stdio.h>
#include <stdlib.h>
#include "hello.h"

int main(){
    hello();
    return EXIT_SUCCESS;
}

你好.c

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

void hello(void){
    printf("Hello World\n");
}

你好.h

#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

有效。

【问题讨论】:

    标签: c linux ubuntu gcc makefile


    【解决方案1】:

    您忘记告诉编译器您在生成目标文件时只需要部分编译(即不链接)。

        ${CC} -c -o xxx.o xxx.c
    

    【讨论】:

    • 谢谢,它现在可以工作了:) @Ignacio Vazquez-Abrams 这是我第一次这样做。我最终会了解它。
    猜你喜欢
    • 2018-09-22
    • 1970-01-01
    • 2023-03-31
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多