470,870 Members | 1,035 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

Wrong linkage of system functions

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-
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 0x0000000000600884 in printf ()

Thanks in advance,

Cristóvão Sousa
ISR - FCT - University of Coimbra
Mar 5 '08 #1
0 978

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by qazmlp | last post: by
4 posts views Thread by Nimmi Srivastav | last post: by
47 posts views Thread by Richard Hayden | last post: by
10 posts views Thread by Mark A. Gibbs | last post: by
3 posts views Thread by al.cpwn | last post: by
5 posts views Thread by Your Uncle | last post: by
15 posts views Thread by robert maas, see http://tinyurl.com/uh3t | last post: by
2 posts views Thread by Nagrik | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.