Linking with GCC doesn't detect -fPIC flag - ld

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

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)

How to enable ompi with MATLAB?

I have C code using OpenMP what I want to include in MATLAB as a mex function. I have already done it and works perfectly in MATLAB R2013b under Linux. However, I recently read about OMPi and according to this article it beats most of the commercial compilers, mainly gcc what I currently use. Therefore, I would like make use of it. It works well for C codes, but I cannot make it work with a mex function. I tried it four ways:
Edited the CC='gcc' entry in the mexopts.sh file to CC='ompicc' and called mex
Directly called ompi as ompicc -k -I/opt/MATLAB/R2013b/extern/include mex_sum_openmp.c
Setting the verbose flag for mex, I obtained information about what mex is doing, so I tried to mimic it: ompicc -k -I/opt/MATLAB/R2013b/extern/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread -DMX_COMPAT_32 -O -DNDEBUG mex_sum_openmp.c
Since ompi is a source-to-source compiler, I compiled the mex_sum_openmp.c file with ompicc -k mex_sum_openmp.c to mex_sum_openmp_ompi.c and then called gcc on it: gcc -I/opt/MATLAB/R2013b/extern/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread -DMX_COMPAT_32 -O -DNDEBUG mex_sum_openmp_ompi.c (this is what ompi does without the -k flag: first creates the source compiled file and then calls a regular compiler on it)
Either way, the result is the same, a bunch of warnings and then error messages.
Here is the original C file: http://s000.tinyupload.com/?file_id=70221693843831744745, here the source compiled file :http://s000.tinyupload.com/?file_id=02497191022856307556 and here are the warnings and errors: http://s000.tinyupload.com/?file_id=93602974698898858427
Thanks

redhat: netbeans: g++: compiler options applied to linker

I am using NetBeans under RedHat. I have a couple of open projects that combine together to make a command line executable and a shared object "plugin". There are two static libraries in the mix as well. That is all fine and good.
I just noticed that when I "clean and build" the executable and the shared object that the compiler options are also expressed on the linker line. a la:
g++ -O2 -Wall -c -fmessage-length=0 -c -g -Werror -DDEBUG -DDEBUG_2 -I/usr/include -I/usr/include/c++/4.4.7/x86_64-redhat-linux -I/usr/include/c++/4.4.7 -I../ITGUtilities -MMD -MP -MF "build/Debug/GNU-Linux-x86/LocalCache.o.d" -o build/Debug/GNU-Linux-x86/LocalCache.o LocalCache.cpp
then later in the compile line (edited for brevity):
g++ -O2 -Wall -c -fmessage-length=0 -fPIC -o dist/Debug/GNU-Linux-x86/pam_plugin.so build/Debug/GNU-Linux-x86/pam_plugin.o -L... -lpam -l... -shared -fPIC
g++: build/Debug/GNU-Linux-x86/pam_plugin.o: linker input file unused because linking not done
I think I have a fairly generic set of project configs. I did set the -O2 -Wall -c -fmessage-length=0 compiler options but there are no options supplied for the linker other than whatever defaults NetBeans applies.
For the executable and plugin projects I can go to Properties > Linker > Additional Options and expand the [...] to see what NB thinks it will apply. That does not show the compile options but doing an actual build does apply them.
It is not doing the link step and I am assuming that is because of the compiler options. This is weird.
ideas?
Do not add -c option to the compiler options which says not to run the linker. It will be added as needed when just compiling anyway.

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!

Compiling a dll with mingw and eclipse

I want to write a tool to capture and visualize pressed keys in a specific application so I searched for a sample source.
My result was this:
http://www.codeguru.com/cpp/w-p/system/keyboard/article.php/c5699
But it doesn't work yet. Here's my approach:
I have imported the sources as makefile project in Elipse (Helios, CDT Version 7.0.0.201006141710) using Mingw 4.6.1 as toolchain.
In keydll3.cpp I added the line
#define KEYDLL3_EXPORTS
to tell the preprocessor that i want to export the dll functions.
Now when I try to compile the project, the following errors occour:
**** Internal Builder is used for build ****
g++ -shared -DBUILDING_EXAMPLE_DLL -IC:\MinGW\include -IC:\MinGW\lib\gcc\mingw32\4.6.1\include\c++ -O2 -g -Wall -c -fmessage-length=0 -oStdAfx.o ..\StdAfx.cpp
g++ -shared -DBUILDING_EXAMPLE_DLL -IC:\MinGW\include -IC:\MinGW\lib\gcc\mingw32\4.6.1\include\c++ -O2 -g -Wall -c -fmessage-length=0 -okeydll3.o ..\keydll3.cpp
..\keydll3.cpp:31:0: warning: ignoring #pragma data_seg [-Wunknown-pragmas]
..\keydll3.cpp:34:0: warning: ignoring #pragma data_seg [-Wunknown-pragmas]
..\keydll3.cpp:36:0: warning: ignoring #pragma comment [-Wunknown-pragmas]
g++ -okeydll3 keydll3.o StdAfx.o
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../libmingw32.a(main.o): In function `main':
C:\MinGW\msys\1.0\src\mingwrt/../mingw/main.c:73: undefined reference to `WinMain#16'
collect2: ld returned 1 exit status
Build error occurred, build is stopped
It seems that the compiler misses the winmain statement because he assume it's a windows application. But a dll isn't.
It also seems that the "-share" option has no effect.
So how do I tell the compiler that my code is a dll with some Windows API calls?
If there's another example which works without visual studio let me know.
Thanks in advance for your contributions.
Noir
You've added the -shared option in the wrong place. It needs to be added to the linker flags, not to the compiler flags. Your commands should look like this.
g++ -DBUILDING_EXAMPLE_DLL -IC:\MinGW\include -IC:\MinGW\lib\gcc\mingw32\4.6.1\include\c++ -O2 -g -Wall -c -fmessage-length=0 -oStdAfx.o ..\StdAfx.cpp
g++ -DBUILDING_EXAMPLE_DLL -IC:\MinGW\include -IC:\MinGW\lib\gcc\mingw32\4.6.1\include\c++ -O2 -g -Wall -c -fmessage-length=0 -okeydll3.o ..\keydll3.cpp
g++ -shared -okeydll3 keydll3.o StdAfx.o