Jonas Daverio
Jonas Daverio

Reputation: 311

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.

Upvotes: 10

Views: 18519

Answers (2)

Llyor
Llyor

Reputation: 31

-o is used for saving a name for the compiled file, for example, g++ -o xyz xyz.cpp here xyz is name of compiled file . main.o changed the magic number of compiled file rendering error rather keep main only

g++ -o main main.cpp

this should create no problem

Upvotes: 3

Florian Weimer
Florian Weimer

Reputation: 33704

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.

Upvotes: 14

Related Questions