study of hacking_linux(day2)

Today i will discuss something around object file and linking system in c programming.

In c programming,when you want to build a big project,you cannot write everything from scratch,it is impossible to make everything your own or keep all the boring code and write them all the time,hence,something called "header" should be involved in order to reduce the repeated coding part and relax programmers' mind.

OK,let us getting into the topic.

1.Suppose i want to write a data structure around stack manipulation where i will use a defined char type array to hold amount of characters and an integer to hold the index of that arrays' location,and there will be couple of characters to push into the array and pop it up using a pop() method,in the end,print out all the characters on to the screen.

2.Before i show you the code,i'd like to share some of the necessary key knowledges with you.

First,in this case,stack or the array is like a list from up to down,user push a character into the stack and label it as index 0,then push the next character into this stack and label it as index 1,so on so forth.when user want to get a character from such list,a method called pop() will be utilized.when user called "pop()",it will get the first character that system can find in the most recently past it has been pushed in,therefore the last character pushed into the stack will be the first one to get out.It is something called "LIFO"(last in first out).

Second,i need to make it clear that something around compiling with linking and object file.Before you can run the binary file you need to compile the source code where in most of the case it is ended with *.c extension.You can either straight use the following command "gcc src_code.c -g -o binary_file" or you can first compile it as "gcc -c src_code.c" to get the object file then "gcc -o obj_file.o " to get the final executable binary file.

3.Ok,we now know what is executable binary and what is source code file also what can you do and how it works around compiling and linking.But in most of the cases,we won't just have couple of simple source code files,rather we would have a large amount of separated files such as user_defined header files,user_defined source code or some function declaration.Usually in a project you cannot just put all the source code into a single file and willing to run it in one shot.Despite the inconvenience of checking with each individual function or declaration,you will find it is bored to write the same code over and over again just because there is no such way to replace the frequently-used part of code(header file).SO we gotta to find a way out to make programming in an easy way.

4.SO the method is to use header files and separated *.c file to declare different functions and build up the content of those functions.The key is "header file,'extern' keyword,object linking and static and shared library".

5.Now let's find out what are those things that mentioned in the last paragraph.

Header file:It is ended with *.h extension and the content would be some function declaration.

Separated *.c: It is the specific content of *.h files which will be the detail implementation of functions.

"extern" keyword: It is used to declare a variable and user can use it as a local coding part where the variable has already been defined in another separated *.c file,that is why it is called "extern".

Object linking:You need to output the object file first later you can use GNU linker to link all the object files and output the final binary file.

Static and shared library:In many cases,it is highly time consumed when you want to use GNU linker "ld" to link all the necessary object files and output the binary file,especially those object files are frequently-used in your proejct.So people defined something called "library" to put all the necessary linking files into one library file.Normally when you write your source code,they very top line is started with "#include <stdio.h>",you have to include necessary header files in order to successfully compile your source code file.Sometimes a user defined header file you will be used and the content of its declaration will be involved with many separated *.c files,you may find it is hard to remember all the *.c separated files name or it is rather bored to type them all the time just because it is high frequently used in many cases.So you have to bundle all the *.c files into library files as what i have mentioned above.

6.Ok,now let's take a look an example:

A. Suppose we want to write a simple application to simulate data structure as stack and we have these files in the current folder and the source files are main.c and stack.c:


B. We can then use "gcc -c stack.c " to build an object file of stack.c which would be stack.o

C. Later we can compile source code main.c and link the object file stack.o then create the final binary file

"gcc main.c stack.o -g -o main"

7. Let's take a look of another example which is a little bit complex than above(static and shared library):

Suppose we have this:


Before we move on,there is something we need make it clear(difference between static and shared library):

In the situation we use shared library,it only supports the dynamic linker and necessary lib file,no actual linking process,the binary executable will only call the lib function dynamically when program gets running,it was only undefined symbol that is existed in the binary executable.

Whereas the static library,when we use it,the linker will get the object file from static lib and link it with binary executable,when we run it,it gets running straightly.

Difference and advantage:

In the static lib situation,you may run your program more faster than shared lib,whereas the shared lib will support flexibility and save more disk space,because if you put all the lib files and binary altogether,the ending file will be very large,and when we use shared lib,the binary executable only use lib when it gets running,and the binary itself is very small in many cases.

OK,we are getting done of this article,before i go,i think i should discuss something about how to make static or shared lib.

static lib: gcc -c object.c object2.c object3.c -> object.o object2.o object3.o;ar rs libsth.a object.o object2.o object3.o -> libsth.a;gcc main.c -L. -lsth -Iheader -o main

shared lib(-fPIC position independent code): gcc -c -fPICobject.c object2.c object3.c -> object.o object2.o object3.o;gcc -shared -o object.o object2.o object3.o;gcc main.c -g -L. -lsth -Iheader -o main

See you next time,have a nice life.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章