【问题标题】:makefile give error undefined reference to 'main'makefile 给出错误未定义的“main”引用
【发布时间】:2013-09-06 14:27:20
【问题描述】:

生成文件:

all: a.out

a.out: b.o a.o 
    gcc -o b.o a.o

a.o: a.c 
    gcc -c a.c

b.o: b.c 
    gcc -c b.c

.PHONY:clean

clean:
    rm *.o a.out

make,提供信息:

错误:未定义对“main”的引用

collect2: ld 返回 1

make: * [a.out] 错误 1

但是当将源文件a.cb.c 放入Eclipse CDT 时,它编译得很好。 请解释一下我的makefile 有什么问题?

PS: 交流:

int global_1 = 100;

b.c:

extern int global_1;

int main()
{
    int global_2 = global_1 * 2;
    return 0;
}

【问题讨论】:

    标签: c makefile


    【解决方案1】:

    此规则未指定正确的结果文件:

    a.out: b.o a.o 
        gcc -o b.o a.o
    

    应该是

    a.out: b.o a.o 
        gcc -o "$@" b.o a.o
    

    这就是违反Paul's Rules of Makefiles 之一的结果。顺便说一句,“给出错误”的不是makefile,而是编译,特别是链接器。

    【讨论】:

      【解决方案2】:

      这个:

      a.out: b.o a.o 
          gcc -o b.o a.o
              \----/
                 |
                 |
           this says "name
           the output b.o"
      

      正在用输出文件覆盖b.o 目标文件。我很确定make 足够聪明,无需任何输入即可计算出所需的命令,因此请尝试删除第二行。

      【讨论】:

      • 抱歉,如您所说,删除第二行时,make 命令不会生成a.out 可执行文件。我使用的是 ubuntu 12.04 默认版本。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 2014-11-05
      • 2021-11-03
      相关资源
      最近更新 更多