Thread: Can anyone explain what are libraries in C?

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    34

    Smile Can anyone explain what are libraries in C?

    Here is a screenshot of my Pelles C directory!

    Can anyone explain what are libraries in C?-capture-png

    Here is another screenshot of my Library folder in Pelles C

    Can anyone explain what are libraries in C?-capture2-png

    So are they machine code?

    If so then in which lib file printf and scanf compiled versions are kept?

    I tried opening a lib file delayimp.lib with Vim and this was the result!
    Can anyone explain what are libraries in C?-capture3-png
    Last edited by ranadas; 11-28-2017 at 06:09 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,671
    For you, they will be in crt.lib.

    They are precompiled by your compiler vendor, usually from C source and possibly a few bits of assembler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    99
    Here is a link to be visited about libraries. There are more to find if you want to.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,747
    Normally static libraries in C are a group of object files.
    Object files are what the C Compiler outputs
    Object files are what the C Linker combines together to make the executable file.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    34

    Talking

    Quote Originally Posted by stahta01 View Post
    Normally static libraries in C are a group of object files.
    Object files are what the C Compiler outputs
    Object files are what the C Linker combines together to make the executable file.

    Tim S.
    Ok So can I make a library file for myself and later on use it for m own personal use??

    If possible then how?
    Last edited by ranadas; 11-29-2017 at 12:41 AM.

  6. #6
    Registered User
    Join Date
    Nov 2017
    Posts
    34
    ok so if I delete crt.lib printf() and scanf() won't work anymore right?? let's try it

  7. #7
    Registered User
    Join Date
    Nov 2017
    Posts
    34
    Furthermore are library files automatically included or if I specify <stdio.h> then the library file for that particular header file is included?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    No, actually, all #include will do is paste the contents of stdio.h in your file, so that you can compile your new code all of the function signatures and stuff pasted in. The compiler knows where to look for the stdio.h file because the < > part of the include preprocessor directive means something like "look in my default directories" for missing headers. Usually your environment is set up such that standard libraries have their path specified, and therefore including them calls for little effort on your part. There are other ways to use the include directive, including with quotes, which basically means "look in the current working directory" for missing files. You can also specify additional directories to search using compiler/environment options, if you want.

    It is the linker's job to find certain precompiled libraries (and other compiled source) in their folders, and then make an executable file out of it. These are set up in much the same way that your headers are in your environment. Although I will admit you have to specify which libraries you are linking with more often. In a typical compile from the command line, I might write:

    gcc -Wall -std=c99 -lm src/*.c -o program.exe

    which would compile and link the entire src directory with the math library.

    Usually if you don't specify which object files that you need, and they aren't part of the standard library, the linker just errors out.

    GUI IDEs and such have made this easier as well, but you can't always escape tweaking options for a build.
    Last edited by whiteflags; 11-29-2017 at 04:08 AM.

  9. #9
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    Ok So can I make a library file for myself and later on use it for m own personal use??
    You can make a header file and include it in future projects. The header file may have useful functions you would not have to define in future files but could still use.

    Furthermore are library files automatically included or if I specify <stdio.h> then the library file for that particular header file is included?
    Libraries are only included if include directive is in the file.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,480
    Libraries are only included if include directive is in the file.
    Libraries are never #included. Header files are not Libraries, they just define the functions that are contained in some specific Library.

    For non-standard libraries you would need to add the proper "switches" to your build line or IDE to tell the linker what libraries are required for this "project" and where those libraries are located.

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    In other words, the header files are there for the compiler to know how to combine the functions with everything else. In order for code from a library to be included though, you need to actually use one of its functions. When you use a library function, the compiler asks from the linker to provide it, be it copy-pasted from a static library or a link to a shared library.

    It's the message you get when you declare but forget to define a function:
    Undefined reference to @FooBar
    The compiler asked for FooBar but the linker couldn't find it anywhere.
    Devoted my life to programming...

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by jimblumberg View Post
    they just define the functions that are contained in some specific Library.
    Incorrect. Header files generally declare functions that exist in some library. If they were defined in the header, and you had multiple source files that included that header, you'd end up with multiple definitions of those functions, and it would fail to build.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,480
    Incorrect. Header files generally declare functions that exist in some library.
    Yes, you are correct. I quite often switch the terms inadvertently, thanks for the correction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using libraries with c
    By errigour in forum C Programming
    Replies: 3
    Last Post: 07-01-2012, 06:24 PM
  2. Libraries
    By kiknewbie in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 01:37 AM
  3. AIM C Libraries
    By _Cl0wn_ in forum C Programming
    Replies: 0
    Last Post: 05-29-2003, 11:06 AM
  4. .so libraries
    By Skarr in forum Linux Programming
    Replies: 4
    Last Post: 11-10-2002, 01:35 AM
  5. Libraries!!
    By Paninaro in forum C Programming
    Replies: 1
    Last Post: 06-20-2002, 12:07 AM

Tags for this Thread