Maxim
Maxim

Reputation: 1239

#including <iostream> breaks linkage of shared object

I have following code:

#include <iostream>

extern "C" {
    void foo() {
        std::cout << "Wow, It's working!" << std::endl;
    }
}

Without inclusion of iostream and printing the library links correctly. But when I include and try to print, compiler gives me following errors:

Compiling: main.cpp
Linking dynamic library: libfoo.so
/usr/bin/ld: obj/main.o: перемещение R_X86_64_32 для «.rodata» не может использоваться при создании общего объекта; перекомпилируйте с -fPIC
obj/main.o: could not read symbols: Некорректное значение
collect2: ошибка: выполнение ld завершилось с кодом возврата 1
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings

Sorry for the errors in Russian, but I have no exact translation into English. Maybe this translation will help:

Compiling: main.cpp
Linking dynamic library: libfoo.so
/usr/bin/ld: obj/main.o: relocation R_X86_64_32 against ".rodata" can not be used when making shared object; recompile with -fPIC
obj/main.o: could not read symbols: bad value
collect2: error: ld terminated with exit code 1
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings

How I must include and compile to get it working?

P.S. The grammar can be very bad: English is not my native language

Upvotes: 0

Views: 611

Answers (1)

Csaba Toth
Csaba Toth

Reputation: 10729

Have you tried -lstdc++ linker switch? How does your makefile look like?

Upvotes: 1

Related Questions