ted0623
2015-09-18 07:21
采纳率: 25%
浏览 2.5k
已采纳

g++编译报错,正在学习makefile

 HelloWorld: main.o print.o
    g++ -o HelloWorld print.o main.o

main.o: main.cpp print.h print.o
    g++ -o main.o main.cpp

print.o: print.cpp print.h
    g++ -o print.o print.cpp

//main.cpp
#include <iostream>
#include "print.h"

using namespace std;

int main()
{
    print();
    return 0;
}

//print.cpp
#include <iostream>
#include "print.h"
using namespace std;

void print()
{
    cout << "Hello World" << endl;
    return;
}

//print.h
 #include <iostream>
using namespace std;

void print();

报错为:

meng@meng-virtual-machine:~/code-linux/nachos-3.4/code$ make HelloWorld
g++ -o print.o print.cpp
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 21 has invalid symbol index 22
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o:在函数‘_start’中:
(.text+0x18):对‘main’未定义的引用
collect2: error: ld returned 1 exit status
make: *** [print.o] 错误 1

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • 斝樂 2015-09-18 08:10
    已采纳

    -c
      只激活预处理,编译,和汇编,也就是他只把程序做成obj文件,就是.o文件
    -o
      制定目标名称,就是生成可执行文件

    点赞 打赏 评论
  • oyljerry 2015-09-18 07:43
     g++ -o -shared HelloWorld print.o main.o
    
    点赞 打赏 评论
  • 斝樂 2015-09-18 07:58

    吧makefile文件改成这样试试:
    HelloWorld: main.o print.o
    g++ -o HelloWorld print.o main.o

    main.o: main.cpp print.o
    g++ -c main.cpp

    print.o: print.cpp
    g++ -c print.cpp

    点赞 打赏 评论
  • _小白鱼儿_ 2017-12-28 09:23

    你的这种Makefile方法不好 一帮用CC变量去定义编译

    点赞 打赏 评论

相关推荐 更多相似问题