what's the properly cmakelists.txt for libpq library - postgresql

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)

Related

/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.

Linking failure using cygwin GCC as matlab mex compiler

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!

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

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.