Linking failure using cygwin GCC as matlab mex compiler - matlab

I'm trying to compile a piece of code which requires linking to matlab libraries in cygwin.
g++ -o mezcaglue.dll -shared -Wl,--out-implib,mezcaglue.lib -I/cygdrive/e/Matlab/R2014b/extern/include -L/cygdrive/e/EPICS/labca_3_4_2/lib/cygwin-x86_64 /cygdrive/e/Matlab/R2014b/extern/lib/win64/microsoft/libut.lib /cygdrive/e/Matlab/R2014b/extern/lib/win64/microsoft/libmx.lib /cygdrive/e/Matlab/R2014b/extern/lib/win64/microsoft/libmex.lib -m64 ini.o multiEzca.o ctrlC-polled.o mglue.o -lezcamt -lpthread -lm
I'm positive that all the *.o and *.lib files are in place, and yet I get errors like these.
/cygdrive/e/EPICS/labca_3_4_2/glue/O.cygwin-x86_64/../multiEzca.c:876: undefined reference to `mxMalloc'
/cygdrive/e/EPICS/labca_3_4_2/glue/O.cygwin-x86_64/../multiEzca.c:894: undefined reference to `mxFree'
multiEzca.o: In function `nativeType':
/cygdrive/e/EPICS/labca_3_4_2/glue/O.cygwin-x86_64/../multiEzca.c:382: undefined reference to `ca_field_type'
multiEzca.o: In function `multi_ezca_ts_cvt':
/cygdrive/e/EPICS/labca_3_4_2/glue/O.cygwin-x86_64/../multiEzca.c:294: undefined reference to `epicsTimeToTimespec'
multiEzca.o: In function `ezcaSetSeverityWarnLevel':
/cygdrive/e/EPICS/labca_3_4_2/glue/O.cygwin-x86_64/../multiEzca.c:1022: undefined reference to `mexPrintf'
I'm new to these gcc g++ cygwin stuffs so any help will be appreciated.
Thanks in advance!

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)

/usr/bin/ld: cannot find -lmat while importing MAT-file in fortran with fintrf.h

I try to compile a matlab "fintrf.h" based fortran code with gfortran to import a MAT-file in a fortran program. Here is the compiling commands:
gfortran -mcmodel=medium -Ofast $1.F90 -I/mnt/f/MATLAB/extern/include/ -L/mnt/f/MATLAB/bin/win64/ -cpp -lmat -lmx -Wl,-rpath /mnt/f/MATLAB/bin/win64/ -o $1.exe
And I am given these errors:
/usr/bin/ld: cannot find -lmat
/usr/bin/ld: cannot find -lmx
but I am sure the "libmat.dll" and "libmex.dll" is in "../win64/" dictionary. Can you help me out? Thanks
You want -L to point to the directory containing the corresponding .lib files.
So something like:
gfortran ... -L/mnt/f/MATLAB/extern/lib/win64/mingw64 -lmat -lmx ...

Linking with GCC doesn't detect -fPIC flag

I am trying to link some files. Here is my command:
gcc -T linker.ld -o Stack\ Berry.bin -ffreestanding -O2 -nostlib kernel.o boot.o -fPIC -lgcc
How ever, I get this error:
/usr/bin/ld: boot.o: relocation R_X86_64_32 against `.multiboot' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
I have included the -fPIC argument, have I just put it in the wrong place? I have tried putting the argument in where it compiles but no luck. I haven't been able to find any thing for the syntax for this flag. Thanks!
You are passing -fPIC to your linkage command. It is a compiler option,
because you need to compile your source code to Position Independent object files.
Remove it from this command and add it to your compilation commands.
(-O2 is likewise a compiler, not linker, option.)

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.

Interfacing to SQLite

I am attempting to interface with a SQLite. In my project I have sqlite3.c and 6_1.cpp (my main source file).
When I attempt to compile, I receive many errors that are similar to this error: undefined reference to `dlerror'. Based on what I've read this error indicates the gcc compiler isn't properly linked to certain libraries. However, the errors I receive occur within sqlite3.c, not 6_1.cpp. Has anyone else had this issue?
To build your program, use the following commands:
gcc -c sqlite3.c
g++ -c 6_1.cpp
gcc -o 6_1 6_1.o sqlite3.o -ldl -lpthread
You should read up on make.