Wrong linkage of system functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?ISO-8859-1?Q?Crist=F3v=E3o_Sousa?=

    Wrong linkage of system functions

    Hi,
    I'm having some trouble with a C project in a Linux system.
    I already know which is the problem but I don't know how to fix it.

    I have a program that links to a third party static library.
    The problem is that the library declares a internal global variable
    named 'accept' not visible by the user.
    In my program I use the system Internet Sockets which API have a
    function named 'accept'.
    When I compile it does not complies but when I run the program it
    segfaults because a call to Internet Sockets 'accept' function tries
    to execute a function which address is the address of the 'accept'
    global variable from the third party library.

    I made a little source code that reproduce the problem with the
    'printf' function. In this case the compiler gives me a warning, but
    the beavior is equal.

    Is this my problem? Is it a bug? I don't think a programer have to
    know all possible system functions names...


    I have three files:
    mylib.c and mylib.h represents the third party library;
    main.c is my program.
    I also made a simple Makefile.

    Here are the files:

    ---------------------------------------
    $ cat mylib.h
    int myfunc();

    ---------------------------------------
    $ cat mylib.c
    #include "mylib.h"

    int printf[2];

    int myfunc(){ // Just for example
    printf[0] = 0;
    return printf[1];
    }

    ---------------------------------------
    $ cat main.c
    #include <stdio.h>
    #include "mylib.h"

    int main(){
    printf("Helo World!");
    return 0;
    }

    ---------------------------------------
    $ cat Makefile
    exec: main mylib
    gcc main.o mylib.o -o exec

    main: main.c
    gcc main.c -c -o main.o

    mylib: mylib.c
    gcc mylib.c -c -o mylib.o

    ---------------------------------------------------


    And here are the results:


    $ make
    gcc main.c -c -o main.o
    gcc mylib.c -c -o mylib.o
    mylib.c:3: warning: built-in function 'printf' declared as non-
    function
    gcc main.o mylib.o -o exec

    $ ./exec
    Segmentation fault (core dumped)

    $ gdb ./exec core
    Core was generated by `./exec'.
    Program terminated with signal 11, Segmentation fault.
    #0 0x0000000000600 884 in printf ()



    Thanks in advance,

    Cristóvão Sousa
    ISR - FCT - University of Coimbra
Working...