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
Have you tried -lstdc++ linker switch? How does your makefile look like?
Related
I tried to compile program with https://github.com/yhirose/cpp-httplib "one-header" library. I wrote
#include "httplib.h"
/* example code from main github page */
and when i tried to compile program
g++ -std=c++11 -o test test.cc
i got this error:
/usr/bin/ld: /tmp/ccYMj4l8.o: in function `std::thread::thread<httplib::ThreadPool::worker, , void>(httplib::ThreadPool::worker&&)':
test.cc:(.text._ZNSt6threadC2IN7httplib10ThreadPool6workerEJEvEEOT_DpOT0_[_ZNSt6threadC5IN7httplib10ThreadPool6workerEJEvEEOT_DpOT0_]+0x2f): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
What can i do?
And how to link libraries that have include and src directories, e.g. libcurl
It's gcc's known particular feature, its std::thread implementation is built upon pthreads so it requires specifying -pthread to correctly link programs with threads.
I have 4 files:
main.cpp
emcd.h
EMCB.dll
EMCB.lib
I want to use the functions that are in the dll.
There is not a lot of code, but it captures the essence.
#pragma comment(lib,"EMCB.lib")
#include "emcb.h"
int main()
{
EMCBLibInitialize(); //DLL function
}
To compile, I use gcc and do it with the following command
gcc main.cpp -l: EMCB.lib
after which I get an error
/usr/bin/ld: cannot find -l:EMCB.lib
collect2: error: ld returned 1 exit status
The preprocessor statement:
#pragma comment(lib,"xxx.lib")
will not be recognized by the GCC compiler. You need to use -l
I am trying to call cplus_demangle() function from libiberty.a, but I am getting "undefined reference to 'cplus_demangle' error.
This is my simple main:
extern "C" char *cplus_demangle(const char *, int);
int main() {
cplus_demangle("a", 0);
}
and my build command (libiberty is in ../../install/lib64):
g++ -L../../install/lib64 -liberty main.cpp -o main
If I rename libiberty.a to something else I'd get "cannot find -liberty" error. So, I assume the linker sees the library. And I can see cplus_demangle in libiberty.a:
$> nm -C --defined-only libiberty.a | grep "cplus_demangle"
0000000000002230 T cplus_demangle
...
But I am getting this error when I compile:
main.cpp:(.text+0xf): undefined reference to `cplus_demangle'
collect2: ld returned 1 exit status
make: *** [main] Error 1
I'd appreciate your help. Thanks!
Move the -liberty to the end of the command line. The library should be searched after main.o is created. Order matters.
I'm trying access an extern device via a serial port and want to use Boost.Asio for this propose.
I have build the boost libraries for MinGw and compiled the regex example successful.
But I have problems to compile my code if I include something from Boost.Asio:
#include <boost/asio/serial_port.hpp>
int main() {
return 0;
}
g++ -D _WIN32_WINNT=0x0501 -O0 -g3 -Wall -c -fmessage-length=0 -osrc\SerialPortTest.o ..\src\SerialPortTest.cpp
g++ -LC:\boost-libs\boost\bin.v2\libs\thread\build\gcc-mingw-4.5.2\release\link-static\threading-multi -LC:\boost-libs\boost\bin.v2\libs\system\build\gcc-mingw-4.5.2\release\link-static\threading-multi -oSerialPortTest.exe src\SerialPortTest.o -lboost_thread-mgw45-mt-1_48 -lboost_system-mgw45-mt-1_48
src\SerialPortTest.o: In function `ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh':
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../include/boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to `WSAStartup#8'
src\SerialPortTest.o: In function `ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE':
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../include/boost/asio/detail/impl/winsock_init.ipp:48: undefined reference to `WSACleanup#0'
collect2: ld returned 1 exit status
For me it seems to be a linking problem, but I don't get it.
Add -lws2_32 flag to link against WinSockets library.
Also, this might be useful: MinGW linker error: winsock
You miss wsock32 library. Add this to your dependencies and it should work.
I'm currently trying to compile GIZA++ which I downloaded from http://code.google.com/p/giza-pp/downloads/detail?name=giza-pp-v1.0.5.tar.gz. But everytime I run the make command, the following error always happened:
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lc
collect2: ld returned 1 exit status
make: *** [GIZA++] Error 1
I really don't know what is wrong, can anyone help me?
I'm using Fedora 14 with g++ version 4.5.1
Thanks for your help
The linker is saying it cannot find the C++ libraries. This suggests that there is something wrong with your GCC installation. Can you post the command line that you use to compile your code. also, can you try compiling this:
#include <iostream>
using namespace std;
int main() {
cout << "hello" << endl;
}
Save it into a file called hello.cpp, and then compile it:
g++ hello.cpp
and tell us what errors you get if any.