undefined reference to `pthread_mutex_trylock' - c++

I have the following test program.
#include <iostream>
#include <cstdlib>
using namespace std;
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
int main(int argc, char *argv[])
{
int iret;
iret = pthread_mutex_trylock( & mymutex );
cout << "Test2 !!! " << endl;
pthread_mutex_unlock( & mymutex );
return EXIT_SUCCESS;
}
If I compile it without adding the pthread library I get the error for unresolved error for pthread_mutex_trylock , but only for function pthread_mutex_trylock.
If I replace pthread_mutex_trylock with pthread_mutex_trylock the program is compiled and runnig well also without the -lpthread* option.
If I add the -lpthraed option to the compile commands all runs well
this run well : $ g++ test2.c -o test2 -lpthread
this warn unresolved : $ g++ test2.c -o test2
Example error output:
$ g++ test2.c -o test2
/tmp/ccU1bBdU.o: In function main':
test2.c:(.text+0x11): undefined reference topthread_mutex_trylock'
collect2: ld returned 1 exit status
If I replace the instruction iret = pthread_mutex_trylock( & mymutex );
with iret = pthread_mutex_lock( & mymutex ); the program compiles and run without error also if didn't add the pthread libarry to the compile command
I know that is right to have the unresolved error if I didn't use the -lpthread option , but why I have not the same unresolved error also for other pthread_ function ?
I'm using gcc 4.4.2 on fedora 12
$ g++ --version
g++ (GCC) 4.4.2 20091222 (Red Hat 4.4.2-20)
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Do some have some suggestion about the meaning of this unreference only for pthread_mutex_trylock ?
thanks for the help, Enzo

If you use pthread functions you should link your object files with -lpthread and not worry about whether symbols are included in libc.
The rationale behind this is said to be such: some time ago the stubs in libc were used when application that used threads was run on a system without threading support. On such system, pthread_* functions became linked to libc stubs that returned errors showing that there's no threading functionality. While on "threaded" systems they were linked to pthread library and worked correctly.
Obviously, pthread_mutex_trylock function appeared after the policy changed to linking with -lpthread. So there's no stub for it.

You should perform both the compilation and linkage with the -pthread option, to be portable. On some systems, the compilation will have specific flags added (e.g., -D_REENTRANT) with -pthread specified.
If you're curious to see what -pthread will do to your compile and link flags, run gcc -dumpspecs.

Related

Undefined reference using Allegro 5 with mingw-w64

I'm trying to compile an Allegro 5 program on Windows 10 with mingw-w64.
I already had installed mingw-w64. Output from g++ --version is:
g++.exe (i686-posix-dwarf-rev2, Built by MinGW-W64 project) 7.1.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I downloaded windows binaries for Allegro 5 from https://github.com/liballeg/allegro5/releases (File: allegro-x86_64-w64-mingw32-gcc-8.2.1-posix-seh-static-5.2.5.0.zip) and unzipped the file into C:/allegro5 so now I have C:/allegro5/bin, C:/allegro5/include, C:/allegro5/lib.
A small test program:
#include <stdio.h>
#include <allegro5/allegro.h>
int main(int argc, char **argv)
{
al_init();
return 0;
}
And finally the command I run to compile: g++ test.cpp -I"C:/allegro5/include" -L"C:/allegro5/lib" -lallegro (There is a lib file called liballegro.dll.a under C:/allegro5/lib)
But there are some problems while linking:
C:\Users\xxxx\AppData\Local\Temp\ccg5z97Y.o:test.cpp:(.text+0x1e): undefined reference to `al_install_system'
collect2.exe: error: ld returned 1 exit status
A) What may be the reason for this ?
B) What should I do to compile in a static way ? Is changing -lallegro to -lallegro-static enough ?
This:
g++.exe (i686-posix-dwarf-rev2, Built by MinGW-W64 project) 7.1.0
is one of the the 32-bit GCC variants provided by MinGW-W64. You are attempting to link the
32-bit code it generates with the 64-bit libraries provided in:
allegro-x86_64-w64-mingw32-gcc-8.2.1-posix-seh-static-5.2.5.0.zip
which will not work. Replace your compiler with the appropriate 64-bit variant x86_64-posix-seh

Relocation R_X86_64_PC32 against undefined symbol can not be used when making a shared object; recompile with -fPIC

I recently upgraded gSOAP from 2.8.7 to 2.8.76. I had to make a few minor code adjustments for the upgrade, but after the upgrade the code won't link on the computer it would before.
I'm trying to use gSOAP to create a shared library on a computer which uses g++ 4.9.2. I've condensed the code down to create a test case that simplifies things a bit to try to identify where the failure is occurring.
gSOAP generates some ebaySoapLib* files when I run:
/usr/local/bin/soapcpp2 -z1 -C -w -x -n -pebaySoapLib -qebaySoapLib -I/usr/local/include/gsoap:/usr/local/share/gsoap:/usr/local/share/gsoap/import ebaySvc.h
The -z1 option is to keep things similar to how they were with gSOAP 2.8.7.
If I run:
g++ -fPIC -c ebaySoapLibClientLib.cpp
g++ -shared -fPIC -o test.so ebaySoapLibClientLib.o
I get the error:
/usr/bin/ld: ebaySoapLibClientLib.o: relocation R_X86_64_PC32 against
undefined symbol `soap_serializeheader' can not be used when making a
shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
I copied over the exact files to another computer running g++ 6.3.0 (not sure that the compiler version is what is mattering or now), and this compiles and links fine.
The file ebaySoapLibClientLib.cpp contains:
#define SOAP_FMAC3 static
#include "ebaySoapLibC.cpp"
#include "ebaySoapLibClient.cpp"
Now, if I remove the line:
#define SOAP_FMAC3 static
then the code compiles fine on both computers.
I'm at a loss for what I need to do differently to make it link OK with the g++ 4.9.2 computer. I could take out the #define so the functions aren't defined static and get it to work, but the question is WHY as gSOAP is putting it in there for a reason, and it links fine on the g++ 6.3.0 with these functions set to static.
You need to get library that supplies soap_serializeheader to be compile ready for shared library, its not ebaySoaLibClientLib.cpp. Use g++ -v to see what libraries g++ is using. Try using shared library instead of static ones.

Using SDL2 with g++ / MinGW crashes on launch

I am working on a game and recently made the transition from SDL 1.2 to SDL2 (kinda late to the party but heh). Despite having no compilation or linking error whatsoever, the program crashed on launch while stating (translated from French) "The application failed to start up correctly". At first I thought it was my own fault, but then I got suspicious and put together a quick SDL2 test, which indeed after flawless compilation crashed on launch.
Here is the code I've been using for this example (you can't make it more simple) :
#include <SDL.h>
int main(int argc, char *argv[])
{
return 0;
}
And the compilation line is g++ main.cpp -ISDL2-2.0.4\x86_64-w64-mingw32\include\SDL2 -LSDL2-2.0.4\x86_64-w64-mingw32\lib -lmingw32 -lSDL2main -lSDL2 -otest.exe -fpermissive -std=c++11 -Wno-write-strings -Wno-overflow
For what it's worth :
> g++ --version
g++ (x86_64-win32-seh, Built by MinGW-W64 project) 6.1.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I'm running MinGW-w64 on Windows, as you can see. The OS is Windows 8.1.
Welp. I actually used the wrong SDL2.dll. I feel dumb now.

linking error in boost asio

I am getting below linking error while compiling a simple piece of code using pthread.
The system i am working on is x86_64 (gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11) and am compiling as -
gcc -lpthread ~/temp/temp.cpp -lrt -L"/usr/lib/x86_64-redhat-linux5E/lib64/".
Its evident that the problem is with pthread library. So i have two questions.
Q1. is it fine to use x64 pthread libs with x86 code. (In this piece of code it doesnt matter but it does in my usage) (I guess its not but how might i differentiate between x64 and x86 libs?)
Q2. i see pthread and lrt both are present at /usr/lib but even if i give that path it says -lpthread not found.
What am i missing here? Thanks for your help.
Error :
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x12): undefined reference to `__libc_csu_fini'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x19): undefined reference to `__libc_csu_init'
/tmp/cc2GQOUf.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
Code :
#include <stdio.h>
#include <pthread.h>
int main()
{
pthread_t f1_thread;
return 0;
}
In compilation phase,compiler can use gcc or g++,but g++ will call gcc command automatically.But in link phase,linker can use g++ or gcc -lstdc++.Because the gcc command cannot link with the libraries that be used by c++ program automatically, so usually use g++to complete the link.
So the command g++ -g -lpthread ~/temp/temp.cpp -o temp should be ok.

Undefined reference to boost::random::random_device constructor and destructor on MinGW-w64 gcc

My OS is Windows 7 64-bit and C++ compiler I'm using is:
g++ (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 5.3.0
And I installed Boost version 1.60 using:
bootstrap.bat mingw
b2 install target=gcc
Then I tested is it working, using examples from Boost.Random tutorial.
With the first two everything was fine, but the third one gave linker errors about boost::random::random_device. I minimized the code to have only this:
// Compiled with:
// g++ -IC:/Boost/include/boost-1_60
// -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
// main.cpp
#include "boost/random/random_device.hpp"
int main() {
boost::random::random_device rng;
}
And I get the following errors:
C:\Users\Daniel\AppData\Local\Temp\cc5DfdjZ.o:main.cpp:(.text+0x15):
undefined reference to `boost::random::random_device::random_device()'
C:\Users\Daniel\AppData\Local\Temp\cc5DfdjZ.o:main.cpp:(.text+0x20):
undefined reference to `boost::random::random_device::~random_device()'
collect2.exe: error: ld returned 1 exit status
Here, on SO, I found that someone with similar problem added -lboost_system to flags, but for me it didn't helped.
Does anyone have any idea, why it isn't working? I checked, and I have random_device.hpp header in my Boost folder, with declarations of random_device() and ~random_device() in it.
I found what was wrong - the g++ command syntax, that I wanted to use to compile and link my code.
As I wrote in my question, I do this that way:
g++ -IC:/Boost/include/boost-1_60 -LC:/Boost/lib -lboost_random-mgw53-mt-1_60 main.cpp
While the correct one is with main.cpp (or any other source code file(s), that we want to include in compiling process) before the -L and -l flags.
For example:
g++ -IC:/Boost/include/boost-1_60 main.cpp -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
or even
g++ main.cpp -IC:/Boost/include/boost-1_60 -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
Hope it will help anyone, who will make such silly mistake too.