Strange behavior with c++11 thread in gcc 4.8.1 - linux

Here's my code.
#include <thread>
#include <iostream>
//#include <ctime>
using namespace std;
void f() {}
int main() {
//struct timespec t;
//clock_gettime(CLOCK_REALTIME, &t);
thread(f).join();
return 0;
}
I have tried to compile this program in the following ways:
1. g++ a.cc -o a -std=c++11 -pthread
2. g++ a.cc -o a -std=c++11 -lpthread
3. g++ -pthread a.cc -o a -std=c++11
4. g++ a.cc -c -std=c++11 && g++ a.o -o a -lpthread
5. g++ a.cc -c -std=c++11 -pthread && g++ a.o -o a -pthread
All of them can output the executable "a". However, none of them seem to link the library "libpthread.so":
./a
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
Aborted
ldd ./a
linux-vdso.so.1 => (0x00007fff997fe000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f350f7b5000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f350f59f000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f350f1d6000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f350eed2000)
/lib64/ld-linux-x86-64.so.2 (0x00007f350fad4000)
But, the following way works perfectly:
uncomment all the three lines in the above code(must do this, or the same error appears);
using "g++ a.cc -std=c++11 -o a -lrt".
I'm using ubuntu 13.10. Any reason for this?

This would seem to be a known Ubuntu related bug.
You can (as noted on the linked page) work around it by passing -Wl,--no-as-needed on the command line;
> g++ a.cc -pthread -std=c++11
> ./a.out
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
>
> g++ a.cc -pthread -std=c++11 -Wl,--no-as-needed
> ./a.out
>

Related

How to build C program with b2 without libstdc++ dependency?

I have a simple C program and such jamroot.jam:
exe hello : hello.c ;
I can run b2 -d+2:
........
gcc.compile.c bin/gcc-8.3.0/debug/hello.o
"g++" -x c -fPIC -O0 -fno-inline -Wall -g -c -o "bin/gcc-8.3.0/debug/hello.o" "hello.c"
gcc.link bin/gcc-8.3.0/debug/hello
"g++" -o "bin/gcc-8.3.0/debug/hello" -Wl,--start-group "bin/gcc-8.3.0/debug/hello.o" -Wl,-Bstatic -Wl,-Bdynamic -Wl,--end-group -fPIC -g
........
After this I receive hello binary which depends on libstdc++:
$ ldd bin/gcc-8.3.0/debug/hello
linux-vdso.so.1 (0x00007fffdaf5b000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007ff6d8176000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007ff6d7ff3000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007ff6d7fd9000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff6d7e18000)
/lib64/ld-linux-x86-64.so.2 (0x00007ff6d8329000)
If I build it with gcc I get much less dependencies:
$ gcc hello.c -o hello
$ ldd ./hello
linux-vdso.so.1 (0x00007ffc661cc000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fca20243000)
/lib64/ld-linux-x86-64.so.2 (0x00007fca20433000)
Can I do this with b2?
I have a project which includes C and C++ programs and I would not like to have different build system for C binaries.
Try using g++, it works for c++ and c it should work for compiling, when you're done compiling run ./filename

ldd on the binary not showing my shared library

I link a shared library on the command line while building an executable. Running ldd on that executable is not showing the linked shared library.
After looking at some of the output of the linker, I have even tried adding the -Wl,--no-as-needed option and that didn't help either.
foo.c:
#include <stdio.h>
void foo () {
printf ("Hello world\n");
}
main.c:
#include <stdio.h>
int main () {
printf ("In main \n");
foo ();
}
Here's the command I used to compile and link:
$ gcc -Wl,--no-as-needed main.c -o main -L./ -lfoo
/bin/ld: cannot find -lfoo
collect2: error: ld returned 1 exit status
$ gcc -c foo.c -shared -Wl,-soname,libfoo.so -o libfoo.so
$ ls -l libfoo.so
-rw-r--r-- 1 apple eng 1488 Jun 4 04:44 libfoo.so
$ gcc -Wl,--no-as-needed main.c -o main -L./ -lfoo
$ ldd main
linux-vdso.so.1 => (0x00007fffbdd6c000)
libc.so.6 => /lib64/libc.so.6 (0x00007f6367e23000)
/lib64/ld-linux-x86-64.so.2 (0x00005556e268a000)
libfoo.so does not show up above.
$ objdump -x main | grep NEEDED
NEEDED libc.so.6
Why isn't libfoo.so showing up as NEEDED?
By using the gcc option -c you're telling it to create only object from foo.c, so the only product you're getting from this gcc command is an object file. The fact that its suffix is .so is only because you forced it using -o option. If you run this command without -o you'd see that the output is just foo.o - an object file.
If you omit the -c from the gcc command you'd get the shared object you wanted.
Running file on the output file shows the difference (note that I'm not setting the output name using -o and letting gcc use its default names):
With -c:
> gcc -c foo.c -shared
> file foo.o
foo.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
Without -c:
> gcc foo.c -shared
> file a.out
a.out: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=a63581bfc45f845c501ffb6635, not stripped
^^^
|||
This command does not generate a shared library:
gcc -c foo.c -shared -Wl,-soname,libfoo.so -o libfoo.so
It generates an object file libfoo.so that is later statically linked with your code. Proof: remove the lib file, the program will still run.
Solution:
Compile the object file separately, then convert it into a shared library. You will have to tell the loader where to search for the shared libraries by setting LD_LIBRARY_PATH:
gcc -c foo.c
gcc foo.o -shared -o libfoo.so
gcc main.c -o main -L./ -lfoo
export LD_LIBRARY_PATH=`pwd`
ldd ./main
# linux-vdso.so.1 (0x00007ffe0f7cb000)
# libfoo.so => /home/---/tmp/libfoo.so (0x00007f9bab6ec000)
# libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9bab2fb000)
# /lib64/ld-linux-x86-64.so.2 (0x00007f9babaf0000)

cygwin : Linker doesn't find shared library

I am creating an exe and shared library in cygwin.
The library is created and is in the proper place, but when I try to compile the client code daemon, the linking phase says that it can't find the sysutil library.
Error is posted below:
/usr/lib/gcc/i686-pc-cygwin/5.4.0/../../../../i686-pc-cygwin/bin/ld: cannot find -lsysutil
collect2: error: ld returned 1 exit status
make: *** [Makefile:84: daemon] Error 1
I tried exporting the path using LD_LIBRARY_PATH but unfortunately that also didn't help.
daemon.c
#include <stdio.h>
#include <sys_util.h>
int main(){
sys_util();
while(1){
}
return 0;
}
sysutil.c
#include <stdio.h>
#include "sys_util.h"
int sys_util(){
return 0;
}
sysutil.h
int sys_util();
test.bat
g++ -fpic -c sysutil.c
g++ -shared -o libsysutil.so sysutil.o -I.
g++ -c daemon.c -I.
g++ -o daemon.exe daemon.o -L. -lsysutil
del *.o
Shared library is generated sysutil.so in the folder c:/test same as the source code (daemon.c,sysutil.c,sys_util.h,test.bat,libsysutil.so)
Cygwin console output:
/cygdrive/c/test
$ ./test.bat
C:\test>g++ -fpic -c sysutil.c
sysutil.c:1:0: warning: -fpic ignored for target (all code is position independent)
C:\test>g++ -shared -o libsysutil.so sysutil.o -I.
C:\test>g++ -c daemon.c -I.
C:\test>g++ -o daemon.exe daemon.o -L. -lsysutil
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -lsysutil
collect2.exe: error: ld returned 1 exit status
C:\test>del *.o
Cygwin expects shared libraries to have the .dll extension.
Change the second line in your batch file to:
g++ -shared -o sysutil.dll sysutil.o -I.
See the users' guide for more info:
https://cygwin.com/cygwin-ug-net/dll.html

Cross Compilation error can't load library 'libc.so.6'

I am trying to implement a dynamic library(liblog.so) which is going to run on i386 controller.
when i compile that in Host Machine (Ubuntu Machine) it compiles and generates the .so file successfully.
liblog.so is the the file which is put under /usr/lib in target machine.
merom#arunkumar:~/freedcs/freedcs-code1/Controller/src/Controller$ ldd log_client
linux-gate.so.1 => (0xb7707000)
libnative.so.3 => not found
libxenomai.so.0 => not found
libpthread.so.0 => /lib/i386-linux-gnu/libpthread.so.0 (0xb76cd000)
librt.so.0 => not found
liblog.so => /usr/lib/liblog.so (0xb76c9000)
libcrypto.so.1.0.0 => /lib/i386-linux-gnu/libcrypto.so.1.0.0 (0xb751e000)
libssl.so.1.0.0 => /lib/i386-linux-gnu/libssl.so.1.0.0 (0xb74c7000)
libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0xb74a9000)
libc.so.0 => not found
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb72f5000)
/lib/ld-uClibc.so.0 => /lib/ld-linux.so.2 (0xb7708000)
libdl.so.2 => /lib/i386-linux-gnu/libdl.so.2 (0xb72f0000)
libz.so.1 => /lib/i386-linux-gnu/libz.so.1 (0xb72d7000)
merom#arunkumar:~/freedcs/freedcs-code1/Controller/src/Controller$
output at host machine.
Output at target machine:
libnative.so.3 => /usr/lib/libnative.so.3 (0xb78d9000)
libxenomai.so.0 => /usr/lib/libxenomai.so.0 (0xb78d4000)
libpthread.so.0 => /lib/libpthread.so.0 (0xb78c1000)
librt.so.0 => /lib/librt.so.0 (0xb78bd000)
liblog.so => /lib/liblog.so (0xb78b9000)
libcrypto.so.1.0.0 => /usr/lib/libcrypto.so.1.0.0 (0xb7793000)
libssl.so.1.0.0 => /usr/lib/libssl.so.1.0.0 (0xb7752000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7748000)
libc.so.0 => /lib/libc.so.0 (0xb770a000)
libdl.so.0 => /lib/libdl.so.0 (0xb7706000)
libc.so.6 => not found
ld-uClibc.so.0 => /lib/ld-uClibc.so.0 (0xb78e2000)
when i send it to Target machine(Currently in VMware), when i run the .\log_client it shows can't load library 'libc.so.6'
Yes i compiled log_client with linked with liblog.so
Compilation Commands
Generating liblog.so
../../../../build/i386/buildroot-2011.11/output/host/usr/bin/i686-unknown-linux-uclibc-gcc -c log.c -o liblog.o
../../../../build/i386/buildroot-2011.11/output/host/usr/bin/i686-unknown-linux-uclibc-gcc -Wcast-align -g -W -Wall -L../../../../build/i386/buildroot-2011.11/output/target/usr/lib -lxenomai -lpthread -lrt -shared -o liblog.so liblog.o -rdynamic -lcrypto -lssl
Generating log_client
cp liblog.so ../../../../build/i386/buildroot-2011.11/output/target/usr/lib
../../../../build/i386/buildroot-2011.11/output/host/usr/bin/i686-unknown-linux-uclibc-gcc -Wcast-align -g -W -Wall -c log_client.c
../../../../build/i386/buildroot-2011.11/output/host/usr/bin/i686-unknown-linux-uclibc-gcc -Wcast-align -g -W -Wall -L../../../../build/i386/buildroot-2011.11/output/target/usr/lib -lxenomai -lpthread -lrt -o log_client log_client.o -llog -rdynamic -lcrypto -lssl
I am using same procedure for an other code that is working, though that code doesn't use this customized library(liblog.so), there were warnings too but i didn't posted here.
Please help me, i am frustrated!!
------------Edited----------
file Output
$ file liblog.so
liblog.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, not stripped
$ file log_client
log_client: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
------------Update----------
Some one suggested me use --sysroot while compiling the files, but even that didn't helped me. Same error coming out. :(
I had put sysroot path to target machine's GCC's sysroot path
Generating liblog.so
../../../../build/i386/buildroot-2011.11/output/host/usr/bin/i686-unknown-linux-uclibc-gcc -Wcast-align -g -W -Wall -c log.c -o liblog.o --sysroot=/home/merom/freedcs/build/i386/buildroot-2011.11/output/host/usr/i686-unknown-linux-uclibc/sysroot/
../../../../build/i386/buildroot-2011.11/output/host/usr/bin/i686-unknown-linux-uclibc-gcc -Wcast-align -g -W -Wall -L../../../../build/i386/buildroot-2011.11/output/target/usr/lib -lxenomai -lpthread -lrt -shared -o liblog.so liblog.o -rdynamic -lcrypto -lssl --sysroot=/home/merom/freedcs/build/i386/buildroot-2011.11/output/host/usr/i686-unknown-linux-uclibc/sysroot/
Generating log_client
cp liblog.so ../../../../build/i386/buildroot-2011.11/output/target/usr/lib
../../../../build/i386/buildroot-2011.11/output/host/usr/bin/i686-unknown-linux-uclibc-gcc -Wcast-align -g -W -Wall -c log_client.c --sysroot=/home/merom/freedcs/build/i386/buildroot-2011.11/output/host/usr/i686-unknown-linux-uclibc/sysroot/
../../../../build/i386/buildroot-2011.11/output/host/usr/bin/i686-unknown-linux-uclibc-gcc -Wcast-align -g -W -Wall -L../../../../build/i386/buildroot-2011.11/output/target/usr/lib -lxenomai -lpthread -lrt -o log_client log_client.o -llog -rdynamic -lcrypto -lssl --sysroot=/home/merom/freedcs/build/i386/buildroot-2011.11/output/host/usr/i686-unknown-linux-uclibc/sysroot/
Update
After Running objdump This was the output.
objdump -x log_client | grep NEEDED
NEEDED libnative.so.3
NEEDED libxenomai.so.0
NEEDED libpthread.so.0
NEEDED librt.so.0
NEEDED liblog.so
NEEDED libcrypto.so.1.0.0
NEEDED libssl.so.1.0.0
NEEDED libgcc_s.so.1
NEEDED libc.so.0
objdump -x liblog.so | grep NEEDED
NEEDED libnative.so.3
NEEDED libxenomai.so.0
NEEDED libpthread.so.0
NEEDED librt.so.0
NEEDED libcrypto.so.1.0.0
NEEDED libssl.so.1.0.0
NEEDED libgcc_s.so.1
NEEDED libc.so.0
I had the same problem with an executable. Every tool I tried - ldd, objdump, readelf, strings - indicated depencency with libc.so.0, rather than .6. It turned out that one of its dependent libraries was depending on .6. So I went and fixed that and everything is working now.
The library had the wrong version because it was compiled using the wrong cross-compiler (I used it by mistake), so I recompiled and reuploaded it.

libgc seems to be missing (Installing Virtuoso-Opensource with Mono support)

I'm building Virtuoso Opensource after doing ./configure --enable_mono - works fine. Calling make then gives me an error, because libgc is not found.
make[2]: Entering directory `/usr/local/src/virtuoso-opensource/libsrc/Thread'
/bin/bash ../../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I../../libsrc/Dk -fno-strict-aliasing -O2 -DMONO_USE_EXC_TABLES -fexceptions -D_REENTRANT -D_GNU_SOURCE -Wall -DNDEBUG -DPOINTER_64 -I/usr/local/src/virtuoso-opensource/libsrc/Xml.new -DOPENSSL_NO_KRB5 -Dlinux -D_GNU_SOURCE -DFILE64 -D_LARGEFILE64_SOURCE -I../../binsrc/mono/mono-1.1.7/libgc/include -I../../libsrc -I../../libsrc/Dk -DNO_UDBC_SDK -DUSE_INCLUDED_LIBGC=1 -g -O2 -MT libthrp_gc_la-sched_pthread_gc.lo -MD -MP -MF .deps/libthrp_gc_la-sched_pthread_gc.Tpo -c -o libthrp_gc_la-sched_pthread_gc.lo `test -f 'sched_pthread_gc.c' || echo './'`sched_pthread_gc.c
libtool: compile: gcc -DHAVE_CONFIG_H -I. -I../../libsrc/Dk -fno-strict-aliasing -O2 -DMONO_USE_EXC_TABLES -fexceptions -D_REENTRANT -D_GNU_SOURCE -Wall -DNDEBUG -DPOINTER_64 -I/usr/local/src/virtuoso-opensource/libsrc/Xml.new -DOPENSSL_NO_KRB5 -Dlinux -D_GNU_SOURCE -DFILE64 -D_LARGEFILE64_SOURCE -I../../binsrc/mono/mono-1.1.7/libgc/include -I../../libsrc -I../../libsrc/Dk -DNO_UDBC_SDK -DUSE_INCLUDED_LIBGC=1 -g -O2 -MT libthrp_gc_la-sched_pthread_gc.lo -MD -MP -MF .deps/libthrp_gc_la-sched_pthread_gc.Tpo -c sched_pthread_gc.c -fPIC -DPIC -o .libs/libthrp_gc_la-sched_pthread_gc.o
sched_pthread_gc.c:41:16: fatal error: gc.h: No such file or directory
compilation terminated.
make[2]: *** [libthrp_gc_la-sched_pthread_gc.lo] Error 1
make[2]: Leaving directory `/usr/local/src/virtuoso-opensource/libsrc/Thread'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/usr/local/src/virtuoso-opensource/libsrc'
make: *** [all-recursive] Error 1
dellas#india672:/usr/local/src/virtuoso-opensource$
I just did sudo apt-get install libgc-dev which worked. Make still gives me the same error. I'm a Linux noob, can somebody give me advice what might be the problem here or how to solve it?
EDIT: FYI: ldconfig -v -p clearly says that libgc.so exists, so why is this make script looking for the header?
...
libgdbm_compat.so.3 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libgdbm_compat.so.3
libgdbm.so.3 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libgdbm.so.3
libgd.so.2 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libgd.so.2
libgcrypt.so.11 (libc6,x86-64) => /lib/x86_64-linux-gnu/libgcrypt.so.11
libgccpp.so.1 (libc6,x86-64) => /usr/lib/libgccpp.so.1
libgccpp.so (libc6,x86-64) => /usr/lib/libgccpp.so
libgcc_s.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libgcc_s.so.1
libgc.so.1 (libc6,x86-64) => /usr/lib/libgc.so.1
libgc.so (libc6,x86-64) => /usr/lib/libgc.so
libgamin-1.so.0 (libc6,x86-64) => /usr/lib/libgamin-1.so.0
libgailutil.so.18 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libgailutil.so.18
libfuse.so.2 (libc6,x86-64) => /lib/libfuse.so.2
...
You should check the contents that were provided by the libgc-dev package. There is generally a compatibility file in /usr/include called gc.h, who's only purpose is to #include <gc/gc.h>. Your distirbution may not have this file, which is causing the compilation problem.
To check the contents of libgc-dev, you do a:
dpkg-query -L libgc-dev
This will list all the files that were provided by this package. If it doesn't contain /usr/include/gc.h, then you may need to alter the #include to read #include <gc/gc.h>, or change the makefile to add: -I/usr/include/gc. I'd change the -I option as a last resort.
As an alternative, you can add a file in /usr/include called gc.h with the simple content:
/* This file is installed for backward compatibility. */
#include <gc/gc.h>
The Virtuoso mono hosting support is for version 1.x and will not work with the current 2.x and later releases for which support needs to be schedule for addition ...
Is is specifically mono runtime support you are seeking or maybe just the ADO.Net Provider for Mono, which can be built using:
cd binsrc/VirtuosoClient.Net
gmake -f Makefile.mono
Best Regards
Hugh Williams

Resources