Tag Archives: Cpp

Linux system programming: Open file, read file and write file

Terminal PromptThis is my first article in what I'm hoping will be a series of articles on system programming for POSIX compliant operating systems with focus on Linux. Actually I've touched this topic a while ago when I wrote three articles about library programming on Linux (static libraries, dynamic libraries and dynamic libraries using POSIX API). In this series my goal is to go trough basics of Linux system programming from the easiest topics like open file, read file and file write to a bit more complicated things like Berkeley sockets network programming. So lets get started with environment setup and an example of program that copies source file into destination file using POSIX API system calls to demonstrate open(), read() and write() system calls on Linux operating system.

Continue reading

C/C++ library programming on Linux - Part three: Dynamic libraries using POSIX API

This is my third article in the “C/C++ library programming on Linux” series. I strongly suggest you read the first two parts parts of this article series where I’ve given some background about libraries on Linux operating system and explained static and dynamic libraries.

C/C++ library programming on Linux – Part one: Static libraries

C/C++ library programming on Linux – Part two: Dynamic libraries

In this third article I will explain the most interesting way of reusing code using libraries on Linux operating system - by using POSIX ("Portable Operating System Interface for Unix") application programming interface. Using POSIX functions dlopen(), dlsym(), dlclose() and dlerror() you can load and unload your shared libraries during the course of your programs operation. This functions present interface to the Linux dynamic linking loader explained in the first part of my "C/C++ library programming on Linux" article series. This system calls are typically used for implementing stuff like plugins for your application so that you can load functionality provided inside plugin specific dynamic library on-demand. So here's simple example of loading dynamic library into the "cprog" program presented inside my first article "C/C++ library programming on Linux – Part one: Static libraries".

Continue reading

C/C++ library programming on Linux – Part two: Dynamic libraries

This is my second article in the "C/C++ library programming on Linux" series. I recommend that you read the first part of this article series where I've explained the whole library thing, and gave an example of creating and using static library.

C/C++ library programming on Linux – Part one: Static libraries

In this article I will explain dynamic libraries and compare them to static libraries. I will also give an example of creating and using dynamic library.

Dynamic (shared) libraries

Dynamic libraries are different from static libraries in a way that by using them, during compilation process, GCC ads only code "hooks" and soname. That "hooks" and library soname are used during the startup of your application to load correct library and connect "inside" with the "outside" code.

Continue reading

C/C++ library programming on Linux - Part one: Static libraries

Background

One of the most important aspects of modern programming is concept of reuse of code. Even C programming language allows us to reuse our code using concepts like simple functions and structures. C++ programming language goes one step further and allows us to group related variables and functions into classes with the same purpose - the reuse of our valuable code. By using libraries we can go even further from sharing code inside one process - we can share code between completely different programs.

What changes when using libraries? Answer to that question is: "link phase" of your program. In this phase GNU linker links all code modules in fully functional program. When it comes to libraries on Linux operating system we have two basic concepts: static and dynamic (often called "shared"). In this article series I will do my best to explain both Linux libraries concepts using simple C language examples.

Continue reading