0

For some reason I have to link glibc manually. I am trying to run the following program:

#include <stdio.h>
int _start(){
    printf("ABCDE");
    return 0;
}

In order to compile it I type the following commands:

gcc -c main.c -o main.o
gcc -L/lib/x86_64-linux-gnu/ -nostdlib main.o -o main -lc

Unfortunately, after running ./main i get only Segmentation fault (core dumped)

Could anyone tell mi what i am doing wrong?

nkdm
  • 1,220
  • 1
  • 11
  • 26
  • Are you trying to override the rt-startup ? because your id is [reserved](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – WhozCraig Oct 13 '12 at 17:52
  • You have to link `-lc` manually because `-nostdlib` disables it. –  Oct 13 '12 at 17:59

1 Answers1

2

an _exit(0); should do the trick.

However, what are you trying to achieve? Sample

जलजनक
  • 3,072
  • 2
  • 24
  • 30
  • Thank you very much! I have to write an application that will be running on an old kernel with old glibc version. I had find out if the linking will work correctly. – nkdm Oct 13 '12 at 19:00
  • @nkdm The approach you've tried here is *exceedingly* unlikely to work on an older kernel once your program becomes slightly more complicated. You should probably read these answers: http://stackoverflow.com/a/2079757/50617 http://stackoverflow.com/a/8658468/50617 – Employed Russian Oct 13 '12 at 23:02
  • I had compiler that was dynamically linking GLIBC 2.15, but on target system i had GLIBC 2.4 that was incompatible. I thought that linking with '--static' option would take to much memory, but fortunately it was acceptable. – nkdm Oct 20 '12 at 18:06