ld -shared success but got a segmentation fault when run - linux

I'm a beginner to learn the basic compiling. I create a very simple c file as below
#include <stdio.h>
int main (void) {
printf("Hello World!\n");
return 0;
}
then I run the commands,
cpp -E helloworld.c -o helloworld.i
gcc -S helloworld.i -o helloworld.s
as helloworld.s -o helloworld.o
ld -shared -o helloworld /usr/lib64/libc.so helloworld.o
in here, I got an error message like this,
ld -shared -o helloworld /usr/lib64/libc.so helloworld.o
ld: helloworld.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
ld: final link failed: Nonrepresentable section on output
so I rerun the upper but with a -fPIC
cpp -E helloworld.c -o helloworld.i
gcc -S helloworld.i -o helloworld.s -fPIC
as helloworld.s -o helloworld.o
ld -shared -o helloworld /usr/lib64/libc.so helloworld.o
at last, I got my helloworld, but when I try ./helloworld, I got a Segmentation fault.
anyone can help? Thanks in advance!

I try the gcc helloworld.c -Wl,--verbose | grep succeeded and get some output as below,
attempt to open /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o succeeded
attempt to open /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crti.o succeeded
attempt to open /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtbegin.o succeeded
attempt to open /tmp/ccSuIvy2.o succeeded
attempt to open /usr/lib/gcc/x86_64-redhat-linux/4.8.5/libgcc.a succeeded
attempt to open /usr/lib/gcc/x86_64-redhat-linux/4.8.5/libgcc_s.so succeeded
attempt to open /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/libc.so succeeded
attempt to open /lib64/libc.so.6 succeeded
attempt to open /usr/lib64/libc_nonshared.a succeeded
attempt to open /lib64/ld-linux-x86-64.so.2 succeeded
attempt to open /usr/lib/gcc/x86_64-redhat-linux/4.8.5/libgcc.a succeeded
attempt to open /usr/lib/gcc/x86_64-redhat-linux/4.8.5/libgcc_s.so succeeded
attempt to open /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtend.o succeeded
attempt to open /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crtn.o succeeded
but I still don't know how to do the link, I try this one
ld -shared -o helloworld /usr/lib64/libc.so /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/libgcc_s.so /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtend.o /usr/lib64/crtn.o helloworld.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/libgcc.a /usr/lib64/libc_nonshared.a /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o
but get an error message,
ld: /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o: relocation R_X86_64_32S against undefined symbol `__libc_csu_fini' can not be used when making a shared object; recompile with -fPIC
ld: final link failed: Nonrepresentable section on output

Related

Failing to compile C using GCC

I am trying to compile test.c as described this NanoPi Neo guide. However, when ever I try to run the gcc command to compile, I get this error message...
GNU ld (GNU Binutils for Ubuntu) 2.26.1
Supported emulations:
armelf_linux_eabi
armelfb_linux_eabi
/usr/lib/gcc/arm-linux-gnueabihf/5/../../../arm-linux-gnueabihf/crt1.o: In function `_start':
(.text+0x28): undefined reference to `main'
collect2: error: ld returned 1 exit status
...the command...
sudo gcc -Wall -o test.c -lwiringPi -lpthread -Wl,-V
...and the code I'm trying to compile...
#include <wiringPi.h>
int main(void)
{
wiringPiSetup() ;
pinMode (7, OUTPUT) ;
for(;;)
{
digitalWrite(7, HIGH) ;
delay (500) ;
digitalWrite(7, LOW) ;
delay (500) ;
}
}
May I guess that this is a possible linking issue? I'm just not sure and I really do know how to change it.
I am rather novice-like when it comes to topics like C, Linux and the like. :)
You have not specified any .c source files since the -o specifies the output as #user3386109 mentioned.
sudo gcc -Wall test.c -o output_file_name -lwiringPi -lpthread -Wl,-V

make "cannot find libraries"

I am trying to compile til parameter estimation tool PEST (http://www.pesthomepage.org/) for linux.
According to the PEST-manual I did:
make -f pest.mak all
but I get following error message:
gfortran -c -O3 -static pestdata.for
gfortran -c -O3 -static pest.for
gfortran -c -O3 -static pestsub2.for
gfortran -c -O3 -static writall.for
gfortran -c -O3 -static pardef.for
gfortran -c -O3 -static readpest.for
gfortran -c -O3 -static runpest.for
gfortran -static -o pest \
pest.o pestsub1.o pestsub2.o dercalc.o modrun.o writall.o \
linpos.o lapack1.o writsig.o common.o \
pgetcl.o pestwait.o writint.o pardef.o\
drealrd.o space.o optwt.o cgsolve.o compress.o \
readpest.o runpest.o lsqr.o orthog.o ms_stubs.o pestdata.o
/bin/ld: cannot find -lgfortran
/bin/ld: cannot find -lm
/bin/ld: cannot find -lquadmath
/bin/ld: cannot find -lm
/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
make: *** [pest] Error 1
After goggling I have tried different things, but without results. Can somebody please help
me?
Regards Tanja
Solutions to a problem shouldn't be hidden in comments, which is why I write this answer.
The make output shows that the Makefile tries to link statically.
That requires the libraries that can be linked statically, notably libm.a, libgfortran.a, libquadmath.a and libc.a
A possible solution is to remove the -static flag in the final step.
The resulting executable will of course need the shared objects (libm.so and friends) in order to be able to run. In most situations that's not a problem though.

cygwin : Linker doesn't find shared library

I am creating an exe and shared library in cygwin.
The library is created and is in the proper place, but when I try to compile the client code daemon, the linking phase says that it can't find the sysutil library.
Error is posted below:
/usr/lib/gcc/i686-pc-cygwin/5.4.0/../../../../i686-pc-cygwin/bin/ld: cannot find -lsysutil
collect2: error: ld returned 1 exit status
make: *** [Makefile:84: daemon] Error 1
I tried exporting the path using LD_LIBRARY_PATH but unfortunately that also didn't help.
daemon.c
#include <stdio.h>
#include <sys_util.h>
int main(){
sys_util();
while(1){
}
return 0;
}
sysutil.c
#include <stdio.h>
#include "sys_util.h"
int sys_util(){
return 0;
}
sysutil.h
int sys_util();
test.bat
g++ -fpic -c sysutil.c
g++ -shared -o libsysutil.so sysutil.o -I.
g++ -c daemon.c -I.
g++ -o daemon.exe daemon.o -L. -lsysutil
del *.o
Shared library is generated sysutil.so in the folder c:/test same as the source code (daemon.c,sysutil.c,sys_util.h,test.bat,libsysutil.so)
Cygwin console output:
/cygdrive/c/test
$ ./test.bat
C:\test>g++ -fpic -c sysutil.c
sysutil.c:1:0: warning: -fpic ignored for target (all code is position independent)
C:\test>g++ -shared -o libsysutil.so sysutil.o -I.
C:\test>g++ -c daemon.c -I.
C:\test>g++ -o daemon.exe daemon.o -L. -lsysutil
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -lsysutil
collect2.exe: error: ld returned 1 exit status
C:\test>del *.o
Cygwin expects shared libraries to have the .dll extension.
Change the second line in your batch file to:
g++ -shared -o sysutil.dll sysutil.o -I.
See the users' guide for more info:
https://cygwin.com/cygwin-ug-net/dll.html

How to build a user level program in parallel with building Linux kernel

I have a set of kernel source files in ~linux/fs/wrapfs/. Besides, there is also a source file present for a user level program. I want to build that user level program while the kernel gets compiled by running "make" in ~/linux folder i.e in the top level kernel directory.
So, I tried adding following in ~/linux/fs/wrapfs/Makefile:
EXTRA_CFLAGS += prog
prog:
gcc -Wall -Werror prog.c -o prog -lssl -lcrypto
But, then everytime I do "make" in ~/linux, I get following error:
gcc: prog: No such file or directory
I then tried changing it to following but nothing helped:
EXTRA_CFLAGS += prog
all:
gcc -Wall -Werror prog.c -o prog -lssl -lcrypto
EXTRA_CFLAGS += prog
prog:
gcc -Wall -Werror ./fs/wrapfs/prog.c -o ./fs/wrapfs/prog -lssl -lcrypto
Please suggest how can this be solved.
You should not be trying to pass prog in CFLAGS, extra or otherwise.

CUDA linker error: undefined reference to main

I'm trying to compile a CUDA program in Linux and I get the following linker error:
/usr/lib/gcc/x86_64-redhat-linux/4.4.4/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
This is my Makefile:
mtrand.o : mtrand/mtrand.cpp
nvcc -I"./mtrand" -O2 -c mtrand/mtrand.cpp
CUDAMCMLrng.o : CUDAMCMLrng.cu
nvcc --use_fast_math -O2 -c CUDAMCMLrng.cu
kernel.o : CUDAMCMLrng.o kernel.cu
nvcc --use_fast_math -O2 -c kernel.cu
main.o : mtrand.o CUDAMCMLrng.o kernel.o main.cu
nvcc --use_fast_math -O2 -Xcompiler "-fopenmp -Wall" -c main.cu
lab : main.o mtrand.o CUDAMCMLrng.o kernel.o
nvcc -lgomp -o lab main.o mtrand.o CUDAMCMLrng.o kernel.o
The main function is in the main.cu file but for some reason the linker is not seeing it.
Could anyone please tell me what am I doing wrong?
Thanks!
I recommend doing the final linking with g++ rather than nvcc. If you only have .o files and libraries as input to the link line, I see no reason to use nvcc.

Resources