GCC outputs ELF executable when I need a shared library

I am trying to create a shared library in Cygwin using the i686-elf cross compiler. The code is very simple:

int add(int a, int b) {
    return a + b;
}

void _init() {
    add(3, 4);
}

I am compiling the following command:

i686-elf-gcc -fPIC -shared -nostdlib core.c -o libcore.so

This should create a shared object, right? But GCC displays a warning that it is not possible to find a character _startthat is an entry point for executable files, and not for shared objects. Also readelfsays the following:

$ readelf -a libcore.so
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  ...
  Type:                              EXEC (Executable file)
  ...

What is wrong here?

+5
source share
2 answers

, i686-, . -Wl,-shared -, , ?

+2

, -shared no-op ( , ), . info gcc :

'-shared'
    Produce a shared object which can then be linked with other
    objects to form an executable.  Not all systems support this
    option.  For predictable results, you must also specify the same
    set of options that were used to generate code ('-fpic', '-fPIC',
    or model suboptions) when you specify this option.(1)

... :

  (1) On some systems, 'gcc -shared' needs to build supplementary stub
  code for constructors to work.  On multi-libbed systems, 'gcc -shared'
  must select the correct support libraries to link against.  Failing to
  supply the correct flags may lead to subtle defects.  Supplying them in
  cases where they are not necessary is innocuous.
0

All Articles