Jump to content

Non-standard C libraries?

ahuckphin

I am trying to understand C libraries and using libraries to expand on a C program functionality. Is libraries a matter of writing a line like this 

#include <stdio.h> 

at the beginning and then when I want to use the library, I write a line of code like this:

nameOfLibrary()

for something like this

#include <temperatureLibrary> 

...

int temperature;

temperature = temperature London; 

?

 

Or is there more to it - will there be separate file the library depends on that I'll need to establish a link between it and my program? 

 

Or is my weak understanding thus far completely off the rails? 

 

 

 

 

 

Edited by ahuckphin
added more details
Link to comment
Share on other sites

Link to post
Share on other sites

For the STD libs and most system libraries its a simple matter of including the appropriate header files which contain the declarations for that functionality, then you just call the functions you need. There is no nameOfLibary or anything like that, you just have to make function calls. Some libraries might have initialization routines depending on what they do so you would need to call a specific function before using any others but that depends on you solely reading the docs for that lib. But regardless if a library is standard or non standard you just call functions. The only thing that changes is the build process.

 

For non standard libraries you have to include the header files like normal but then you also have to tell your compiler where it can find the .libs that allow for linking the library into your executable. These libs contain the actual compiled symbols for the library. These symbols then get used by other object files that are looking for those symbols (your code) before being linked into an executable. 

 

I suggest you read this SO post on the subject of compilation as it will help you grasp how libraries fit into the picture. https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

12 hours ago, ahuckphin said:

Or is my weak understanding thus far completely off the rails? 

 

Yes.

  • C has header-only libraries that would work almost as you described; most libraries come with a combination of a header and a library though (unless you compile the library into your project, which is usually possible but makes a giant mess).
     
  • temperature = temperature London;

    This is - at best - invalid code. 🙂
    (I guess it could be made valid code with a bunch of #defines, but you really should not try.)

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

23 hours ago, ahuckphin said:
#include <temperatureLibrary> 

...

int temperature;

temperature = temperature London; 

...you should try to understand the core syntax of C better before looking into libraries.

 

a library can be very roughly defined as a collection of preexisting code that you can reference and thus use in your program. typing

#include <stdio.h>

does not magically activate some hidden functionality; it just tells the compiler that it should consider the standard C library when compiling your code. On a system that doesn't have the standard C library installed or referenced in the right environment variables this just won't work.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×