Search code examples
c++g++linker-errorsld

ld: Invalid version 3 (max 0) - error adding symbols: Bad value


I'm using g++ 9.2.1 and binutils 2.34. I'm running Manjaro 19.0.2 with linux kernel 5.4.23-1. Everything comes out of a fresh install of Manjaro that I just did, thinking it would solve the problem.

While compiling the following super simple program:

#include <iostream>

int main()
{
    std::cout << "Hello" << std::endl;

    return 0;
}

If I run g++ main.cpp -o program, everything's fine and I get Hello when executing ./program.

However, when I run g++ main.cpp -o main.o, it works, but then when I run g++ main.o -o program, I get the following errors:

/usr/bin/ld: main.o: _ZSt4cout: invalid version 3 (max 0)
/usr/bin/ld : main.o : error adding symbols: bad value
collect2: error: ld return 1 exit status

I have absolutely no idea where it could come from.


Solution

  • g++ main.cpp -o main.o does not produce a relocatable object file. The output is an executable file instead. Such a file cannot be used for further linking.

    To produce an object file, use g++ -c main.cpp -o main.o instead. The -c flag instructs GCC not to link the final executable.