-4

A bit of the backstory: I first used Eclipse but it had trouble resolving namespace std so I switched to code::blocks and now I'm having this problem.

Everything went through debugging just fine except for one error: In function '_start': undefined reference to 'main' (.text+0x20). However, I have the main() defined and there's nothing fancy in it.

code::blocks version: 13.12 compiler: GNU gcc, have the g++ follow c++11

Thanks in advance.

There's nothing special in the main function

//: "main.cpp"
#include "bar.h"
#include "foo.h"
#include <iostream>
using namespace std;

int main() {
    int num;
    bar bar_;
    foo foo_;
    num = calc(bar_, foo_);
    cout << num << endl;
    return 0;
}

This is the Build Log:

g++  -o bin/Debug/test obj/Debug/bar.o obj/Debug/foo.o   
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))
Siphenx
  • 21
  • 2
  • 5

2 Answers2

2
  1. cout << << num << endl; is invalid (note the two repeated <<). This means your main.cpp shouldn't even compile.
  2. In your linking command, you're not linking main.o (the compiled object file for main.cpp). You're linking foo.o and bar.o, but you're missing main.o.

How you need to fix this:

  1. Fix your code in main.cpp
  2. Compile main.cpp to get the object file main.o
  3. Link all the object files together.
Cornstalks
  • 37,137
  • 18
  • 79
  • 144
  • The first one was a typo, fixed for 2 and 3, I directly used 'build' in code::blocks, how come it didn't link 'main.o'? – Siphenx Dec 29 '14 at 16:06
  • @Siphenx: for future reference, you should include your *exact* code in your questions (not a retype, but a copy 'n' paste). It's part of providing a good [MVCE](http://stackoverflow.com/help/mcve). – Cornstalks Dec 29 '14 at 16:08
  • Is `main.cpp` part of your project? It's been years since I've used Code::Blocks, so I'm not much help there. – Cornstalks Dec 29 '14 at 16:09
  • Yes, it's part of a small project. Also, regarding copy n paste code, is there a way to make an entire block a code at once rather than indent every line manually? – Siphenx Dec 29 '14 at 16:18
  • When you're typing a question/answer, there's a button that looks like this: **`{}`**. If you select your code and click that button, it will automatically add 4 spaces to the beginning of every line so it will be formatted as code. – Cornstalks Dec 29 '14 at 17:05
0

Hmmm..

As per code block website, when such error occure,

"Dont look at Build Message tab, look at Build log tab"

In my case: it was showing one .so file which is missing in linker.

Here is quick solution:

Setting>>compiler>>Linker Setting tab>>add path to .so file (which was pointed in build log)

Just ensure that the .so to add is really exist on your system.

Hope this helps.Cheers.

Hridaynath
  • 497
  • 5
  • 4