make doesn't link math lib when compiling - c

I get the following error while compiling xpdf using make. I've tried using the command:
LIBS=-lm make
However, it doesn't work. I know the problem is that the c compiler cannot recognise the math symbols in the source code because the math library is not available to it, but I don't know how to fix it.
[ 71%] Linking CXX executable pdftohtml
/usr/bin/ld: CMakeFiles/xpdf_objs.dir/Gfx.cc.o: undefined reference to symbol 'acos##GLIBC_2.2.5'
//usr/lib64/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [xpdf/CMakeFiles/pdftohtml.dir/build.make:219: xpdf/pdftohtml] Error 1
make[1]: *** [CMakeFiles/Makefile2:428: xpdf/CMakeFiles/pdftohtml.dir/all] Error 2
make: *** [Makefile:130: all] Error 2
I have put my Makefile and makeLists.txt on pastebin for more information

I realised I had to use the g++ compiler instead of gcc after reading the man page for gcc which stated:
The usual way to run GCC is to run the executable called gcc, or
machine-gcc when cross-compiling, or machine-gcc-version to run a
specific
version of GCC. When you compile C++ programs, you should invoke GCC as g++ instead.

Related

CMake undefined and duplicate symbols when building executable. Fortran77 C mix

I have a fortran 90 program which calls Sundials CVODE via the FCVODE inteface. Sundials is written in C, but they provide two interfaces to fortran. One interface is for fortran 77 applications, and one uses modern fortran methods (iso_c_binding).
My code uses the fortran 77 interface (just the way it ended up, I know iso_c_binding would be better)
Here is my project (dev branch): https://github.com/Nicholaswogan/PhotochemPy/tree/dev
My goal is to write a CMakesLists.txt which compiles all the fortran and C in one swoop. I have done this, but it only works with some compiler combinations and opperating systems. For example, on MacOS with clang 12.0.5 and gfortran 11.1.0, everything works great. But, on MacOS, when I switch to gcc 11.1.0 (instead of clang), I get an error when linking my executable:
[ 98%] Building Fortran object CMakeFiles/photo.run.dir/src/main.f90.o
[100%] Linking Fortran executable photo.run
duplicate symbol '_F2C_CVODE_linsol' in:
lib/libsundials_fcvode.a(fsunlinsol_band.c.o)
lib/libsundials_fcvode.a(fcvnulllinsol.c.o)
duplicate symbol '_F2C_CVODE_matrix' in:
lib/libsundials_fcvode.a(fsunmatrix_band.c.o)
lib/libsundials_fcvode.a(fcvnullmatrix.c.o)
ld: 2 duplicate symbols for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [photo.run] Error 1
make[1]: *** [CMakeFiles/photo.run.dir/all] Error 2
make: *** [all] Error 2
I have looked at libsundials_fcvode.a and libsundials_fcvode.a, and they do contain these duplicate symbols. However, when I compile with clang + gfortran, these libraries also have these duplicate symbols, but, these compilers don't seem bothered by it in this case.
Also, on Ubuntu 20.04 with gcc 9 and gfortran 9, I get an undefined symbol:
[ 98%] Building Fortran object CMakeFiles/photo.run.dir/src/main.f90.o
[100%] Linking Fortran executable photo.run
/usr/bin/ld: lib/libsundials_fcvode.a(fcvband.c.o): in function `FCVBandJac':
fcvband.c:(.text+0x141): undefined reference to `fcvbjac_'
/usr/bin/ld: lib/libsundials_fcvode.a(fcvode.c.o): in function `FCVf':
fcvode.c:(.text+0x50): undefined reference to `fcvfun_'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/photo.run.dir/build.make:91: photo.run] Error 1
make[1]: *** [CMakeFiles/Makefile2:711: CMakeFiles/photo.run.dir/all] Error 2
make: *** [Makefile:130: all] Error 2
This missing symbol is very puzzling because It is not missing! I've used nm to to interogate the library I'm linking to, and the symbols are there:
...
cvode_funcs.f90.o:
0000000000000250 T fcvbjac_
0000000000000000 T fcvfun_
...
My question: What common items are missing from a CMakesList.txt, that cause these sorts of problems? Or is this sort of problem bound to happen when mixing fortran and C without iso_c_binding?
EDIT:
I ended up spending the time switching from the fortran 77 interface to the modern fortran C interface, which uses iso_c_binding. Every compiler I have tried works.

OpenGL Compilation Errors Ubuntu

I'm trying to set up the code from
http://www.openglsuperbible.com/example-code/
with command:
$ cmake -G "Unix Makefiles" ; make clean ; make all
I've installed the following packages in order to prepare the system:
xrandr libxrandr x11-xserver-utils libxrandr-dev libglfw2 libglfw-dev xorg-dev libx11-dev libgl-dev libglu-dev libgl1-mesa-dev freeglut3 freeglut3-dev mesa-common-dev freeglut3-dev build-essential libx11-dev libx11-*
I may have missed some of the packages I've installed in this list, I apologize.
Then I've modified the CMakeList.txt in order to add to the linking phase the following things:
glfw GL rt glut X11 dl Xrandr Xext GLEW GLU
Then I try to compile, but it fails at the linking phase:
/opt/sb6code/src/xraw/xraw.c: In function ‘CreateWindow’:
/opt/sb6code/src/xraw/xraw.c:106:14: warning: assignment makes pointer from integer without a cast [enabled by default]
Linking C executable bin/xraw
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function CreateWindow: error: undefined reference to 'XOpenDisplay'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function CreateWindow: error: undefined reference to 'XCloseDisplay'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function CreateWindow: error: undefined reference to 'XCreateColormap'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function CreateWindow: error: undefined reference to 'XCreateWindow'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function CreateWindow: error: undefined reference to 'XMapWindow'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function CreateWindow: error: undefined reference to 'glXCreateContextAttribsARB'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function Cleanup: error: undefined reference to 'XDestroyWindow'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function Cleanup: error: undefined reference to 'XCloseDisplay'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function main: error: undefined reference to 'XNextEvent'
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function main: error: undefined reference to 'XGetWindowAttributes'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/xraw] Errore 1
make[1]: *** [CMakeFiles/xraw.dir/all] Errore 2
make: *** [all] Errore 2
Now, I've tried to search the web and even here on SO.com for solutions, but they keep telling me to link the libraries I've already added to the linking phase.. so I can't seem to make any more progress.
Do you happen to have some knowledge or hint that could help me?
Ok, stimulated by answers I've gone into CMakeFiles directory and looked for the configuration file that manages the compilation. I've found out that for some reasons it did miss some of the flags stated into the CMakeLists despite of my update.
So now the compilation line looks like:
/usr/bin/cc CMakeFiles/xraw.dir/src/xraw/xraw.c.o -o bin/xraw -L/opt/sb6code/lib -rdynamic -lGL -lglut -lGLU -lm -lX11 -ldl -lGLEW -lXrandr -lXext -lrt -lglfw -Wl,-rpath,/opt/sb6code/lib
And the errors have reduced to :
CMakeFiles/xraw.dir/src/xraw/xraw.c.o:xraw.c:function CreateWindow: error: undefined reference to 'glXCreateContextAttribsARB'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/xraw] Errore 1
make[1]: *** [CMakeFiles/xraw.dir/all] Errore 2
make: *** [all] Errore 2
Other hints? :)
Side Note:
I've also installed CUDA libraries and under path
/usr/lib/nvidia-304/
I happen to have
alt_ld.so.conf libnvidia-ml.so.1
bin/ libnvidia-ml.so.304.88
ld.so.conf libnvidia-opencl.so.1
libcuda.so libnvidia-opencl.so.304.88
libcuda.so.1 libnvidia-tls.so.304.88
libcuda.so.304.88 libnvidia-wfb.so.1
libGL.so libnvidia-wfb.so.304.88
libGL.so.1 libOpenCL.so
libGL.so.304.88 libOpenCL.so.1
libnvcuvid.so libOpenCL.so.1.0
libnvcuvid.so.1 libOpenCL.so.1.0.0
libnvcuvid.so.304.88 libXvMCNVIDIA_dynamic.so.1
libnvidia-cfg.so libXvMCNVIDIA.so
libnvidia-cfg.so.1 libXvMCNVIDIA.so.1
libnvidia-cfg.so.304.88 libXvMCNVIDIA.so.304.88
libnvidia-compiler.so tls/
libnvidia-compiler.so.1 vdpau/
libnvidia-compiler.so.304.88 xorg/
libnvidia-glcore.so.304.88 XvMCConfig
libnvidia-ml.so
Which I would like to use since I'm not sure that my intel graphic card supports OpenGl 4.* which I shall use with the book, while the nvidia one supports 4.2.
I really can't find any good how to on the matter, the Book conveniently avoids to give installation instructions.
Temporary Solution:
I've found out that this is the only source file that does not work. Removing its files and CMakeList.txt configuration seems to be wise until someone finds out another solution.
Also, i had to use optirun to run the executables, in case other people with bumblebee/nvidia are wondering why their programs do not work.
You probably link the libraries in wrong order; all these symbols are defined in libX11.a; you should want to see what is the actual linking command line fed to GCC, and post it here.
Update now it means that you are statically trying to link against an API function that is not available in your libs (it is an optional extension and should be queried dynamically).

CMAKE DSO linking

I'm totally new to cmake. After an svn update (but also to binutils and I suspect this is causing the problem), I get an error (I successfully compiled and used the program before)
Linking CXX executable gmsh
/usr/bin/ld: /usr/local/lib/liblapack.a(dgesvd.o): undefined reference to symbol '_gfortran_concat_string##GFORTRAN_1.0'
/usr/bin/ld: note: '_gfortran_concat_string##GFORTRAN_1.0' is defined in DSO /usr/lib/libgfortran.so.3 so try adding it to the linker command line
/usr/lib/libgfortran.so.3: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
make[2]: *** [gmsh] Error 1
make[1]: *** [CMakeFiles/gmsh.dir/all] Error 2
make: *** [all] Error 2
Maybe it's related to this Fedoraproject DSO Change?
I have an up-to-date archlinux (binutils-2.23-1).
I tried to edit lines in the ccmake . menu. After hitting [c] the original options are restored (so I cannot add /usr/lib/libgfortran.so.3 to the GMSH_EXTERNAL_LIBRARIES variable in order [g] generate the new Makefiles).
I also tried adding some options which were recommended in the cmake irc chat, but it eventually didn't work.
cmake . -DCMAKE_LINK_FLAGS=-Wl,--add-needed
or
cmake . -DCMAKE_LINK_FLAGS=-lgfortran
Resulting in the same error. What can I do?
Additional information: make VERBOSE=1 pastebin link
To add a library to the link command, you can use target_link_libraries. Apparently in this case you want:
target_link_libraries(gmsh ${LINK_LIBRARIES} gfortran)

'undefined reference' errors when compiling against library

I added a third party library to my code and is getting errors like this when running make. Please help me understand this error.
(.text+0x9b4): undefined reference to `snd_strerror'
/home/bet/Tent/tun/app/Common/hl/lib/libGHAL.a(gfxhal.o): In function `GFX_create_region':
/home/bet/Tent/tun/app/Common/hl/src/GHAL/gfxhal.c:1141: undefined reference to `my_key_handler'
/home/bet/Tent/tun/app/Common/hal/src/GHAL/gfxhal.c:1141: undefined reference to `create_window'
collect2: ld returned 1 exit status
make[2]: *** [all] Error 1
make[2]: Leaving directory `/home/bet/Tent/tun/app/Common/c_app'
make[1]: *** [ctv_all] Error 2
make[1]: Leaving directory `/home/bet/Tent/tun/app/Common'
Those are Linking errors, which tell you the linker cannot find definitions for the library you are using.
You will have to link the library to your project.
gcc <your files for compilation> -lLibName
Read this for better understanding.
I still get the error even if
readline library is installed and
-lreadline is added to gcc compiling command.
The root cause of my issue is the order of the libraries/code: -lreadline (and other libraries) should be put right before -o options.

Two basic questions on the -l(blah) flag while compiling / installing programs

I am new to Ubuntu and I had the following questions.
When compiling a C or any other program one often writes a command such as gcc hello.c -lm
I am told the -lm option is for explicitly linking the math library during the compiling
phase. I wanted to know where the library 'foo' is supposed to be located if linked with - lfoo.
This is an error I get when I was trying to install a package named SUPERLU which in turn depends on the existence of a package named PARMETIS. (ending of the stuff after running make)
/usr/bin/ld: cannot find -lparmetis
collect2: ld returned 1 exit status
make[1]: * [pddrive] Error 1
make[1]: Leaving directory `/home/gaurish108/Desktop/ResearchMeetings/SUPERPETS/SuperLU_DIST_2.4/EXAMPLE'
make: * [example] Error 2
What should I do ???? I found that there is no ld folder in my /usr/bin/......Should I modify my make.inc file in some way?
Take a look here: http://www.network-theory.co.uk/docs/gccintro/gccintro_21.html. -lm says "search for libm.a in the search path". libm.a is found in /usr/lib/libm.a.

Resources