I'm hoping for help installing Andreas Mueller's MATLAB bindings for Global Probability of Boundary.
I hope that my issue is not deeply ingrained in my installation, so I'll describe the immediate problem first, with the following paragraph noting some attempts at implementation.
According to Mueller's instructions in the readme, I did my best to install CUDA, ACML, and GCC (currently working on RHEL 7). After a successful build/install of the library libdamascene.so, I included a txt file in /etc/ld.so.conf.d/ which includes the path of libdamascene.so. It shows up when I use ldconfig -p | grep libdamascene.so.
When I open up MATLAB and try to build the MEX-file (using GCC 6.3) I get
Error using mex:
In file included from ../damascene-python-and-matlab-bindings-master/bindings/gpb_mex.c: 3: 0:
../damascene-python-and-matlab-bindings-master/bindings/gpb.h: 10: 143: error: expected ';', ',' or ')' before '=' token
void gpb(const unsigned int* in)image, unsigned int width, unsigned int height, float* border, int*textons, float* orientations, int device_num=0);
And a couple of subsequent errors that I gather are from the fact that the compiler wasn't able to properly read the header.
One thing I tried to do was to simply remove the declaration of device_num = 0 and place explicitly initialize it in the gpb_mex.c function call. This result is more nebulous.
Error using mex
/tmp/mex_1295917814264136_135235/gpb_mex.o: In function 'mexFunction':
gpb_mex.c: (.text+0x161): undefined reference to gpb
collect2: error: ld returned 1 exit status.
This error makes me thing that theres a problem with my library, which is a little more challenging to debug. I won't put all the things I tried (available on demand) to get a successful build of the library but let me summarize:
I'm running CUDA 9.0, not CUDA 8.0.
I'm running the gpuacml from the AMD Open64 SDK (only one I could find).
I had to change the gpu_architecture in the CMakelists.txt master file from sm_20 to sm_70.
I have a separate installation of GCC 6.3.0 (and GCC 5.3.0) so I can build the library.
My biggest question is: do you think this indeed a matter of an incorrectly built library or is there something else missing. Is there any way I can check if the library libdamascene.so is working correctly?
Please let me know how I can clarify my situation. Thank you so much!
Edit: I am including additional data.
Here's MATLAB output when I try to compile the MEX-file.
mex -v GCC='/usr/bin/gcc63' G++='/usr/bin/g++63' gpb_mex.c
Verbose mode is on.
... Looking for compiler 'gcc' ...
... Executing command 'which gcc' ...Yes ('/usr/bin/gcc').
... Executing command 'gcc -print-file-name=libstdc++.so' ...Yes ('/usr/lib/gcc/x86_64-redhat-linux/4.8.5/libstdc++.so').
Found installed compiler 'gcc'.
Options file details
-------------------------------------------------------------------
Compiler location: /usr/bin/gcc63
Options file: /home/clusterbomb/.matlab/R2018b/mex_C_glnxa64.xml
CMDLINE2 : /usr/bin/gcc63 -pthread -Wl,--no-undefined -Wl,-rpath-link,/usr/local/MATLAB/R2018b/bin/glnxa64 -shared -O -Wl,--version-script,"/usr/local/MATLAB/R2018b/extern/lib/glnxa64/c_exportsmexfileversion.map" /tmp/mex_1326595352957888_135235/gpb_mex.o /tmp/mex_1326595352957888_135235/c_mexapi_version.o -L"/usr/local/MATLAB/R2018b/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -o gpb_mex.mexa64
CC : /usr/bin/gcc63
DEFINES : -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -D_GNU_SOURCE -DMATLAB_MEX_FILE
MATLABMEX : -DMATLAB_MEX_FILE
CFLAGS : -fexceptions -fPIC -fno-omit-frame-pointer -pthread
INCLUDE : -I"/usr/local/MATLAB/R2018b/extern/include" -I"/usr/local/MATLAB/R2018b/simulink/include"
COPTIMFLAGS : -O2 -fwrapv -DNDEBUG
CDEBUGFLAGS : -g
LD : /usr/bin/gcc63
LDFLAGS : -pthread -Wl,--no-undefined -Wl,-rpath-link,/usr/local/MATLAB/R2018b/bin/glnxa64
LDTYPE : -shared
FUNCTIONMAP : "/usr/local/MATLAB/R2018b/extern/lib/glnxa64/mexFunction.map"
VERSIONMAP : "/usr/local/MATLAB/R2018b/extern/lib/glnxa64/c_exportsmexfileversion.map"
LINKEXPORT : -Wl,--version-script,"/usr/local/MATLAB/R2018b/extern/lib/glnxa64/mexFunction.map"
LINKEXPORTVER : -Wl,--version-script,"/usr/local/MATLAB/R2018b/extern/lib/glnxa64/c_exportsmexfileversion.map"
LINKLIBS : -L"/usr/local/MATLAB/R2018b/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++
LDOPTIMFLAGS : -O
LDDEBUGFLAGS : -g
MWCPPLIB : "/usr/local/MATLAB/R2018b/sys/os/glnxa64/libstdc++.so.6"
OBJEXT : .o
LDEXT : .mexa64
SETENV : CC="/usr/bin/gcc63"
CXX="g++"
CFLAGS="-fexceptions -fPIC -fno-omit-frame-pointer -pthread -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -D_GNU_SOURCE -DMATLAB_MEX_FILE "
CXXFLAGS="-fexceptions -fPIC -fno-omit-frame-pointer -pthread -std=c++11 -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -D_GNU_SOURCE -DMATLAB_MEX_FILE "
COPTIMFLAGS="-O2 -fwrapv -DNDEBUG"
CXXOPTIMFLAGS="-O2 -fwrapv -DNDEBUG"
CDEBUGFLAGS="-g"
CXXDEBUGFLAGS="-g"
LD="/usr/bin/gcc63"
LDXX="g++"
LDFLAGS="-pthread -Wl,--no-undefined -Wl,-rpath-link,/usr/local/MATLAB/R2018b/bin/glnxa64 -shared -L"/usr/local/MATLAB/R2018b/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -Wl,--version-script,"/usr/local/MATLAB/R2018b/extern/lib/glnxa64/mexFunction.map""
LDDEBUGFLAGS="-g"
GCC : /usr/bin/gcc63
CPPLIBS : /usr/lib/gcc/x86_64-redhat-linux/4.8.5/libstdc++.so
MATLABROOT : /usr/local/MATLAB/R2018b
ARCH : glnxa64
SRC : "/home/clusterbomb/Downloads/GPUGPB/damascene-python-and-matlab-bindings-master/bindings/gpb_mex.c";"/usr/local/MATLAB/R2018b/extern/version/c_mexapi_version.c"
OBJ : /tmp/mex_1326595352957888_135235/gpb_mex.o;/tmp/mex_1326595352957888_135235/c_mexapi_version.o
OBJS : /tmp/mex_1326595352957888_135235/gpb_mex.o /tmp/mex_1326595352957888_135235/c_mexapi_version.o
SRCROOT : /home/clusterbomb/Downloads/GPUGPB/damascene-python-and-matlab-bindings-master/bindings/gpb_mex
DEF : /tmp/mex_1326595352957888_135235/gpb_mex.def
EXP : "gpb_mex.exp"
LIB : "gpb_mex.lib"
EXE : gpb_mex.mexa64
ILK : "gpb_mex.ilk"
MANIFEST : "gpb_mex.mexa64.manifest"
TEMPNAME : gpb_mex
EXEDIR :
EXENAME : gpb_mex
G++ : /usr/bin/g++63
OPTIM : -O2 -fwrapv -DNDEBUG
LINKOPTIM : -O
CMDLINE1_0 : /usr/bin/gcc63 -c -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -D_GNU_SOURCE -DMATLAB_MEX_FILE -I"/usr/local/MATLAB/R2018b/extern/include" -I"/usr/local/MATLAB/R2018b/simulink/include" -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O2 -fwrapv -DNDEBUG "/home/clusterbomb/Downloads/GPUGPB/damascene-python-and-matlab-bindings-master/bindings/gpb_mex.c" -o /tmp/mex_1326595352957888_135235/gpb_mex.o
CMDLINE1_1 : /usr/bin/gcc63 -c -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -D_GNU_SOURCE -DMATLAB_MEX_FILE -I"/usr/local/MATLAB/R2018b/extern/include" -I"/usr/local/MATLAB/R2018b/simulink/include" -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O2 -fwrapv -DNDEBUG "/usr/local/MATLAB/R2018b/extern/version/c_mexapi_version.c" -o /tmp/mex_1326595352957888_135235/c_mexapi_version.o
-------------------------------------------------------------------
Building with 'gcc'.
/usr/bin/gcc63 -c -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -D_GNU_SOURCE -DMATLAB_MEX_FILE -I"/usr/local/MATLAB/R2018b/extern/include" -I"/usr/local/MATLAB/R2018b/simulink/include" -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O2 -fwrapv -DNDEBUG "/home/clusterbomb/Downloads/GPUGPB/damascene-python-and-matlab-bindings-master/bindings/gpb_mex.c" -o /tmp/mex_1326595352957888_135235/gpb_mex.o
/usr/bin/gcc63 -c -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -D_GNU_SOURCE -DMATLAB_MEX_FILE -I"/usr/local/MATLAB/R2018b/extern/include" -I"/usr/local/MATLAB/R2018b/simulink/include" -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O2 -fwrapv -DNDEBUG "/usr/local/MATLAB/R2018b/extern/version/c_mexapi_version.c" -o /tmp/mex_1326595352957888_135235/c_mexapi_version.o
/usr/bin/gcc63 -pthread -Wl,--no-undefined -Wl,-rpath-link,/usr/local/MATLAB/R2018b/bin/glnxa64 -shared -O -Wl,--version-script,"/usr/local/MATLAB/R2018b/extern/lib/glnxa64/c_exportsmexfileversion.map" /tmp/mex_1326595352957888_135235/gpb_mex.o /tmp/mex_1326595352957888_135235/c_mexapi_version.o -L"/usr/local/MATLAB/R2018b/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -o gpb_mex.mexa64
Error using mex
/tmp/mex_1326595352957888_135235/gpb_mex.o: In function `mexFunction':
gpb_mex.c:(.text+0x161): undefined reference to `gpb'
collect2: error: ld returned 1 exit status
Here's the header file gpb.h (with edit of removing device_num=0):
void gpb(const unsigned int* in_image,unsigned int width,unsigned int height, float* border, int* textons, float* orientations, int device_num);
Here's the gpb_mex.c file with the alteration of addition the declared device_num in the final line:
#include <mex.h>
#include "gpb.h"
void mexFunction(int nOut, mxArray *pOut[],
int nIn, const mxArray *pIn[])
{
mwSize width, height;
float* borders;
float* orientations;
int* textons;
unsigned int *in_image;
mwSize dims[3];
mwSize orientation_dims[3];
if((nIn != 1) || (nOut != 3))
mexErrMsgTxt("Usage: border,textons,orientations = gpb(image)");
if (!mxIsClass(pIn[0],"uint8") || mxGetNumberOfDimensions(pIn[0]) != 3) {
mexErrMsgTxt("Usage: th argument must be a unsigned int matrix");
}
const mwSize *indims= mxGetDimensions(pIn[0]);
if (indims[0]!=4)
mexErrMsgTxt("Image needs to be of shape 4 x widht x height");
width = indims[2];
height = indims[1];
mexPrintf("width %d, height %d\n",width,height);
mexPrintf("Element-size: %d, sizeof(int): %d, sizeof(char) %d\n",mxGetElementSize(pIn[0]),sizeof(int),sizeof(unsigned char));
dims[0]=width; dims[1]=height; //for rgb0
mexPrintf("width: %d height: %d\n",width, height);
pOut[0]=mxCreateNumericMatrix(height,width,mxSINGLE_CLASS,mxREAL);
pOut[1]=mxCreateNumericMatrix(height,width,mxINT32_CLASS,mxREAL);
orientation_dims[0]=width; orientation_dims[1]=height; orientation_dims[2]=8;
pOut[2]=mxCreateNumericArray(3,orientation_dims,mxSINGLE_CLASS,mxREAL);
borders=(float*) mxGetPr(pOut[0]);
textons=(int*) mxGetPr(pOut[1]);
orientations=(float*) mxGetPr(pOut[2]);
in_image = (unsigned int*) mxGetData(pIn[0]);
gpb(in_image,height,width,borders,textons,orientations, 0);
}
Happy to provide any more source code as need, but the rest would be ripped straight from amueller's repository (link above).
Edit 2:
using -L resulted in a permission error, which I resolved by sudo chmod 755 libdamascene.so in linux terminal.
So the good news is that matlab was able to find libdamascene, evidenced by it moving forward after permission denied. Unfortunately, the compiler is still struggling.
mex -v GCC='/usr/bin/gcc63' gpb_mex.c -L</home/clusterbomb/Downloads/GPUGPB/damascene-python-and-matlab-bindings-master/build/libdamascene.so>
Verbose mode is on.
... Looking for compiler 'gcc' ...
... Executing command 'which gcc' ...Yes ('/usr/bin/gcc').
... Executing command 'gcc -print-file-name=libstdc++.so' ...Yes ('/usr/lib/gcc/x86_64-redhat-linux/4.8.5/libstdc++.so').
Found installed compiler 'gcc'.
Options file details
-------------------------------------------------------------------
Compiler location: /usr/bin/gcc63
Options file: /home/clusterbomb/.matlab/R2018b/mex_C_glnxa64.xml
CMDLINE2 : /usr/bin/gcc63 -pthread -Wl,--no-undefined -Wl,-rpath-link,/usr/local/MATLAB/R2018b/bin/glnxa64 -shared -O -Wl,--version-script,"/usr/local/MATLAB/R2018b/extern/lib/glnxa64/c_exportsmexfileversion.map" /tmp/mex_1363958469865432_171081/gpb_mex.o /tmp/mex_1363958469865432_171081/c_mexapi_version.o -L</home/clusterbomb/Downloads/GPUGPB/damascene-python-and-matlab-bindings-master/build/libdamascene.so> -L"/usr/local/MATLAB/R2018b/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -o gpb_mex.mexa64
CC : /usr/bin/gcc63
DEFINES : -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -D_GNU_SOURCE -DMATLAB_MEX_FILE
MATLABMEX : -DMATLAB_MEX_FILE
CFLAGS : -fexceptions -fPIC -fno-omit-frame-pointer -pthread
INCLUDE : -I"/usr/local/MATLAB/R2018b/extern/include" -I"/usr/local/MATLAB/R2018b/simulink/include"
COPTIMFLAGS : -O2 -fwrapv -DNDEBUG
CDEBUGFLAGS : -g
LD : /usr/bin/gcc63
LDFLAGS : -pthread -Wl,--no-undefined -Wl,-rpath-link,/usr/local/MATLAB/R2018b/bin/glnxa64
LDTYPE : -shared
FUNCTIONMAP : "/usr/local/MATLAB/R2018b/extern/lib/glnxa64/mexFunction.map"
VERSIONMAP : "/usr/local/MATLAB/R2018b/extern/lib/glnxa64/c_exportsmexfileversion.map"
LINKEXPORT : -Wl,--version-script,"/usr/local/MATLAB/R2018b/extern/lib/glnxa64/mexFunction.map"
LINKEXPORTVER : -Wl,--version-script,"/usr/local/MATLAB/R2018b/extern/lib/glnxa64/c_exportsmexfileversion.map"
LINKLIBS : -L</home/clusterbomb/Downloads/GPUGPB/damascene-python-and-matlab-bindings-master/build/libdamascene.so> -L"/usr/local/MATLAB/R2018b/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++
LDOPTIMFLAGS : -O
LDDEBUGFLAGS : -g
MWCPPLIB : "/usr/local/MATLAB/R2018b/sys/os/glnxa64/libstdc++.so.6"
OBJEXT : .o
LDEXT : .mexa64
SETENV : CC="/usr/bin/gcc63"
CXX="g++"
CFLAGS="-fexceptions -fPIC -fno-omit-frame-pointer -pthread -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -D_GNU_SOURCE -DMATLAB_MEX_FILE "
CXXFLAGS="-fexceptions -fPIC -fno-omit-frame-pointer -pthread -std=c++11 -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -D_GNU_SOURCE -DMATLAB_MEX_FILE "
COPTIMFLAGS="-O2 -fwrapv -DNDEBUG"
CXXOPTIMFLAGS="-O2 -fwrapv -DNDEBUG"
CDEBUGFLAGS="-g"
CXXDEBUGFLAGS="-g"
LD="/usr/bin/gcc63"
LDXX="g++"
LDFLAGS="-pthread -Wl,--no-undefined -Wl,-rpath-link,/usr/local/MATLAB/R2018b/bin/glnxa64 -shared -L</home/clusterbomb/Downloads/GPUGPB/damascene-python-and-matlab-bindings-master/build/libdamascene.so> -L"/usr/local/MATLAB/R2018b/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -Wl,--version-script,"/usr/local/MATLAB/R2018b/extern/lib/glnxa64/mexFunction.map""
LDDEBUGFLAGS="-g"
GCC : /usr/bin/gcc63
CPPLIBS : /usr/lib/gcc/x86_64-redhat-linux/4.8.5/libstdc++.so
MATLABROOT : /usr/local/MATLAB/R2018b
ARCH : glnxa64
SRC : "/home/clusterbomb/Downloads/GPUGPB/damascene-python-and-matlab-bindings-master/bindings/gpb_mex.c";"/usr/local/MATLAB/R2018b/extern/version/c_mexapi_version.c"
OBJ : /tmp/mex_1363958469865432_171081/gpb_mex.o;/tmp/mex_1363958469865432_171081/c_mexapi_version.o
OBJS : /tmp/mex_1363958469865432_171081/gpb_mex.o /tmp/mex_1363958469865432_171081/c_mexapi_version.o
SRCROOT : /home/clusterbomb/Downloads/GPUGPB/damascene-python-and-matlab-bindings-master/bindings/gpb_mex
DEF : /tmp/mex_1363958469865432_171081/gpb_mex.def
EXP : "gpb_mex.exp"
LIB : "gpb_mex.lib"
EXE : gpb_mex.mexa64
ILK : "gpb_mex.ilk"
MANIFEST : "gpb_mex.mexa64.manifest"
TEMPNAME : gpb_mex
EXEDIR :
EXENAME : gpb_mex
OPTIM : -O2 -fwrapv -DNDEBUG
LINKOPTIM : -O
CMDLINE1_0 : /usr/bin/gcc63 -c -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -D_GNU_SOURCE -DMATLAB_MEX_FILE -I"/usr/local/MATLAB/R2018b/extern/include" -I"/usr/local/MATLAB/R2018b/simulink/include" -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O2 -fwrapv -DNDEBUG "/home/clusterbomb/Downloads/GPUGPB/damascene-python-and-matlab-bindings-master/bindings/gpb_mex.c" -o /tmp/mex_1363958469865432_171081/gpb_mex.o
CMDLINE1_1 : /usr/bin/gcc63 -c -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -D_GNU_SOURCE -DMATLAB_MEX_FILE -I"/usr/local/MATLAB/R2018b/extern/include" -I"/usr/local/MATLAB/R2018b/simulink/include" -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O2 -fwrapv -DNDEBUG "/usr/local/MATLAB/R2018b/extern/version/c_mexapi_version.c" -o /tmp/mex_1363958469865432_171081/c_mexapi_version.o
-------------------------------------------------------------------
Building with 'gcc'.
/usr/bin/gcc63 -c -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -D_GNU_SOURCE -DMATLAB_MEX_FILE -I"/usr/local/MATLAB/R2018b/extern/include" -I"/usr/local/MATLAB/R2018b/simulink/include" -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O2 -fwrapv -DNDEBUG "/home/clusterbomb/Downloads/GPUGPB/damascene-python-and-matlab-bindings-master/bindings/gpb_mex.c" -o /tmp/mex_1363958469865432_171081/gpb_mex.o
/usr/bin/gcc63 -c -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -D_GNU_SOURCE -DMATLAB_MEX_FILE -I"/usr/local/MATLAB/R2018b/extern/include" -I"/usr/local/MATLAB/R2018b/simulink/include" -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O2 -fwrapv -DNDEBUG "/usr/local/MATLAB/R2018b/extern/version/c_mexapi_version.c" -o /tmp/mex_1363958469865432_171081/c_mexapi_version.o
/usr/bin/gcc63 -pthread -Wl,--no-undefined -Wl,-rpath-link,/usr/local/MATLAB/R2018b/bin/glnxa64 -shared -O -Wl,--version-script,"/usr/local/MATLAB/R2018b/extern/lib/glnxa64/c_exportsmexfileversion.map" /tmp/mex_1363958469865432_171081/gpb_mex.o /tmp/mex_1363958469865432_171081/c_mexapi_version.o -L</home/clusterbomb/Downloads/GPUGPB/damascene-python-and-matlab-bindings-master/build/libdamascene.so> -L"/usr/local/MATLAB/R2018b/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -o gpb_mex.mexa64
/tmp/mex_1363958922594243_171081: line 2: -L/usr/local/MATLAB/R2018b/bin/glnxa64: No such file or directory
Error using mex
The command '/usr/bin/gcc63' exited with a return value '1'
I have double and triple checked that the directory /usr/local/MATLAB/R2018b/bin/glnxa64 exists (it does). I have also tried restoredefaultpath and rehash toolboxcache to no avail. No change when I utilize gcc 5.3.0 or the native RHEL 7 gcc. Thank you for your help so far!
In the file damascene-python-and-matlab-bindings/bindings/gpb.h, the following line causes the error:
void gpb(const unsigned int* in_image,unsigned int width,unsigned int height, float* border, int* textons, float* orientations, int device_num=0);
The last argument has a default parameter, which is a C++ feature, an not valid C syntax. Remove the =0 so it becomes:
void gpb(const unsigned int* in_image,unsigned int width,unsigned int height, float* border, int* textons, float* orientations, int device_num);
Next, when you call gpb, you cannot rely on the default value for device_num, so you need to pass this explicitly. In damascene-python-and-matlab-bindings/bindings/gpb_mex.c, the line:
gpb(in_image,height,width,borders,textons,orientations);
needs an additional argument 0:
gpb(in_image,height,width,borders,textons,orientations,0);
With these changes, I hope, the code should compile normally with a C compiler.
(I realize that you already did these things...)
Finally, the linker needs to find the gpb function. I presume this one is in the libdamascene.so library that you built. You need to tell the mex command to link in this library:
mex -v GCC='/usr/bin/gcc63' gpb_mex.c -ldamascene
(the argument to -l gets lib prepended and .a or .so appended, to form the name of the library file. Depending on where this library file resides, you might need to also add a -L<path> argument to the mex call, with <path> the directory that contains the library file:
mex -v GCC='/usr/bin/gcc63' gpb_mex.c -L/home/clusterbomb/Downloads/GPUGPB/damascene-python-and-matlab-bindings-master/build/libdamascene.so
Related
I'm building a package (vice 3.4) that uses GNU autoconf. Running ./configure fails with the following message:
checking size of time_t... 0
configure: error: can not figure type of time_t
error: Bad exit status from /var/tmp/rpm-tmp.wIgnPw (%build)
Examining config.log, this appears to be failing due to the following error:
/usr/bin/ld: /tmp/ccMTSdtB.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: nonrepresentable section on output
collect2: error: ld returned 1 exit status
I tried resolving this by adding -fPIC to the CFLAGS environment variable:
CFLAGS=-fPIC ./configure
But while this is clearly being used during other stages of ./configure...
configure:3657: checking whether the C compiler works
configure:3679: gcc -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE=1 -fPIC -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld conftest.c >&5
It doesn't seem to be used in the feature tests related to time_t:
configure:9684: checking for time_t in time.h
configure:9700: gcc -c -g -O3 -Wall -Wformat -Wformat-signedness -Wshadow -Wpointer-arith -Wstrict-prototypes -Wuninitialized -Wunreachable-code -Wno-unused-parameter -Werror=implicit-function-declaration -Wfatal-errors conftest.c >&5
[...]
configure:9753: checking size of time_t
configure:9758: gcc -o conftest -g -O3 -Wall -Wformat -Wformat-signedness -Wshadow -Wpointer-arith -Wstrict-prototypes -Wuninitialized -Wunreachable-code -Wno-unused-parameter -Werror=implicit-function-declaration -Wfatal-errors -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld conftest.c >&5
Looking at configure.ac, the above code is generated from:
if test $bu_cv_decl_time_t_time_h = yes; then
AC_CHECK_SIZEOF([time_t],[],[#include <time.h>])
else
AC_CHECK_SIZEOF([time_t],[],[#include <sys/types.h>])
fi
I've been able to work around the problem by redefining CC instead:
CC="gcc -fPIC" ./configure
This works, but it's ugly (what if something wants to call gcc without -fPIC?). Is there a reason that AC_CHECK_SIZEOF is ignoring CFLAGS?
It looks like the suspicion I expressed in comments is confirmed. The VICE configure program intentionally substitutes its own choice of CFLAGS for the user's specified ones when it performs some of its checks, including the ones related to time_t. Thus, this is not a question of Autoconf ignoring your flags, but rather of VICE doing so.
I reiterate that I think it's a more significant issue that configuration doesn't work out of the box for you in the first place. I suggest raising that as an issue against the project. Although I don't like that VICE is so cavalier about ignoring the user's CFLAGS, that's a separate matter, and one that I suspect wouldn't get much traction with the devs.
John correctly identified the issue, and based on that information I did a little digging and I've figure out what's going on.
I'm building this under the control of rpm, which calls configure like this:
%configure --enable-native-gtk3ui $COMMON_FLAGS
That %configure expands into a chunk of shell code that looks like this:
CFLAGS="${CFLAGS:--O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection}" ; export CFLAGS ;
CXXFLAGS="${CXXFLAGS:--O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection}" ; export CXXFLAGS ;
FFLAGS="${FFLAGS:--O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -I/usr/lib64/gfortran/modules}" ; export FFLAGS ;
FCFLAGS="${FCFLAGS:--O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -I/usr/lib64/gfortran/modules}" ; export FCFLAGS ;
LDFLAGS="${LDFLAGS:--Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld}" ; export LDFLAGS;
[ "1" = 1 ] && for i in $(find $(dirname ./configure) -name config.guess -o -name config.sub) ; do
[ -f /usr/lib/rpm/redhat/$(basename $i) ] && /usr/bin/rm -f $i && /usr/bin/cp -fv /usr/lib/rpm/redhat/$(basename $i) $i ;
done ;
[ "1" = 1 ] && [ x != "x-Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld" ] &&
for i in $(find . -name ltmain.sh) ; do
/usr/bin/sed -i.backup -e 's~compiler_flags=$~compiler_flags="-Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld"~' $i
done ;
./configure --build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu \
--program-prefix= \
--disable-dependency-tracking \
\
--prefix=/usr \
--exec-prefix=/usr \
--bindir=/usr/bin \
--sbindir=/usr/sbin \
--sysconfdir=/etc \
--datadir=/usr/share \
--includedir=/usr/include \
--libdir=/usr/lib64 \
--libexecdir=/usr/libexec \
--localstatedir=/var \
--sharedstatedir=/var/lib \
--mandir=/usr/share/man \
--infodir=/usr/share/info
Of particular interest is the fact that this is setting LDFLAGS. The failing config test is:
gcc -o conftest -g -O3 -Wall -Wformat -Wformat-signedness -Wshadow \
-Wpointer-arith -Wstrict-prototypes -Wuninitialized -Wunreachable-code \
-Wno-unused-parameter -Werror=implicit-function-declaration \
-Wfatal-errors -Wl,-z,relro -Wl,--as-needed -Wl,-z,now \
-specs=/usr/lib/rpm/redhat/redhat-hardened-ld conftest.c
That -specs line points to a gcc spec file that looks like this:
*self_spec:
+ %{!static:%{!shared:%{!r:-pie}}}
That effectively adds the -pie option to the command line. We can reproduce the failure with a simple hello.c program:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv) {
printf("hello world\n");
return(0);
}
Like this:
$ gcc -o hello -pie hello.c
/usr/bin/ld: /tmp/ccXRAu6r.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: nonrepresentable section on output
collect2: error: ld returned 1 exit status
But here's the thing:
A successful gcc invocation looks like this:
gcc -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 \
-Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong \
-grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 \
-specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic \
-fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection \
-D_GNU_SOURCE=1 -fPIC -Wl,-z,relro -Wl,--as-needed -Wl,-z,now \
-specs=/usr/lib/rpm/redhat/redhat-hardened-ld conftest.c
Note that there is a second -specs option there, that points to /usr/lib/rpm/redhat/redhat-hardened-cc1, which looks like:
*cc1_options:
+ %{!r:%{!fpie:%{!fPIE:%{!fpic:%{!fPIC:%{!fno-pic:-fPIE}}}}}}
This takes care of adding -fPIE to the command line, which results in a successful compile:
$ gcc -o hello -pie -fPIE hello.c
$
We're hitting this failure because the configure script is replacing CFLAGS during the tests, but preserving LDFLAGS. This results in a faulty command line.
For my own purposes, I'm just patching the configure script to not replace CFLAGS during the time_t related tests. This seems both expedient (it's a one-character diff if I just comment out the appropriate line) and effective (it results in a successful build).
I will probably report this upstream as a bug.
After a certain attempt to write a simple program with a main and one function,
I ask for your help to find the bug. I include the 3 files that are in action:
the main function in base.c
the function in fun.c
the makefile
The compiler says that the function is called in a bad way in the main:
undefined reference to `fun'
base.c
#include <stdio.h>
int fun(char c);
main()
{
printf("please enter a single char\n");
char c=getchar();
fun(c);
return 0;
}
fun.c
#include <stdio.h>
int fun(char c)
{
printf("%d3 is the value of your char!\n", 'c');
return 0;
}
makefile
charprint: base.o fun.o
gcc -g -Wall -ansi base.o fun.o -o charprint
base.o: base.c
gcc -g -Wall -ansi base.c -o base.o
fun.o: fun.c
gcc -g -Wall -ansi fun.c -o fun.o
The compilation problem is that you forgot the -c flags in the compiler line for base.o and fun.o. One obvious simple (but not very good) way to fix that is:
charprint: base.o fun.o
gcc -g -Wall -ansi base.o fun.o -o charprint
base.o: base.c
gcc -c -g -Wall -ansi base.c -o base.o
fun.o: fun.c
gcc -c -g -Wall -ansi fun.c -o fun.o
Interestingly, the simplest fix would be to delete the two compiler command for the two object files — make knows how to compile C files to object files. You could set CFLAGS += -Wall (or CFLAGS = -Wall) to get the (very important) -Wall flag included. Adding -Werror too would be good.
CFLAGS += -Wall -Werror -g -std=c11
charprint: base.o fun.o
${CC} ${CFLAGS} base.o fun.o -o $#
base.o: base.c
fun.o: fun.c
In the code for fun(), your argument to printf() should be just c and not 'c'. You probably also mean %3d rather than %d3 though that 'works'; it just doesn't do what you expect. Note that you should use an explicit int main(void) { … }. And you should probably create a header fun.h containing:
extern int fun(char c);
and #include "fun.h" in both source files, and add fun.h after the source file name in the dependency lines in the makefile:
CFLAGS += -Wall -Werror -g -std=c11
charprint: base.o fun.o
${CC} ${CFLAGS} base.o fun.o -o $#
base.o: base.c fun.h
fun.o: fun.c fun.h
In fact, you don't need to list the source files as dependencies for the object files; make will infer that dependency automatically. But you do need to specify the header file dependency.
I am using NDK standalone toolchain to build a dynamic library(.so) on Mac OS.
I just migrate the C language compiler from GCC to clang.When using GCC the processes are all OK and so file is generated successfully. But when using clang,some errors happen when issue ./configure.The errors are as follows.
The picture error logs:
The text error logs,it is the same with the above picture error logs:
check_as
BEGIN /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.t5WIvSJ0.S
1 .altmacro
END /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.t5WIvSJ0.S
arm-linux-androideabi-clang -D_ISOC99_SOURCE -Dstrtod=avpriv_strtod -I$(SRC_PATH)/compat/float -DPIC -O3 -Wall -pipe -std=c99 -ffast-math -fstrict-aliasing -Werror=strict-aliasing -Wno-psabi -Wa,--noexecstack -DANDROID -DNDEBUG -I/Users/zexu/github/ijkplayer/android/contrib/build/openssl-armv7a/output/include -march=armv7-a -mcpu=cortex-a8 -mfpu=vfpv3-d16 -mfloat-abi=softfp -mthumb -mcpu=cortex-a8 -fPIC -c -o /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.DIksqWpw.o /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.t5WIvSJ0.S
warning: unknown warning option '-Wno-psabi' [-Wunknown-warning-option]
1 warning generated.
/var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.t5WIvSJ0.S:1:1: error: unknown directive
.altmacro
^
check_gas using 'arm-linux-androideabi-clang' as AS
check_as
BEGIN /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.t5WIvSJ0.S
1 .macro m n, y:vararg=0
2 \n: .int \y
3 .endm
4 m x
END /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.t5WIvSJ0.S
arm-linux-androideabi-clang -D_ISOC99_SOURCE -Dstrtod=avpriv_strtod -I$(SRC_PATH)/compat/float -DPIC -O3 -Wall -pipe -std=c99 -ffast-math -fstrict-aliasing -Werror=strict-aliasing -Wno-psabi -Wa,--noexecstack -DANDROID -DNDEBUG -I/Users/zexu/github/ijkplayer/android/contrib/build/openssl-armv7a/output/include -march=armv7-a -mcpu=cortex-a8 -mfpu=vfpv3-d16 -mfloat-abi=softfp -mthumb -mcpu=cortex-a8 -fPIC -c -o /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.DIksqWpw.o /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.t5WIvSJ0.S
warning: unknown warning option '-Wno-psabi' [-Wunknown-warning-option]
1 warning generated.
check_as
BEGIN /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.t5WIvSJ0.S
1 .altmacro
END /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.t5WIvSJ0.S
arm-linux-androideabi-clang -D_ISOC99_SOURCE -Dstrtod=avpriv_strtod -I$(SRC_PATH)/compat/float -DPIC -O3 -Wall -pipe -std=c99 -ffast-math -fstrict-aliasing -Werror=strict-aliasing -Wno-psabi -Wa,--noexecstack -DANDROID -DNDEBUG -I/Users/zexu/github/ijkplayer/android/contrib/build/openssl-armv7a/output/include -march=armv7-a -mcpu=cortex-a8 -mfpu=vfpv3-d16 -mfloat-abi=softfp -mthumb -mcpu=cortex-a8 -fPIC -c -o /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.DIksqWpw.o /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.t5WIvSJ0.S
warning: unknown warning option '-Wno-psabi' [-Wunknown-warning-option]
1 warning generated.
/var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.t5WIvSJ0.S:1:1: error: unknown directive
.altmacro
^
GNU assembler not found, install/update gas-preprocessor
The toolchain configurations in the configure file:
# toolchain
ar_default="ar"
cc_default="clang"
cxx_default="clang++"
host_cc_default="clang"
cp_f="cp -f"
doxygen_default="doxygen"
install="install"
ln_s_default="ln -s -f"
nm_default="nm -g"
objformat="elf"
pkg_config_default=pkg-config
ranlib_default="ranlib"
strip_default="strip"
version_script='--version-script'
yasmexe_default="yasm"
windres_default="windres"
The NDK version is r14b,and the clang version:
Android clang version 3.8.275480 (based on LLVM 3.8.275480)
Target: x86_64-apple-darwin17.2.0
Thread model: posix
I know that the error is not caused by the "gas-preprocessor",because GCC compiler also has this problem.The GCC logs with no errors:
check_cc
BEGIN /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.51pLIKDT.c
1 unsigned int endian = 'B' << 24 | 'I' << 16 | 'G' << 8 | 'E';
END /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.51pLIKDT.c
arm-linux-androideabi-gcc -D_ISOC99_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Dstrtod=avpriv_strtod -DPIC -O3 -Wall -pipe -std=c99 -ffast-math -fstrict-aliasing -Werror=strict-aliasing -Wno-psabi -Wa,--noexecstack -DANDROID -DNDEBUG -I/Users/zexu/github/ijkplayer/android/contrib/build/openssl-armv7a/output/include -march=armv7-a -mcpu=cortex-a8 -mfpu=vfpv3-d16 -mfloat-abi=softfp -mthumb -mcpu=cortex-a8 -std=c11 -fomit-frame-pointer -fPIC -c -o /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.bcyz1Buv.o /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.51pLIKDT.c
gas-preprocessor.pl -arch arm -as-type gas -- arm-linux-androideabi-gcc -v
./configure: line 883: gas-preprocessor.pl: command not found
check_gas using 'arm-linux-androideabi-gcc' as AS
check_as
BEGIN /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.QeTKRayl.S
1 .macro m n, y:vararg=0
2 \n: .int \y
3 .endm
4 m x
END /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.QeTKRayl.S
arm-linux-androideabi-gcc -D_ISOC99_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Dstrtod=avpriv_strtod -DPIC -O3 -Wall -pipe -std=c99 -ffast-math -fstrict-aliasing -Werror=strict-aliasing -Wno-psabi -Wa,--noexecstack -DANDROID -DNDEBUG -I/Users/zexu/github/ijkplayer/android/contrib/build/openssl-armv7a/output/include -march=armv7-a -mcpu=cortex-a8 -mfpu=vfpv3-d16 -mfloat-abi=softfp -mthumb -mcpu=cortex-a8 -fPIC -c -o /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.bcyz1Buv.o /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.QeTKRayl.S
check_as
BEGIN /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.QeTKRayl.S
1 .altmacro
END /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.QeTKRayl.S
arm-linux-androideabi-gcc -D_ISOC99_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Dstrtod=avpriv_strtod -DPIC -O3 -Wall -pipe -std=c99 -ffast-math -fstrict-aliasing -Werror=strict-aliasing -Wno-psabi -Wa,--noexecstack -DANDROID -DNDEBUG -I/Users/zexu/github/ijkplayer/android/contrib/build/openssl-armv7a/output/include -march=armv7-a -mcpu=cortex-a8 -mfpu=vfpv3-d16 -mfloat-abi=softfp -mthumb -mcpu=cortex-a8 -fPIC -c -o /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.bcyz1Buv.o /var/folders/k6/bsgr75lj6j99fl0q8ygrr7840000gn/T//ffconf.QeTKRayl.S
Anyone can give me some tips?
Issues with assembler when switching from GCC to Clang are usually fixed with -fno-integrated-as. Clang has a built in assembler that it uses for assembling generating code that is not 100% compatible with GNU's assembler. If you have any inline assembly, you will either need to update that assembly to be compatible with LLVM's assembler or pass -fno-integrated-as to instruct Clang to use GNU as instead.
I am trying to compile a program and I have to set the paths to the NETCDF inc and lib directories in the Makefile as well as set the gfortran and c compiler settings. The variables were already named as shown below, it just told me to edit the paths/options which I have tried to do. Here is what I have for my netcdf inc and lib, as well as the hdf5:
NETCDFINC = -I$(NETCDFF_INCDIR) -I$(NETCDFC_INCDIR)
NETCDFLIB = -Wl,-rpath=$(NETCDFF_LIBDIR) -L$(NETCDFF_LIBDIR) -Wl,-
rpath=$(NETCDFC_LIBDIR) -L$(NETCDFC_LIBDIR) -Wl,-rpath=$(HDF5_LIBDIR) -
L$(HDF5_LIBDIR) -lhdf5_hl -lhdf5 -lz
Note that the NETCDFLIB is on one line on my file, and the paths of the variables are correct, but the syntax may not be.
Next for the fortran and c compiler information I have the following:
FC = gfortran -m64 -g -O0 -ffixed-line-length-132 -Wunused -Wuninitialized
CC = gcc
CFLAGS = -m64 -c -g -I. -DLONG32 -DUNDERSCORE -DLITTLE -Wunused -
Wuninitialized
Finally, when I run the program I first get a bunch of warnings such as:
oban_namelist.f90:495.29:
real :: flt
1
Warning: Unused variable 'flt' declared at (1)
Followed by a bunch of undefined reference errors such as:
oban.o: In function `check':
/uufs/chpc.utah.edu/common/home/zpu-
group3/dhodges/DART/data/radar/opaws/oban.f90:1902: undefined reference to
`__netcdf_MOD_nf90_strerror'
The error shouldn't be in this oban.o function since I didn't write it. The only things I have edited in the program I listed above for the netcdf path and gfortran/c information and so I think the error lies there. This is my first question on here, so if I missed anything you need feel free to ask. What can I try?
Edit: By request, this is the entire makefile I am using. Also, I just type 'make' to compile it per the instructions in the README file.
# Makefile for creating the OPAWS analysis software
#
# Rev: 02/05/10 LJW
# 12/05/10 DCD
# 02/11/11 LJW
#
# netCDF4 libs - you need to fill in the blanks
NETCDFINC = -I$(NETCDFF_INCDIR) -I$(NETCDFC_INCDIR)
NETCDFLIB = -Wl,-rpath=$(NETCDFF_LIBDIR) -L$(NETCDFF_LIBDIR) -Wl,-rpath=$(NETCDFC_LIBDIR) -L$(NETCDFC_LIBDIR) -Wl,-rpath=$(HDF5_LIBDIR) -
L$(HDF5_LIBDIR) -lhdf5_hl -lhdf5 -lz
# Fortran and C compiler information - various configurations are setup, try and find one close
#=====>> Gfortran
#
FC = gfortran -m64 -g -O0 -ffixed-line-length-132 -Wunused -Wuninitialized
CC = gcc
CFLAGS = -m64 -c -g -I. -DLONG32 -DUNDERSCORE -DLITTLE -Wunused -Wuninitialized
# Leave this stuff alone
EXEC = x.oban
EXECcs = x.clutter_stats
EXECmd = mosaic_2_dart
OBJS = DART.o oban_module.o dict_module.o oban_namelist.o derived_types.o util.o fileio.o read_dorade.o binio.o v5d.o
OBJScs = DART.o dict_module.o oban_namelist.o derived_types.o fileio.o util.o read_dorade.o binio.o v5d.o
OBJSmd = DART.o dict_module.o oban_namelist.o derived_types.o fileio.o util.o read_dorade.o binio.o v5d.o
default: $(EXEC) $(EXECcs) $(EXECmd)
$(EXEC): $(OBJS) oban.o
$(FC) $(OPT) -o $(EXEC) oban.o $(OBJS) $(NETCDFLIB)
$(EXECcs): $(OBJScs) clutter_stats.o
$(FC) $(OPT) -o $(EXECcs) clutter_stats.o $(OBJScs) $(NETCDFLIB)
$(EXECmd): $(OBJSmd) mosaic_2_dart.o
$(FC) $(OPT) -o $(EXECmd) mosaic_2_dart.o $(OBJSmd) $(NETCDFLIB)
clean:
rm $(EXEC) oban.o $(OBJS) $(EXECcs) clutter_stats.o $(OBJScs) $(EXECmd) mosaic_2_dart.o $(OBJSmd) *.mod ncgen.input *.pyc sweep_file_list.txt
# Individual compilation instructions
oban.o: oban.f90 structures.inc opaws.inc DART.o
$(FC) $(OPT) -c $(NETCDFINC) oban.f90
clutter_stats.o: clutter_stats.f90 opaws.inc
$(FC) $(OPT) -c $(NETCDFINC) clutter_stats.f90
mosaic_2_dart.o: mosaic_2_dart.f90 opaws.inc
$(FC) $(OPT) -c $(NETCDFINC) mosaic_2_dart.f90
oban_module.o: oban_module.f90 derived_types.o opaws.inc
$(FC) $(OPT) -c oban_module.f90
read_dorade.o: read_dorade.c read_dorade.h
$(CC) $(CFLAGS) -c read_dorade.c
fileio.o: fileio.f90
$(FC) $(OPT) $(NETCDFINC) -c fileio.f90
util.o: util.f opaws.inc structures.inc
$(FC) $(OPT) -c util.f
DART.o: DART.f
$(FC) $(OPT) -c DART.f
derived_types.o: derived_types.f90
$(FC) $(OPT) -c derived_types.f90
oban_namelist.o: oban_namelist.f90 opaws.inc
$(FC) $(OPT) -c oban_namelist.f90
dict_module.o: dict_module.f90
$(FC) $(OPT) -c dict_module.f90
binio.o: binio.c
$(CC) $(CFLAGS) binio.c -o binio.o
v5d.o: v5d.c
$(CC) $(CFLAGS) v5d.c -o v5d.o
The problem was solved by editing the NETCDFLIB line as follows:
I edited the NETCDFLIB line as follows and it works: NETCDFLIB = -Wl,-
rpath=$(NETCDFF_LIBDIR) -L$(NETCDFF_LIBDIR) -Wl,-rpath=$(NETCDFC_LIBDIR) -
L$(NETCDFC_LIBDIR) -lnetcdff -lnetcdf -lm -Wl,-rpath=$(HDF5_LIBDIR) -
L$(HDF5_LIBDIR) -lhdf5_hl -lhdf5 -lz
Basically I just needed to add:
-lnetcdff -lnetcdf -lm
This allowed for the compiler to get access to the libraries it needed that were originally undefined.
Hello to the good people of Stack Overflow, I am back at it again with the questions! haha
So I have a couple of files that I am trying to compile and link together. The library I have created is a DLL. Using a RTLD_LAZY method. I have the library in the same directory as the application. I'm having a tough time completing this task. I'm trying to link it using -ldl.
My files that I have are:
lab3.c //Which has my main
myLibrary.c //which is my library
sdv.h //which is my header file
testDynamic.c //which is my handle file for the RTLD_LAZY
Here is what I have done so far, but I'm clearly screwing something up..
dynamic: lab3.o myLibrary.o test
gcc -o dynamic lab3.o myLibrary.o -lm
lab3.o: lab3.c
gcc -fPIC -c -o lab3.o lab3.c -lm
myLibrary.o: myLibrary.c
gcc -fPIC -c myLibrary.c -o myLibrary.o -lm
libmyLibrary.so: myLibrary.o
gcc -shared -o libmyLibrary.so myLibrary.o -lm
testDynamic.o:
gcc testDynamic.c -L. -lmyLibrary -o test -ldl
export LD_LIBRARY_PATH=./
Any advice will be greatly appreciated! Thanks in advance!
Here comes an example close to your own makefile.
makefile:
CC=gcc
CFLAGS=-Wall -Wextra -Werror -std=gnu99
.PHONY:all mrproper clean
all:libmyLibrary.so dynamic testDynamic
dynamic:lab3.o
$(CC) $(CFLAGS) -o dynamic lab3.o -L. -lmyLibrary -lm
lab3.o:lab3.c
$(CC) $(CFLAGS) -c -o lab3.o lab3.c
myLibrary.o:myLibrary.c
$(CC) $(CFLAGS) -fPIC -c myLibrary.c -o myLibrary.o
libmyLibrary.so:myLibrary.o
$(CC) $(CFLAGS) -shared -o libmyLibrary.so myLibrary.o -lm
testDynamic.o:testDynamic.c
$(CC) $(CFLAGS) -o testDynamic.o -c testDynamic.c
testDynamic:testDynamic.o
$(CC) $(CFLAGS) -o testDynamic testDynamic.o -ldl
mrproper:clean
rm -f dynamic libmyLibrary.so testDynamic
clean:
rm -f lab3.o myLibrary.o testDynamic.o
Use -lm only when linking. -fPic is only necessary when linking dynamic library. -lmyLibrary is only necessary when linking program with dynamic library. So generating testDynamic does not need it as it will load library dynamically, it does not require being linked to it.
sdv.h:
#ifndef SDV_H
#define SDV_H
extern void sdv_print_version();
#endif
myLibrary.c:
#include <stdio.h>
#include <stdlib.h>
void sdv_print_version()
{
printf("v1.0\n");
}
testDynamic.c:
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#define MY_LIB "libmyLibrary.so"
#define PRINT_VERSION "sdv_print_version"
int main()
{
void *handle = dlopen(MY_LIB, RTLD_LAZY);
if (handle == NULL)
{
fprintf(stderr, "%s\n", dlerror());
return 1;
}
void (*p_print_version)() = NULL;
p_print_version = dlsym(handle, PRINT_VERSION);
if (p_print_version == NULL)
{
printf("No version available.\n");
fprintf(stderr, "%s\n", dlerror());
}
else
{
(*p_print_version)();
}
if (dlclose(handle) != 0)
{
fprintf(stderr, "%s\n", dlerror());
}
return 0;
}
Now building:
>make
gcc -Wall -Wextra -Werror -std=gnu99 -fPIC -c myLibrary.c -o myLibrary.o
gcc -Wall -Wextra -Werror -std=gnu99 -shared -o libmyLibrary.so myLibrary.o -lm
gcc -Wall -Wextra -Werror -std=gnu99 -c -o lab3.o lab3.c
gcc -Wall -Wextra -Werror -std=gnu99 -o dynamic lab3.o -L. -lmyLibrary -lm
gcc -Wall -Wextra -Werror -std=gnu99 -o testDynamic.o -c testDynamic.c
gcc -Wall -Wextra -Werror -std=gnu99 -o testDynamic testDynamic.o -ldl
Let's keep a first version of dynamic library:
>mkdir v1.0
>mv libmylibrary.so v1.0/
Now update myLibrary.c:
#include <stdio.h>
#include <stdlib.h>
void sdv_print_version()
{
printf("v2.0\n");
}
Build again:
>make
gcc -Wall -Wextra -Werror -std=gnu99 -fPIC -c myLibrary.c -o myLibrary.o
gcc -Wall -Wextra -Werror -std=gnu99 -shared -o libmyLibrary.so myLibrary.o -lm
Let's move this new dynamic library to another directory:
>mkdir v2.0
>mv libmylibrary.so v2.0/
Now testing with no dynamic library available:
>export LD_LIBRARY_PATH=./
>PATH=$PATH:.
>dynamic
dynamic: error while loading shared libraries: libmyLibrary.so: cannot open shared object file: No such file or directory
>testDynamic
libmyLibrary.so: cannot open shared object file: No such file or directory
What's the difference? In "testDynamic.c" we could, instead of showing a message error, do an alternative algorithm not requiring "libmyLibrary.so".
Now checking we can use "libmyLibrary.so":
>cd v1.0
>../dynamic
v1.0
>../testDynamic
v1.0
>cd ../v2.0
>../dynamic
v2.0
>../testDynamic
v2.0