Leak Society - The Home Of Nulled Resources.
Forum Beta v1 Now Live!
Librarys - Explained [Part 1 of 2]
Thead Owner : Houga, Category : Everything Coding, 1 Comment, 56 Read
Viewers: 1 Guest(s)
Member
***
68
Messages
29
Threads
0
Rep
4 Years of Service
08-11-2014, 01:56 AM
#1
There are two types of libraries that I‟ll go over in detail: Dynamically linked, or „shared‟, libraries and Static Libraries.

Static Libraries (*.o [unlinked object], Linux: *.a , NT: *.lib):
Static libraries are incredibly simple. When a library is compiled as a shared library and is linked to another binary at compile-time, it becomes apart of that binary. Its functions can be used by the original program. Here is an example of a very simple static library:

Code:
//File saved as libfoo.c
void foo(int *i){
       *i = 500;
}

That's simple enough, now to compile Static libs.

gcc -c libfoo.c

The -c will compile the libfoo.c library template as an object as opposed to allowing it to be linked with standard libraries. At this point, you will have a “libfoo.o” file. Before we compile it, let‟s write a simple application that utilizes the foo() function we‟ve just written.

Code:
// File saved as foo.c
#include <stido.h>
void foo(int *i); //this is the prototype
int main(int argc, char **argv){
       int num = 100;
       printf("num = %d\n", num); // prints “num = 100”
       foo(&num); // exectes foo() in library
       printf("num = %d\n", num); // prints “num = 500”
       return 0;
} // When compiled, .o object code will be "inserted" at the end

This is quite simple. We see that it creates an integer, prints it, executes the foo() function by passing the memory address of the integer wherein the memory address value is set to 500, and prints the number again. At that time, it is 500. However, this is all theoretical until we compile and execute it:


Messages In This Thread
Librarys - Explained [Part 1 of 2] - by Houga - 08-11-2014, 01:56 AM

Forum Jump: