undefined reference to `tan' but math.h has been included - math

I want to compile the sample pthread code from http://pages.cs.wisc.edu/~travitch/pthreads_primer.html (Mutex section). As I run this command:
gcc -pedantic -Wall -o theaded_program pth.c -lpthread
which is stated in the link, I get this error
pth.c:45:5: warning: ISO C90 forbids mixed declarations and code [-pedantic]
/tmp/ccajksBv.o: In function `opponent':
pth.c:(.text+0x4a): undefined reference to `tan'
/tmp/ccajksBv.o: In function `main':
pth.c:(.text+0x131): undefined reference to `tan'
collect2: ld returned 1 exit status
However #include <math.h> is there in the code!! The gcc version is 4.6

You should add -lm to your compiler option.
Besides of that, you could also change -lpthread to -pthread.

In the end, it has to be like this: gcc -pedantic -Wall -o theaded_program pth.c -pthread -lm

Related

make: *** [fusion/calvin_files/fusion/src/FusionCELData.o] Error 1

I tried to build affxparser an R package on my book, M1 MacOS 12.3.1, with R 4.1.3 and gcc 11.2.0.
However the building runs error. (as the limit of characters, i haven't put few beginning lines, to see more details can click here
g++-11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I. -Ifusion/calvin_files/array/src -Ifusion/calvin_files/data/src -Ifusion/calvin_files/exception/src -Ifusion/calvin_files/fusion/src -Ifusion/calvin_files/fusion/src/GCOSAdapter -Ifusion/calvin_files/fusion/src/CalvinAdapter -Ifusion/calvin_files/parameter/src -Ifusion/calvin_files/parsers/src -Ifusion/calvin_files/portability/src -Ifusion/calvin_files/template/src -Ifusion/calvin_files/utils/src -Ifusion/calvin_files/writers/src -Ifusion/file -Ifusion/file/TsvFile -Ifusion/portability -Ifusion/util -Ifusion -D_USE_MEM_MAPPING_ -I/opt/R/arm64/include -fPIC -mtune=native -g -O2 -Wall -pedantic -Wconversion -Wno-sign-compare -O0 -c fusion/calvin_files/fusion/src/FusionCELData.cpp -o fusion/calvin_files/fusion/src/FusionCELData.o
In file included from fusion/file/CELFileData.h:33,
from fusion/calvin_files/fusion/src/GCOSAdapter/GCOSCELDataAdapter.h:30,
from fusion/calvin_files/fusion/src/FusionCELData.cpp:24:
fusion/file/FileIO.h: In function 'uint16_t affy_swap16(uint16_t)':
fusion/file/FileIO.h:67:31: warning: conversion from 'int' to 'uint16_t' {aka 'short unsigned int'} may change value [-Wconversion]
67 | return ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8));
| ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from fusion/calvin_files/fusion/src/GCOSAdapter/GCOSCELDataAdapter.h:30,
from fusion/calvin_files/fusion/src/FusionCELData.cpp:24:
fusion/file/CELFileData.h: At global scope:
fusion/file/CELFileData.h:91:23: warning: malformed '#pragma options align={mac68k|power|reset}', ignoring [-Wpragmas]
91 | #pragma options align=packed
| ^~~~~~
fusion/file/CELFileData.h:127:23: error: too many '#pragma options align=reset'
127 | #pragma options align=reset
| ^~~~~
fusion/calvin_files/fusion/src/FusionCELData.cpp: In member function 'unsigned int affymetrix_fusion_io::FusionCELData::GetFileSize()':
fusion/calvin_files/fusion/src/FusionCELData.cpp:492:22: warning: conversion from 'int64_t' {aka 'long long int'} to 'unsigned int' may change value [-Wconversion]
492 | return Fs::fileSize(filename);
| ~~~~~~~~~~~~^~~~~~~~~~
make: *** [fusion/calvin_files/fusion/src/FusionCELData.o] Error 1
ERROR: compilation failed for package ‘affxparser’
* removing ‘/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/affxparser’
Besides, i have installed some other packages which also run error. According to some information, i wrote some codes in bash_profile and Makevars in command line.
I add codes below in bash_profile
alias gcc='gcc-11'
alias cc='gcc-11'
alias g++='g++-11'
alias c++='c++-11'
I create a Makevars which is
VER=-11
CC=gcc$(VER)
CXX=g++$(VER)
CFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
CXXFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
FLIBS=-L/usr/local/Cellar/gcc/11.2.0/lib/gcc/11
I thought the make: ***[fusion/calvin_files/fusion/src/FusionCELData.o] Error 1 might be the problem, but I don't know how to fix it. Hope for anyone answer.
I fix this problem with hiding away those codes in my Makevars and bash_profile showed above, which turn my profiler into default clang to profile codes in affxparser. Finally, finish this.

Error in R CMD SHLIB compiling Fortran code

I'm trying to compile a Fortran subroutine in the remote machine, when I run:
R CMD SHLIB -fPIC vintp2p_afterburner_wind.f
I get the following error:
gcc -shared -L/sw/installed/R/3.6.2-fosscuda-2019b/lib64/R/lib -L/sw/installed/pocl/1.4-GCC-8.3.0/lib64 -L/sw/installed/ImageMagick/7.0.9-5-GCCcore-8.3.0/lib -L/sw/installed/GSL/2.6-GCC-8.3.0/lib -L/sw/installed/UDUNITS/2.2.26-GCCcore-8.3.0/lib -L/sw/installed/HDF5/1.10.5-gompic-2019b/lib -L/sw/installed/ICU/64.2-GCCcore-8.3.0/lib -L/sw/installed/libsndfile/1.0.28-GCCcore-8.3.0/lib -L/sw/installed/FFTW/3.3.8-gompic-2019b/lib -L/sw/installed/NLopt/2.6.1-GCCcore-8.3.0/lib64 -L/sw/installed/GMP/6.1.2-GCCcore-8.3.0/lib -L/sw/installed/libxml2/2.9.9-GCCcore-8.3.0/lib -L/sw/installed/cURL/7.66.0-GCCcore-8.3.0/lib -L/sw/installed/Tk/8.6.9-GCCcore-8.3.0/lib -L/sw/installed/Java/11.0.2/lib -L/sw/installed/LibTIFF/4.0.10-GCCcore-8.3.0/lib -L/sw/installed/libjpeg-turbo/2.0.3-GCCcore-8.3.0/lib64 -L/sw/installed/libpng/1.6.37-GCCcore-8.3.0/lib -L/sw/installed/PCRE/8.43-GCCcore-8.3.0/lib -L/sw/installed/SQLite/3.29.0-GCCcore-8.3.0/lib -L/sw/installed/zlib/1.2.11-GCCcore-8.3.0/lib -L/sw/installed/XZ/5.2.4-GCCcore-8.3.0/lib -L/sw/installed/bzip2/1.0.8-GCCcore-8.3.0/lib -L/sw/installed/ncurses/6.1-GCCcore-8.3.0/lib -L/sw/installed/libreadline/8.0-GCCcore-8.3.0/lib -L/sw/installed/cairo/1.16.0-GCCcore-8.3.0/lib -L/sw/installed/libGLU/9.0.1-GCCcore-8.3.0/lib -L/sw/installed/Mesa/19.1.7-GCCcore-8.3.0/lib -L/sw/installed/X11/20190717-GCCcore-8.3.0/lib -L/sw/installed/ScaLAPACK/2.0.2-gompic-2019b/lib -L/sw/installed/OpenBLAS/0.3.7-GCC-8.3.0/lib -L/sw/installed/GCCcore/8.3.0/lib64 -L/sw/installed/GCCcore/8.3.0/lib -L/sw/installed/CUDA/10.1.243/lib64 -o vintp2p_afterburner_wind.so vintp2p_afterburner_wind.o -fPIC -lgfortran -L/sw/installed/R/3.6.2-fosscuda-2019b/lib64/R/lib -lR
/sw/installed/binutils/2.28-GCCcore-6.4.0/bin/ld.gold: error: vintp2p_afterburner_wind.o: requires dynamic R_X86_64_PC32 reloc against 'log' which may overflow at runtime; recompile with -fPIC
collect2: error: ld returned 1 exit status
make: *** [vintp2p_afterburner_wind.so] Error 1
The subroutine has a log() function. I'm totally new to this error and I don't know where to start with!
Maybe someone will find it useful: compiling is done by :
gfortran -fPIC -shared -ffree-form vintp2p_afterburner_wind.f -o vintp2p_afterburner_wind.so

Using devtools::document to compile Rcpp code that includes foreign package

I'm trying to develop an R package (named gpuUtils) that will import C++ code that makes use of RViennaCL for GPU utilization.
I installed RViennaCL. Then, I followed Hadley's guidelines for package development and usage of C++ code, and created a C++ file with these lines:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
#include "viennacl/ocl/backend.hpp"
using namespace Rcpp;
Then, when running devtools::document I get this error:
g++ -m64 -I/usr/include/R -DNDEBUG -I/usr/local/include -I"/home/ndr/R/x86_64-redhat-linux-gnu-library/3.3/Rcpp/include" -I"/home/ndr/R/x86_64-redhat-linux-gnu-library/3.3/RcppArmadillo/include" -I"/home/ndr/R/x86_64-redhat-linux-gnu-library/3.3/RViennaCL/include" -fpic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -c gpuUtils.cpp -o gpuUtils.o
In file included from /home/ndr/R/x86_64-redhat-linux-gnu-library/3.3/RViennaCL/include/viennacl/ocl/backend.hpp:26:0,
from gpuUtils.cpp:3:
/home/ndr/R/x86_64-redhat-linux-gnu-library/3.3/RViennaCL/include/viennacl/ocl/context.hpp:28:19: fatal error: CL/cl.h: No such file or directory
#include <CL/cl.h>
^
compilation terminated.
make: *** [gpuUtils.o] Error 1
OpenCL and CUDA are installed and I do see CL/cl.h in my system:
$ ls -1 /usr/local/cuda-9.0/include/CL
cl_egl.h
cl_ext.h
cl_gl_ext.h
cl_gl.h
cl.h
cl.hpp
cl_platform.h
opencl.h
and
$ ls -1 /usr/include/CL/
cl2.hpp
cl_egl.h
cl_ext.h
cl_gl_ext.h
cl_gl.h
cl.h
cl.hpp
cl_platform.h
opencl.h
Also, in my DESCRIPTION file I have these lines:
LinkingTo: Rcpp, RViennaCL (>= 1.7.1.7)
Imports: Rcpp
I guess that either -I/usr/include/ or -I/usr/local/cuda-9.0/include/ have to be included in my g++ call, but I'm not sure how to get Rstudio to do that.
Any idea?

R-3.1.3 installation from source errors .bss.zero_length_string' can not be used when making a shared object; recompile with -fPIC

I've been attempting to install R-3.1.3 from source on
# cat /etc/*release*
NAME="Amazon Linux AMI"
VERSION="2015.09"
ID="amzn"
ID_LIKE="rhel fedora"
VERSION_ID="2015.09"
PRETTY_NAME="Amazon Linux AMI 2015.09"
ANSI_COLOR="0;33"
CPE_NAME="cpe:/o:amazon:linux:2015.09:ga"
HOME_URL="http://aws.amazon.com/amazon-linux-ami/"
Amazon Linux AMI release 2015.09
cpe:/o:amazon:linux:2015.09:ga
And after ./configure && make installation fails with the following error
gcc -std=gnu99 -shared -fopenmp -L/usr/local/lib64 -o libRlapack.so
dlamch.o dlapack.o cmplx.o -L../../../lib -lRblas -lgfortran -lm -lquadmath
/usr/bin/ld: /usr/lib/gcc/x86_64-amazon-linux/4.8.3/libgfortran.a(string_intrinsics.o): relocation R_X86_64_32S against `.bss.zero_length_string' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-amazon-linux/4.8.3/libgfortran.a: could not read symbols: Bad value
collect2: error: ld returned 1 exit status
make[4]: *** [libRlapack.so] Error 1
Naturally I learn about -fPIC on how to recompile with -fPIC and R_X86_64_32S on What do R_X86_64_32S and R_X86_64_64 relocation mean?
I then make the change in the CFLAGS
# ./configure 'CFLAGS=-g -O2 -fPIC' --with-x=no --enable-shared
I know it worked because configure prints the values it set,
C compiler: gcc -std=gnu99 -g -O2 -fPIC
Fortran 77 compiler: gfortran -g -O2
Nevertheless same error occurs.
I've tested this on Centos65, and Centos71 and Ubuntu1404 all work, but my VPC won't, please help

Why Am I Getting Link Errors When Calling Function in Math.h?

When attempting to call functions in math.h, I'm getting link errors like the following
undefined reference to sqrt
But I'm doing a #include <math.h>
I'm using gcc and compiling as follows:
gcc -Wall -D_GNU_SOURCE blah.c -o blah
Why can't the linker find the definition for sqrt?
Add -lm to the command when you call gcc:
gcc -Wall -D_GNU_SOURCE blah.c -o blah -lm
This will tell the linker to link with the math library. Including math.h will tell the compiler that the math functions like sqrt() exist, but they are defined in a separate library, which the linker needs to pack with your executable.
As FreeMemory pointed out the library is called libm.a . On Unix-like systems, the rule for naming libraries is lib[blah].a . Then if you want to link them to your executable you use -l[blah] .
You need to link the math library explicitly. Add -lm to the flags you're passing to gcc so that the linker knows to link libm.a
Append -lm to the end of the gcc command to link the math library:
gcc -Wall -D_GNU_SOURCE blah.c -o blah -lm
For things to be linked properly, the order of the compiler flags matters! Specifically, the -lm should be placed at the end of the line.
If you're wondering why the math.h library needs to be included at all when compiling in C, check out this explanation here.

Resources