Symbolic link to compile the code using 32-bit libraries in 64-bit Ubuntu 12.10 - matlab

I am trying to mex C code using 32-bit 2012a Matlab on 64-bit Ubuntu 12.10
I've downloaded all possible libraries (gcc 4.7, build-essential,libs-32 etc.) however I am getting the following error
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libstdc++.a when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgomp.so when searching for -lgomp
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgomp.a when searching for -lgomp
/usr/bin/ld: cannot find -lgomp
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
I've found that this problem can be solved by setting symbolic link from 64-bit libraries to the 32-bit ones. I tried to create different links but couldn't finish the compilation.
Thank you in advance.
UPDATE 1
gcc-multilib was missing, so after
sudo apt-get install gcc-multilib
I get the following errors
Warning: You are using gcc version "4.7.2-2ubuntu1)". The version
currently supported with MEX is "4.4.6".
For a list of currently supported compilers see:
http://www.mathworks.com/support/compilers/current_release/
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libstdc++.a when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++
collect2: error: ld returned 1 exit status
My mexopts.sh looks like
CC='gcc'
CFLAGS='-ansi -D_GNU_SOURCE'
CFLAGS="$CFLAGS -fPIC -pthread -m32"
CFLAGS="$CFLAGS -fexceptions"
CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64"
CLIBS="$RPATH $MLIBS -lm"
COPTIMFLAGS='-O -DNDEBUG'
CDEBUGFLAGS='-g'
CLIBS="$CLIBS -lstdc++"
the -m32 is there, however I am not sure if it should be written in that way.
Can someone elaborate on how to edit mexopts.sh to make matlab look at 32-bit libraries?
UPDATE 2
after looking at Linking using g++ fails searching for -lstdc++
I tried to install g++-multilib
sudo apt-get install g++-multilib
Now, errors have the form:
/usr/bin/ld: i386:x86-64 architecture of input file `bin/fv_cache.o' is incompatible with i386 output
/usr/bin/ld: i386:x86-64 architecture of input file `bin/obj_func.o' is incompatible with i386 output
...

First make sure you can build 32-bit executable from command prompt by executing gcc (more info here: 32bit application on 64 bit Linux)
In matlab command prompt execute mex -setup and choose gcc as a compiler. At the end you will get a message about location of 'mexopts.sh' (usually ~/.matlab//mexopts.sh)
Try to "mex".
If not successful open mexopts.sh and check if option -m32 is in CFLAGS. If not, add it.

Related

what's the properly cmakelists.txt for libpq library

I am trying to use libpq postgresql.
https://pgpedia.info/l/libpq.html
I copied the exact code.Trying to understand how to use cmake.
I am using Clion. (I guess the IDE doesn't matter.
The following part is CMakeLists.txt.
cmake_minimum_required(VERSION 3.23) project(test111 C)
set(CMAKE_C_STANDARD 99)
add_executable(test111 main.c)
find_package(PostgreSQL REQUIRED)
#target_include_directories(ecpg PUBLIC ${/usr/include/postgresql})
#target_link_libraries(ecpg PUBLIC ${PostgreSQL_LIBRARIES}
target_link_libraries(test111 PUBLIC ${/usr/lib/postgresql/15/lib})
#target_include_directories(MyTarget PRIVATE ${/usr/include/postgresql})
include_directories("/usr/include/postgresql")
using pg_config | rg PKG command return:
PKGINCLUDEDIR = /usr/include/postgresql
PKGLIBDIR = /usr/lib/postgresql/15/lib
Also mark this related to postgresql. I copy the exact code from https://pgpedia.info/l/libpq.html to c_libpqtest.c
gcc -o c_assert -I/usr/include/postgresql -L/usr/lib/postgresql/15/lib c_libpqtest.c
Still errors:
/usr/bin/ld: c_libpqtest.c:(.text+0x70): undefined reference to `PQstatus'
/usr/bin/ld: c_libpqtest.c:(.text+0x80): undefined reference to `PQerrorMessage'
/usr/bin/ld: c_libpqtest.c:(.text+0xbd): undefined reference to `PQexec'
/usr/bin/ld: c_libpqtest.c:(.text+0xd7): undefined reference to `PQgetvalue'
/usr/bin/ld: c_libpqtest.c:(.text+0xeb): undefined reference to `PQclear'
/usr/bin/ld: c_libpqtest.c:(.text+0xf7): undefined reference to `PQfinish'
collect2: error: ld returned 1 exit status
Update: The following compile ways will work. Reference: https://riptutorial.com/postgresql/example/8018/accessing-postgresql-with-the-c-api
gcc -Wall -o c_libpq -I/usr/include/postgresql -L/usr/lib/x86_64-linux-gnu c_libpqtest.c -lpq
gcc -Wall -o c_libpq -I "$(pg_config --includedir)" -L "$(pg_config --libdir)" c_libpqtest.c -lpq
gcc -o c_libpq -I/usr/include/postgresql -L/usr/lib/postgresql/14/lib c_libpqtest.c -lpq
Shared libpq library in /usr/lib/x86_64-linux-gnu
So overall I know how to use gcc command line solve this problem. I still don't know how to use cmake to solve the problem...
CMake provides a FindPostgreSQL.cmake module file, and it defines an imported target since 3.14: https://cmake.org/cmake/help/latest/module/FindPostgreSQL.html
So the most robust approach is:
cmake_minimum_required(VERSION 3.14)
project(myproject LANGUAGES C)
find_package(PostgreSQL REQUIRED)
add_executable(myapp main.c)
target_link_libraries(myapp PRIVATE PostgreSQL::PostgreSQL)

ld: cannot find -lc when linking as elf i386

I'm stumped on this.
I am using Ubuntu 18.04. I want link together an executable as a 32-bit program, for educational purposes.
My ld default search paths are all for the 64bit toolchain, so I think I need to invoke ld with additional library search paths since I want to link as 32bit. It can not find libc for some reason.
ld -mi386linux -L/lib/i386-linux-gnu/ -lc -o cc codechef.o -dynamic-linker /lib/i386-linux-gnu/ld-linux.so.2
This gives me the error ld: cannot find -lc
I have verified that libc.so.6 file does exist at /lib/i386-linux-gnu/libc.so.6
adding --verbose to my command reveals: attempt to open /lib/i386-linux-gnu//libc.a failed Why does my ubuntu installation not have this file but does have the .so?

Matlab: Cannot compile mex with openmp (undefined reference)

When compiling some C++ code in Matlab 2016b using:
mex CXXFLAGS="\$CXXFLAGS -std=c++11 -fopenmp" CXXOPTIMFLAGS='\$CXXOPTIMFLAGS -Ofast -DNDEBUG mexMyFunction.cpp
I got the following errors:
undefined reference to `omp_get_thread_num'
undefined reference to `omp_get_num_threads'
System: Ubuntu 16.04, g++ version: 5.4.0.
If I remove -fopenmp from the above command then it worked fine. Compiling directly with g++ (without mex) also worked.
Could you please help me to resolve this?
Thank you so much in advance!
The the following linking flags were missing:
LDOPTIMFLAGS="$LDOPTIMFLAGS -fopenmp -O2" -lgomp
Complete command:
mex CXXFLAGS="\$CXXFLAGS -std=c++11 -fopenmp" CXXOPTIMFLAGS='\$CXXOPTIMFLAGS -Ofast -DNDEBUG' LDOPTIMFLAGS="$LDOPTIMFLAGS -fopenmp -O2" -lgomp -I"/home/khue/Libs/Eigen" mexMyFunction.cpp
Thanks to #Zulan for his suggestion.

Ubuntu gcc compile errors

When I compile my program with the command
gcc -o ****** -Xlinker -Bstatic -L/usr/lib -lf2c -lm
an error occurs
usr/bin/ld: cannot find -lgcc_s
Who can tell me the way to solve this error? The version of my system is 32bit Ubuntu 13.04, gcc 4.6. And I had searched the error,somebody said it maybe the error of symlink, but it has the link of /usr/lib/libgcc_s.so to /lib/i386-linux-gnu/libgcc_s.so.1 on my system. I also setted system variable LD_LIBRARY_PATH=/lib/i386-linux-gnu, but it did not work.
It is happening when I add -Bdynamic to the command,gcc compile successfully.The commands of compiling program with archive library and dynamic library are different.Maybe it is the explanation of "cannot find -lgcc_s"

mex files in 32-bit matlab on 64-bit Ubuntu

I am trying to compile object detection program from http://people.cs.uchicago.edu/~rbg/latent/, without any luck, however.
I installed 64-bit Ubuntu 12.10, 32-bit MATLAB 2012a. I also have gcc compiler
ivan#ubuntu:~$ gcc --version
gcc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Here is the error I get
EDU>> demo
compiling the code...
Warning: You are using gcc version "4.7.2-2ubuntu1)". The version
currently supported with MEX is "4.4.6".
For a list of currently supported compilers see:
http://www.mathworks.com/support/compilers/current_release/
/usr/bin/ld: cannot find crti.o: No such file or directory
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libstdc++.a when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lm
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgomp.so when searching for -lgomp
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgomp.a when searching for -lgomp
/usr/bin/ld: cannot find -lgomp
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: cannot find -lpthread
/usr/bin/ld: cannot find -lc
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: cannot find crtn.o: No such file or directory
collect2: error: ld returned 1 exit status
mex: link of ' "bin/fv_cache.mexglx"' failed.
Warning: Maybe you need to call fv_cache('unlock') first?
> In fv_compile at 50
In compile at 48
In demo at 5
Warning: You are using gcc version "4.7.2-2ubuntu1)". The version
currently supported with MEX is "4.4.6".
For a list of currently supported compilers see:
http://www.mathworks.com/support/compilers/current_release/
/usr/bin/ld: cannot find crti.o: No such file or directory
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libstdc++.a when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lm
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: cannot find -lpthread
/usr/bin/ld: cannot find -lc
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: cannot find crtn.o: No such file or directory
collect2: error: ld returned 1 exit status
mex: link of ' "bin/cascade.mexglx"' failed.
Error using mex (line 206)
Unable to complete successfully.
Error in cascade_compile (line 43)
eval(mexcmd);
Error in compile (line 50)
cascade_compile(opt, verb);
Error in demo (line 5)
compile;
I suspect it has something to do with missing 32-bit libraries. I am new to unix systems so I might not have included all the information you need to know.
Update:
New errors look like
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libstdc++.a when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgomp.so when searching for -lgomp
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgomp.a when searching for -lgomp
/usr/bin/ld: cannot find -lgomp
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
mex: link of ' "bin/fv_cache.mexglx"' failed.
I tried to create a symbolic link
sudo ln -s /usr/lib/i386-linux-gnu/libc.so.6 /usr/lib/libc.so.6
However it didn't solve the problem
Try looking for crti in your system first:
~$ sudo find /usr/ -name crti*
/usr/lib/x86_64-linux-gnu/crti.o
/usr/lib32/crti.o
If it's not there, or the lib32 version, install libc6-dev:
~$ sudo apt-get install libc6-dev
If it is there, try adding the library location:
LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LIBRARY_PATH
export LIBRARY_PATH
See also these posts in Ask Ubuntu and SO for relevant questions and help.