strange linker situation

This is a discussion on strange linker situation within the C Programming forums, part of the General Programming Boards category; It happened to me the following strange thing: I had a function1_1() in file1.c called by another routine in file2.c ...

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    strange linker situation

    It happened to me the following strange thing:

    I had a function1_1() in file1.c called by another routine in file2.c
    without it being declared extern inside file2.c (or any included header)
    Strangely enough the linker gave no error
    Plus I had another function still in file1.c function1_2() exactly the same as function1_1() with the only difference that this time I had
    Code:
    extern function1_2();
    inside file2.c

    If I a substitute (inside file2.c) the call of function1_1() with a call to function1_2() the program runs correctly
    while if I substitute the call of function1_2() with a call to function1_1() the program has an incorrect behaviour and i think i tested some memory areas erroneously overwritten (don't remember but guess some stack variables of function1_1() )

    Could you suggest an explanation??

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,157
    In C, if a function is called without being previously declared, the compiler assumes it is a function that accepts an arbitrary set of arguments and returns int (effectively the function as called is implicitly declared). This causes strange behaviours if the actual function (eg as implemented in another source file) does not match what the compiler assumes.

    The latest C standard has either deprecated or removed (I can't remember which offhand) this "feature". But older compilers will support it - and recent compilers might.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by mynickmynick View Post
    I had a function1_1() in file1.c called by another routine in file2.c
    without it being declared extern inside file2.c (or any included header)
    Strangely enough the linker gave no error
    C functions are all implicitly extern, unless you declare them static. Just an observation. If it's an included header, it's an included header, right?
    Last edited by MK27; 04-21-2009 at 07:25 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker problem... no idea
    By cyreon in forum C Programming
    Replies: 2
    Last Post: 04-03-2009, 02:53 PM
  2. linker
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2008, 12:25 AM
  3. Linker errors in VC++ 2005
    By C+/- in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2007, 07:42 AM
  4. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  5. Very strange linker error
    By hajohoff in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2005, 07:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21