Compile C++ project statically on redhat - c++

I'm trying to compile statically my C++ project on Redhat 7.3.
This is my script:
g++ -static -ldl -std=c++0x -O3 -fopenmp *.cpp -o main
But I obtain this error: /usr/bin/ld: cannot find -ldl
How can I fix it?
I think a library misses.

-ldl means /usr/lib/libdl.so
( Provided by glibc-devel )
the -static -ldl means /usr/lib/libdl.a
( Provided by glibc-static )
yum install glibc-devel glibc-static

Related

g++ can't find ncurses.h despite it being installed

I'm trying to experiment around in ncurses for the first time, but I'm having problems compiling my source code. As far as I can tell, ncurses is installed and in the proper directories.
My makefile is super simple:
.cpp :
g++ -Wall -g -o $* $*.cpp -std=c++11 -lncurses
and here's my output when I try to locate ncurses.h
$ locate ncurses.h
/usr/include/ncursesw/ncurses.h
and when I check to see if it's installed
$ dpkg -l | grep ncurses
ii libncurses5:amd64 5.9+20140118-1ubuntu1 amd64 shared libraries for terminal handling
ii libncursesw5:amd64 5.9+20140118-1ubuntu1 amd64 shared libraries for terminal handling (wide character support)
ii libncursesw5-dev:amd64 5.9+20140118-1ubuntu1 amd64 developer's libraries for ncursesw
ii mtr-tiny 0.85-2 amd64 Full screen ncurses traceroute tool
ii ncurses-base 5.9+20140118-1ubuntu1 all basic terminal type definitions
ii ncurses-bin 5.9+20140118-1ubuntu1 amd64 terminal-related programs and man pages
ii ncurses-term 5.9+20140118-1ubuntu1 all additional terminal type definitions
But g++ tells me this when I try to make
bankacct.cpp:18:29: fatal error: ncurses.h: No such file or directory
compilation terminated.
Unfortunately, I've not got root access and I need to be able to compile on this machine. What are my options?
I've tried including <ncursesw/ncurses.h> based on suggestions from other users, but now g++ is giving me this error:
$ make bankacct
g++ -Wall -g -o bankacct bankacct.cpp -std=c++11 -lncurses
/usr/bin/ld: cannot find -lncurses
and if I try removing -lncurses it gives me this:
$ make bankacct
g++ -Wall -g -o bankacct bankacct.cpp -std=c++11
/tmp/cc8rPQfK.o: In function `main':
bankacct.cpp:23: undefined reference to `initscr'
Now I've tried linking the libraries. Here's what I did:
$ locate libncurse
/lib/x86_64-linux-gnu/libncurses.so.5
/lib/x86_64-linux-gnu/libncurses.so.5.9
/lib/x86_64-linux-gnu/libncursesw.so.5
/lib/x86_64-linux-gnu/libncursesw.so.5.9
/usr/lib/x86_64-linux-gnu/libncurses++w.a
/usr/lib/x86_64-linux-gnu/libncursesw.a
/usr/lib/x86_64-linux-gnu/libncursesw.so
/usr/share/doc/libncurses5
/usr/share/doc/libncursesw5
/usr/share/doc/libncursesw5-dev
/var/lib/dpkg/info/libncurses5:amd64.list
/var/lib/dpkg/info/libncurses5:amd64.md5sums
/var/lib/dpkg/info/libncurses5:amd64.postinst
/var/lib/dpkg/info/libncurses5:amd64.postrm
/var/lib/dpkg/info/libncurses5:amd64.shlibs
/var/lib/dpkg/info/libncurses5:amd64.symbols
/var/lib/dpkg/info/libncursesw5-dev:amd64.list
/var/lib/dpkg/info/libncursesw5-dev:amd64.md5sums
/var/lib/dpkg/info/libncursesw5-dev:amd64.postinst
/var/lib/dpkg/info/libncursesw5:amd64.list
/var/lib/dpkg/info/libncursesw5:amd64.md5sums
/var/lib/dpkg/info/libncursesw5:amd64.postinst
/var/lib/dpkg/info/libncursesw5:amd64.postrm
/var/lib/dpkg/info/libncursesw5:amd64.shlibs
/var/lib/dpkg/info/libncursesw5:amd64.symbols
So then I tried two variations of my makefile:
g++ -Wall -g -L/usr/lib/x86_64-linux-gnu/ -o $* $*.cpp -std=c++11 -lncurses
and
g++ -Wall -g -L/lib/x86_64-linux-gnu/ -o $* $*.cpp -std=c++11 -lncurses
which still gave me the errors undefined reference to 'initscr' (without -lncurses) or /usr/bin/ld: cannot find -lncurses (with it)
-lncurses
tells the linker to look for a library called "ncurses.". You clearly indicate that's not what your library is called:
/usr/lib/x86_64-linux-gnu/libncursesw.a
You need
-lncursesw
You don't need to modify the source code to specify <ncursesw/ncurses.h> you can simply add
-I/usr/include/ncursesw

Linking CUDA + plain C++ code: undefined reference to `__fatbinwrap_66_tmpxft_ etc

Somehow my CUDA binary build process has been messed up. All of the .cu files compile nicely to .o files, but when I try to link, I get:
CMakeFiles/tester.dir/tester_intermediate_link.o: In function `__cudaRegisterLinkedBinary_66_tmpxft_00007a5f_00000000_16_cuda_device_runtime_compute_52_cpp1_ii_8b1a5d37':
/tmp/tmpxft_00006b54_00000000-2_tester_intermediate_link.reg.c:7: undefined reference to `__fatbinwrap_66_tmpxft_00007a5f_00000000_16_cuda_device_runtime_compute_52_cpp1_ii_8b1a5d37'
Now, I have not used compute_52 anywhere. My nvcc command-line is:
/usr/local/cuda/bin/nvcc -M -D__CUDACC__ /home/joeuser/src/my_project/src/kernel_specific/elementwise/Add.cu -o /home/joeuser/src/my_project/CMakeFiles/tester.dir/src/kernel_specific/elementwise/tester_generated_Add.cu.o.NVCC-depend -ccbin /usr/bin/gcc-4.9.3 -m64 --std c++11 -D__STRICT_ANSI__ -Xcompiler ,\"-Wall\",\"-g\",\"-g\",\"-O0\" -gencode arch=compute_35,code=compute_35 -g -G --generate-line-info -DNVCC -I/usr/local/cuda/include -I/opt/cub -I/usr/local/cuda/include
and my link line is:
/usr/bin/g++-4.9.3 -Wall -std=c++11 -g some.o files.o here.o blah.o blahblah.o bar.cu.o baz.cu.o -o bin/myapp -rdynamic -Wl,-Bstatic -lcudart_static -Wl,-Bdynamic -lpthread -lrt -ldl /usr/lib/libboost_system.so /usr/lib/libboost_program_options.so -Wl,-Bstatic -lcudart_static -Wl,-Bdynamic -lpthread -lrt -ldl /usr/local/cuda/extras/CUPTI/lib64/libcupti.so -lnvToolsExt -lOpenCL /usr/lib/libboost_system.so /usr/lib/libboost_program_options.so /usr/local/cuda/extras/CUPTI/lib64/libcupti.so -lnvToolsExt -lOpenCL -Wl,-rpath,/usr/lib:/usr/local/cuda/extras/CUPTI/lib64
I'll note I have separate compilation enabled, and do not seem to have skipped my intermediate link phase.
Why is this happening?
CUDA has two compilation modes, relocatable and static.
The relocatable mode is required for some configurations-which we will not get into now.
If you want to compile in relocatable mode -rdc=true, you'll need the Cuda device runtime library.
Which is located in the file cudadevrt.lib.
On some instances, supplying -lcudadevrt as a command line switch to the CUDA linker does the job, but on e.g. MSVC, you'll also need to specify cudadebrt.lib as a link dependency.
Well, I'm not sure why I'm seeing missing references to Compute 5.2 calls, but adding -lcudadevrt to the end of the link command makes the error go away.

mingw g++ is unable to link with libraries

I'm trying to use X86_64-w64-mingw32-g++ (packaged in Archlinux's MingW package) to cross compile some C++ code into an Windows executable, however I'm having trouble getting past some issues.
I'm calling
x86_64-w64-mingw32-g++ -o build_win/asm build_win/asm.o build_win/asm_lib.o build_win/socket_boost.o -I../extra/etc -fopenmp -lrt -std=c++11 -g -lboost_system -lboost_serialization
from a makefile, but I get thrown the errors:
/usr/lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lrt
/usr/lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lboost_system
/usr/lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lboost_serialization
This works fine with native g++, so exactly do I have to change for mingw to compile?
EDIT: I have mingw-w64-boost package installed, which includes boost libraries pre-compiled and ready to be linked. However, it seems the naming convention is a bit different, and -lboost_system for example becomes -llibboost_system-mt (not exactly sure what the -mt suffix entails).
Problem is I can't find the mingw counterpart for -lrt. I've tried with both -lrtm and -lrtutils but in both cases I get:
[...]
undefined reference to `__imp_getsockopt'
Are you sure that -lboost_system and other libraries are present in the same directory as makefile ?
If not then please include -L flag which indicates the location of your library.
For example:
-L /path_openmp -fopenmp -L /path_boost_system/ -lboost_system -L /path_serialization -lboost_serialization
Moreover, you need not include -I and -g flag when creating an executable from .o files. These are needed when you create .o from .cpp files.
There is no rt library on Windows.
You are missing -lws2_32.
$ x86_64-w64-mingw32-nm -A /usr/x86_64-w64-mingw32/lib/*.a 2>/dev/null | grep getsockopt | grep " T "

Error when run LD_PRELOAD with boost

I compiled LD_PRELOAD which uses boost (locks.hpp). Compile was successfull. I copied this LD_PRELOAD to other linux server, and when i run, error:
/usr/bin/java: symbol lookup error: /test/test.so: undefined symbol:
_ZN5boost11this_thread20disable_interruptionC1Ev
How can i fix this? Can i avoid this problem without installing boost on this server?
How i compile LD_PRELOAD:
g++ -fPIC -m32 -shared -Wl,-soname,test.so -ldl -o test.so test.cpp
Thanks!
It seems you have to get libboost_thread into your test.so file. Something along the lines of:
g++ -fPIC -m32 -shared -Wl,-soname,test.so -ldl -o test.so test.cpp \
/usr/lib/libboost_thread.a -lpthread
Since I wouldn't know the specifics for your system, the boost library might be in a different place than from mine.

OpenGL/SDL problem in Ubuntu

I have a C-code which I have not managed to run
http://dl.getdropbox.com/u/175564/problem-sdl.png
The problem is in OpenGL or SDL.
I do not have SDL.h at /usr/local/SDL/SDL.h, so gcc cannot find it.
I have SDL.h installed by MacPorts at /opt/local/include/SDL/SDL.h.
I tried to copy it to /Masi/local/SDL/SDL.h unsuccessfully at the folder by
cp /opt/local/include/SDL/SDL.h /
and by
cp /opt/local/include/SDL/SDL.h /Masi/local/SDL/
I tried to solve the problem by creationg a symlink by
$ln -s /opt/local/include/SDL/SDL.h /Masi/local/SDL/SDL.h
the simplest way to get all the compiler flags for SDL is by using sdl-config:
gcc sdl_gl_1.c $(sdl-config --cflags --libs) -lGL -lGLU
No, Ubuntu does not have them by default (at least the development versions). For my own little program I just installed libsdl1.2-dev and mesa-common-dev (OpenGL).
For the build process I use scons which produces the following commands:
gcc -o src/geom.o -c -Wall -ansi src/geom.c
gcc -o src/main.o -c -Wall -ansi src/main.c
gcc -o test src/main.o src/geom.o -lSDL -lGL
If you install the libraries in some non-standard location, you might have to specify your own include (-I) and library (-L) paths.