Monday, December 16, 2019

C Programming Dynamic Library

Dynamic Library


What is a Dynamic Library?

Dynamic libraries consist of separate files containing separate pieces of object code. To form a single piece of object code these files are dynamically linked together. Also, dynamic libraries contain extra information that the operating system will require to link the library to other programs in the system.
for more information on static libraries check out my previous blog at https://howtols.blogspot.com/2019/10/c-programming-static-library.html

Why should we use them?

Only one copy of the shared library is kept in memory, making it much faster to compile programs and significantly reducing the size of the executable program also, its load time will be reduced if the shared library code is already present in memory.

A disadvantage compared to a static library is that dynamic libraries have a slower execution time compared to static libraries and potential compatibility issues if a library is changed without recompiling the library into memory.

How to create a dynamic library?

Okay, suppose we have a directory full of files written in c like this:




After we created out files we will compile them using the (GCC) command on the command prompt. 
The GNU Compiler Collection or GCC) is a compiler system produced by the GNU Project supporting various programming languages. For more information on how to use gcc, type the command (man gcc) in your command prompt or terminal. This information is also mentioned in my Static Libraries blog.

next step is to compile all the c files into object files with this line: gcc -fPIC -c *.c







Now that we have made our c files into object files we type this command: gcc *.o -shared -o liball.so

We are telling our computer to compile all the obeject files onto a Dynamic Library. Using the (-shared) flag will make the library dynamic and the naming convention for dynamic libraries is such that each shared library name must start with (lib) and end with (.so).

And it looks like this:











And finally we end up using the command : 
(export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH)
You can see the result of this code by using the (printenv) command in the command line.

Sunday, November 24, 2019

What happens when ls-l is executed?

What happens when ls-l is executed in your command line?



Before we dive into what this Unix command do, lets first find out what the shell is. The shell is a command line interpreter or also known as "shell",  that provides a command line user interface for Unix operating systems. The shell is an interactive command language and a scripting language, used by operating systems to control execution of shell scripts. Think of the shell as you would normally interact with a Windows or MAC, the only difference being that instead of clicking and scrolling to navigate around, you use commands to do the same thing. The simplest way to describe shell is, a program that employs commands.
Now you are probably wondering, what is a shell script? well, a shell script is a program designed to be run using a Unix like operation system. The uses for a shell script are file manipulation, program execution, and printing text. The term is also used more generally to mean the automated mode of running an operating system shell.
This is what it looks like 


There are different types of shell. For example: 

  • The KornShell (ksh).
  • The Bourne Shell (sh, and oldest shell in use).
  • C Shell (csh).
  • Bash (bash).
  • Remote Shell (rsh).
  • Secure Shell (ssh).
  • Almquist Shell (ash).
  • Power Shell (msh).
  • Z Shell (zsh).
If you want to know more information about these types of shell, use google!


Differences between a Graphic user interface and a command user interface.


As we explained earlier, the main difference between a Graphic User Interface and a Command User Interface is the fact that with one you use the mouse and keyboard to navigate in your computer and the other one uses a command prompt where you will input commands to navigate. 

They look like this: 
 
On the left is a visual way of interacting with a computer using items such as windows, icons, and menus, used by most modern operating systems, and on the right we use commands to interact and navigate withing the system in your computer. Visually interacting with windows involve clicking on a folder to open it, while on the command line we use the command ls to display all files in the working directory. 

Inside the Shell

Now that we know how the shell works, lets take a look on how it makes commands and processes happen. As the user, every time you type a command , you hit the enter key. This will tell your computer that an action is being processed and then the computer returns in the output the result of the command you entered. For example: 

When you type the ls command in your terminal, the system will look for it in all its archives until it finds it and then returns an output that displays all files in your working directory. Like this:


But that is not all there is to it. When we type ls with the option -l the system recognize it like this 
ls -l\n. The \n represent a new line in the command line. The system then will replace the \n with a \0 which represents a NULL terminator for the command. This lets the system know where the command ends and how many of them there are. Then the system uses the function strtok() to parse  the command line and  to an array of strings and separating ls and -l into individual strings. Last both of the strings will be concatenated for execution. 

THE PATH 

But how does it find where this command is located or even what it is? The answer lies in PATH.
PATH is an environmental variable that tells the shell in which directories to look for executable files in response to a command input by the user.  Most commands users will enter when operating a shell exist within PATH. First the computer will look if the entered command is a built-in or an alias, if the command is not either of those then it will look into PATH for an executable. 

PATH looks like this: 

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

The directories above contain most of the command you would normally use when interacting with the shell. You may have noticed that all the directories are separated by a ":", more on that in a bit. When we execute the command ls we are looking into the /bin/ls and that will be its full PATH.
Now that we know where to find ls, how do get there? The system takes these steps.

Step 1: The system loops through all the Environmental variables until we find PATH.
Image result for environmental variables in shell

Step 2: The system uses strtok() to look for ":" and then separate each directory into an array of strings.

Step 3: The system concatenates ls to each of the directories until we hit /bin/ls.

That's it!

Now that we have the full PATH we can attach the -l option to it and execute them all together and then the system returns this output:

Once the process is finished we return to the command prompt that waits for the next command form the user input.


By: Andres Cheung & Nicholas Uchida.



 

Sunday, October 13, 2019

C Programming Static Library

Static Library 

What is static library?

In the C programming language, a static library is a compiled object file containing all symbols required by the main program to operate as opposed to having to pull in separate entities. Static libraries are either merged with other static libraries and object files during building/linking to form a single executable or loaded at run-time into the address space of their corresponding executable at a static memory offset determined at compile-time/link-time.

Why should we use a static library?

There are several advantages to statically linking libraries with an executable instead of dynamically linking them. The most significant advantage is that the application can be certain that all its libraries are present and that they are the correct version. This avoids dependency problems. Static linking can also allow the application to be contained in a single executable file, simplifying distribution and installation.

With static linking, it is enough to include those parts of the library that are directly and indirectly referenced by the target executable or target library. With dynamic libraries, the entire library is loaded, as it is not known in advance which functions will be invoked by applications depending on the structure of the library.

How to create and use a static library

In order to create a static library first we need to access a terminal or command prompt(Terminal is used in MacOS operating systems while the command prompt is used by Windows Operating systems). After we access the command prompt or Terminal we should create some files written in the C programming language.

We can either use the text editors Emacs, Vim, or Vi, but in this case we will use Emacs.






After we created out files we will compile them using the (GCC) command on the command prompt. 
The GNU Compiler Collection or GCC) is a compiler system produced by the GNU Project supporting various programming languages. For more information on how to use gcc, type the command (man gcc) in your command prompt or terminal.






When we use the gcc command we used the option "-c" accompanied by a wild card "*.c". We use this option when we what to compile files but not link them and the wild card to make sure all files that end with the extension .c end up being compiled. This will create object files or files with the extension ".o".

Next step is  to use the "ar"command. The ar command creates, modifies, and extracts from archives. An archive is a single file holding a collection of other files in a structure that makes it possible to retrieve the original individual files.






The "ar" command with the option -r will insert the files member into the archive and the option -c will create the archive, in this case we will call it "libholberton" with the extension ".a" for an archive. And finally using the wildcard  "*.o" to select all object files. 

Whenever files are added to a library, including the initial creation of the library , the library needs to be indexed, which is done with the command "ranlib". The ranlib makes a header in the library with the symbols of the object file contents.This helps the compiler to quickly reference symbols. A large library may have thousands of symbols meaning an index can significantly speed up finding references. For more information use the "man ranlib" command.





Next, if we want to see the contents of our archive we use the "ar" command the with option "-t". 




















We have now created a static library "libholberton.a" and now let us use the static library by invoking it as part of the compilation and linking process when creating an executable program.





Our "main.c" file contains a quote from Kevin Mitnick, and we will include this file when we compile our program with the "libholberton.a" library. finally we use the option "-L" that will specifies the path to the library. The executable file should be named quote.







Making a bash script to create a static library

Now that we have created out library, how about we do all the above in a single command. We will use a Bash script to combine all previous steps to a single command. A  Bash script is a plain text file which contains a series of commands.

First step is to create a text file that we will call "create_static_lib.sh" using the Emacs text editor. 






After that we add the "#!/bin/bash" command at the very beginning of our text file. Then we proceed to a add each previous command to create a static library one after the other on a new line like this: 

  1. The command "gcc -c *.c" will compile all file with extension ".c" into object files.
  2. The command "ar -rc liball.a *.o" will create our new library with all object files inside.
  3. The command "ranlib liball.a" will index our library.
Finally make sure that you make the file executable by using the command "chmod u+x create_static_lib.sh" so that all users in your computer can run the program. Type "man chmod" in your command prompt or terminal for more information.

That's it! we have successfully created a program that will create a static library.






















Wednesday, September 18, 2019

gcc and what to do

What happens when you run gcc main.c?

What is gcc?

First to start thing we need to know what gcc is. The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain and the standard compiler for most projects related to GNU and Linux, including the Linux kernel.

The Free Software Foundation (FSF) distributes GCC under the GNU General Public License (GNU GPL). GCC has played an important role in the growth of free software, as both a tool and an example.

For more information about gcc visit: https://en.m.wikipedia.org/wiki/GNU_Compiler_Collection

What is the C programming language?

C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, while a static type system prevents unintended operations. 

By design, C provides constructs that map efficiently to typical machine instructions and has found lasting use in applications previously coded in assembly language. Such applications include operating systems and various application software for computers, from supercomputers to embedded systems.

For more information about C visit: https://en.wikipedia.org/wiki/C_(programming_language)

Steps on how to use gcc

  • First step is to open your Terminal/Command prompt.
  • create a file using the C programming language, the file must have the format .c at the end of its name.
  • now this c file will have a program that will print: Hello, World! but for that to happen we need to compile said file in order to present the output. We use the command gcc in order to compile the main.c file.
  •  What gcc will do is to make the main.c file executable so it will print the text Hello, World!
  • Now that we have compiled our file we can now execute it using this command: ./a-out .
Now that we have executed our main.c file we have printed the words Hello, World! The gcc command will compile files on C programming language and will make those executable to run in your Terminal/ Command prompt.


Monday, September 16, 2019

Symbolic and Hard Links

Symbolic and Hard links on Linux

What is a hard link?
A hard link is merely an additional name for existing file on Linux and other Unix-like operating systems. Any number of hard links, and thus any number of names can be created for any file. Hard links may also be created for other hard links but they cannot be created for directories and also cannot cross filesystem boundaries.

What is a symbolic link?
A symbolic link is a file that links to another file or directory using its path. In Linux and Unix symbolic links are created with the 'ln' command, and in the Windows command line, symbolic links are created using the 'mklink' command. 

Now what is the difference?
  • Symbolic links , unlike hard links, can link to any file or directory on any computer.
  • In a hardlink you can use any of the hardlink names created to execute a program or script in the same manner as the original name given.
  • A hard link preserves the contents of the file.
  • A soft link does not contain the data in the target file.
Here we use the 'ln' command to make a hard link to the file called file1.
Now we use the command 'ls -i' to view the link.
If you look at the file1 and the hlink1 you can view the inode on the left side is the same. As you can see,  hardlinks act as a shortcut to that file that is hard linked.

Now lets look at Symbolic links.
Using the command 'ln -s' will create a symbolic link between files.
Now we use the 'ls -i' to view the link. 

Notice that only the hardlink file has the same inode as the hardlink while the soft link file has different inodes.

Beginners guide to ls on linux

This is a simple guide to use the Linux command 'ls' with the option '*.c'.


  • objective: Learn how to use the ls *.c command

First let's talk about what does the 'ls' command do. The 'ls' command will list all files in your current working directory like this: 


The * acts as a wildcard that in this case will be used to filter what type of file we want to display. The option for the 'ls' command '*.c' will act as a filter that will only display files that end with the format  ".c". for example:

As you can now see, all the files that end with the format '.txt' were not displayed and all files that end with the format '.c' have been listed.