473,395 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Thanks in advance,

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: qazmlp | last post by:
const has internal linkage in C++, but external linkage in C. Am I right ? But, linker reports multiply-defined error if the following header is included in multiple .cpp files. //...
4
by: Nimmi Srivastav | last post by:
Once and for all can someone kindly tell me the difference between C and C++ linkage. I thought I understood it till someone showed me the other day that C functions, that would ordinarily require...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
10
by: Sune | last post by:
Hi, previously I used Eclipse CDT for compiling my files just to get started with C and leave C++ behind. Now it's time to get a little more serious so I've moved my files to a new workplace and...
3
by: al.cpwn | last post by:
do static and inline functions or members have internal linkage? I have been reading this newsgroup on google and found conflicting ideas. Can someone please help me understand why in some places...
5
by: Your Uncle | last post by:
I'm given to understand that your standard is specific regarding linking to c. Furthermore I'm going to speculate that there exists a c++ version of Keith Thompson, brilliant, encyclopedic and...
15
by: robert maas, see http://tinyurl.com/uh3t | last post by:
Here's the source: #include <stdio.h> #include <errno.h> main () { char* str = "9999999999"; long long int llin; char* endptr; /* Set by strtoll */ int nch; errno = 0; llin = strtoll(str,...
2
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.