Find the answer to your Linux question:
Results 1 to 7 of 7
Hey all When I have a simple program: #include <stdio.h> int main() { printf("Hello world\n"); return 0; } How does GCC know to link whatever libraries are needed when I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2005
    Posts
    5

    Compiling & Linking


    Hey all

    When I have a simple program:

    #include <stdio.h>
    int main() {
    printf("Hello world\n");
    return 0;
    }

    How does GCC know to link whatever libraries are needed when I include stdio.h? I tried copying /usr/include/stdio.h to a file, and then ammending the above program (without the #include path) to the end of the newly created file, and found it compiled and ran well.

    As far as I can tell, that means that either stdio.h contains the full source code for printf, etc, or somehow GCC can tell from the contents of stdio.h which libraries it needs to link. I'm assuming it must be the second, since the header file should only contain declarations, and not actual code...

    Anyway, searching a bit further, I decided the above theory must be correct:

    extern int printf (__const char *__restrict __format, ...);
    int main() {
    printf("Hello world\n");
    return 0;
    }

    That program compiled and ran without a problem. So, I'm assuming that when GCC compiles a program, if it sees certain function declarations, it'll include the correct library. What happens if I want my own function named printf? How does this work, etc.

    Can anyone give me a link to some good reading material - I've tried Google and got some interesting results, but I'm still somewhat confused (as you can no doubt tell by this post!)

    Thanks!

  2. #2
    Linux Guru kkubasik's Avatar
    Join Date
    Mar 2004
    Location
    Lat: 39:03:51N Lon: 77:14:37W
    Posts
    2,396
    not an exact find, but an overall good resource for C is www.cprogramming.com but skip past their beginner stuff, its a little elementary and not perfect. However the more advanced examples and tutorials are nice, if not completly comprehensive.
    Avoid the Gates of Hell. Use Linux
    A Penny for your Thoughts

    Formerly Known as qub333

  3. #3
    Just Joined!
    Join Date
    Nov 2004
    Posts
    47
    try:

    Code:
    ldd ./a.out
    at your command prompt and you will find out which libraies are linked with your program.

    You can
    Code:
    man ldconfig
    to know more

  4. #4
    Just Joined!
    Join Date
    May 2005
    Posts
    5
    Thanks for the replies.

    I'm currently reading through www.cprogramming.com...

    sybvn:

    I've read the manpage for ldd, ld.so and ldconfig (they're all unusually short for unix man pages). I'm getting a better idea on how the linking process works, but I'm still somewhat unsure how the linker decides which libraries to link when you run GCC.

    Is there a file (possibly /etc/ld.so.cache?) that tells the linker which libraries to link for each C system call? I took a look thruogh /etc/ld.so.cache though, and could only find locations for libraries, and no references to system calls.

    Many thanks

  5. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    For every unresolved symbol in your source code (ie symbol which does not
    get defined in your source code) the linker searches the files in some
    standard directory for that symbol.I'm not sure in which order the files
    get searched but I suspect it is in the order they appear in the directory.When
    it finds a symbol with the correct name it does the linking.To define your
    own function named printf you would need to define it in your source code.
    (I'm not 100% percent about this so best to perform a little experiment.If
    you do please report your findings ) In other words you would need to
    provide the code for the function inside your code.Another solution is to
    create your own library containing a function named printf and instruct the
    linker to look first in your own library before looking in the standard
    libraries.You do that using the -L and -l options. ( Check the gcc manual )
    Check also the man pages for ranlib and nm.

  6. #6
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    By the way don't confuse run-time linking with compile time linking.
    When you execute a programme, ld.so determines which libraries
    your programme needs and loads them to the memory from the disk.That's
    run-time linking.What I mentioned in the post above is concerned with
    compile time linking.

  7. #7
    Just Joined!
    Join Date
    May 2005
    Posts
    5
    Santa's little helper:

    Thanks, that's exactly what I wanted to know.

    > (I'm not 100% percent about this so best to perform a little experiment.If
    > you do please report your findings )

    Sure thing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •