Tag Archive : c++

/ c++

GMP (GNU Multi Precision) Library is one of vital library in GNU infrastructure. It is a popular library which gives flexibility to operate abritrary precision integers, rationals, and floating point numbers. There are also MPFR (Multi Precision Floating-point Reliably) and MPC (Multi Precision Complex) which are extension to to GMP capability for precision in computation.

In this article we will try to code using GMP, with C, on Linux 64-bit.

Installation

Check whether our box has GMP library.

locate libgmp.a

If GMP is installed already, we will get entry. If not, proceed to GMP site to download and install the library. Just do usual Linux installation procedure.

Getting Start

Before we start, let’s talking about the arbritrary precision. Do we need it? or why do we need it?

Program constitute of two element: algorithm and data. Data is represented by variable, which has size on memory. The size is fix, according to the word size of processor register. To simplify it, computer process numbers in multiple of 2: 8, 16, 32, or 64 bits at a time. For example, if we declare an int variable x, it will have 32 bit size.

Let’s discover by writing little code.

#include <stdio.h>
#include <limits.h>

void printbin(int x)
{
    if (x==0)
        return;
    else
    {
        printbin(x>>1);
        if (x&1)
            printf("1");
        else
            printf("0");
        
    }
}

int main() {
    int k, x, i;
    char c;

    /* 1. Print machine native size and the max/min integer */
    printf("sizeof(int) = %d bits\n", sizeof(int) * 8);
    printf("max(int)    = %d\n", INT_MAX);
    printf("min(int)    = %d\n", INT_MIN);

    /* 2. Try to overflow */
    k = INT_MAX;
    printf("%d + 1 = %d\n", k, k+1);

    /* 3. See the representation of variable */
    printf("Enter x: ");
    scanf("%d", &x);
    printf("x in binary = ");
    printbin(x);
    printf("\n");

    return 0;
}

sizeof(int) gives the number of bytes for integer and INT_MAX is a macro defined in limits.h which inform the largest possible integer. INT_MIN is just the opposite of it.

This is the result in my box.

Selection_056

See the range, -2147483648 (min) to 2147483647 is the valid value of 32-bits integer. If we increment x which hold maximum value it will overflow to negative.

Enter GMP!

Now let’s dive to the GMP. GMP allows us to use integers whose sizes can grow dynamically to the required precision. Imagine it as BigNum, close to. It can support 128, 256, or 1024 bits. There is no need for us to specify this, the library does to dirt. GMP will dynamically allocate memory to accommodate extra bits of precision as we need.

Let’s try it with simple code.

#include <gmp.h>
#include <stdio.h>
 
int main()
{
    char inputStr[1025];
    
    /* type for GMP integers. 
        We don't care about the internal representation 
    */
    mpz_t n;
    int flag;
    
    printf ("Enter your number: ");
    scanf("%1024s" , inputStr);
  
      /* 1. Initialize the number */
    mpz_init(n);
    mpz_set_ui(n,0); 

    /* 2. Parse the input string as a base 10 number */
    flag = mpz_set_str(n,inputStr, 10);
    if (flag == 0)
    {
        printf("Fail");
        return -1;
    }
    
    printf ("n = ");
    mpz_out_str(stdout,10,n);
    printf ("\n");

    /* 3. Add one to the number */
    mpz_add_ui(n,n,1); /* n = n + 1 */

    /* 4. Print the result */
    printf (" n +1 = ");
    mpz_out_str(stdout,10,n);
    printf ("\n");

    /* 5. Square n+1 */
    mpz_mul(n,n,n); /* n = n * n */
    printf (" (n +1)^2 = ");
    mpz_out_str(stdout,10,n);
    printf ("\n");

    /* 6. Clean up the mpz_t handles or else we will leak memory */
    mpz_clear(n);

    return 0; 
}

Remember to include library libgmp.a so GCC won’t complaint. Here is the result:

Selection_057

The complete information is always on GMP manual. But we love simplicity. So here we go.

At minimum we need this skeleton to use GMP.

#include <gmp.h>

/* ... */

  mpz_t n;
  mpz_init(n);

      /* ... */

  mpz_set_ui(n,0);

      /* ... */

  mpz_clear(n);

Now let’s make something out of it. Say, factorial program?

#include <gmp.h>
#include <stdio.h>
#include <stdlib.h>

/* Use iterative solution here. */
void factorial(int n)
{
    int i;
    mpz_t p;
    
    mpz_init_set_ui(p, 1);      /* p = 1 */
    for (i=1; i<= n; i++)
        mpz_mul_ui(p, p, i);    /* p = p * 1 */
    
    printf("%d! = ", n);
    mpz_out_str(stdout, 10, p);
    printf("\n");
    mpz_clear(p);
}

int main()
{
    int n;
    
    do {
        printf("N = ");
        scanf("%d", &n);
    
        if (n <= 1)
        {
            printf("N should be > 1\n");
            printf("Leaving...\n");
            break;
        }
        
        factorial(n);
    } while (1);
    return 0;
}

Try it!

Before Wt: Installing Prerequisite for Wt

December 12, 2015 | Article | No Comments

In this article we will discuss about installation of various packages and software as requirement for Wt. The goal of this article is for everyone to successfully install prerequisites for building Wt.

For this purpose, I use Slackware64 14.0 as development machine. However, you can also use other machine.

This article can be broken down to two sections:

  • System requirements
  • Optional packages

System Requirements

Applications on this sections is the primary requirements for building Wt. At least there are two packages:

  • CMake: cross platform make configure utility.
  • Boost C++ Library: A general purpose library for C++. Latest version is recommended.

Another requirements are the connector support which depend on what would you like.

For FastCGI (Unix only)

  • Apache2 or other web server which supports the FastCGI protocol. You can go to this page for more information about Web Server (Apache, Lighttpd, Nginx, etc). When using Apache use mod_fastcgi-2.4.x or mod_fcgid-2.3.5 alternatively.
  • FastCGI development kit

For built in http daemon

For ISAPI (Windows only)

  • ISAPI, which only works for deploying Wt applications within Microsoft IIS server.

Optional Packages

The following packages are optional, but highly recommended for Wt. They enables additional features in Wt. The packages are:

  • OpenSSL: Enabling HTTPS protocol support for Client (Http::Client) and Server (Http::Server)
  • libharu: Enabling rendering support for PDF documents.
  • GraphicsMagick: Enabling rendering support for raster images like PNG or GIF
  • pango: Text rendering for TrueType font selection
  • PostgreSQL, MySQL/MariaDB, and Firebird: Enabling connectors database for ORM library (alongside the Sqlite3 connector). You can choose any (all whole).

CMake

Cross platform, open-source build system consists of tools designed to build, test, and packaging software. It used to control the software compilation process using simple platform and compiler independent configuration files.

For information on how to install CMake, follow this article.

Boost C++ Library

Portable C++ source libraries and work well with the C++ Standard Library. It is intended to be widely useful and usable across a broad spectrum of applications.

For information on how to install Boost Library, follow this article.

OpenSSL

libharu

Free, cross platform, open source library for generating PDF files.

For information on how to install libHaru, follow this article.

GraphicsMagick

The swiss army knife of image processing. It provides a robust and efficient collection of tools and libraries which support reading, writing, and manipulating an image in over 88 major formats including DPX, GIF, JPEG, JPEG-2000, PNG, PDF, PNM, and TIFF.

For information on how to install GraphicsMagick, follow this article.

pango

PostgreSQL

MySQL

MariaDB

Firebird

A relational database offering many ANSI SQL standard features that runs on various operating system. It offers excellent concurrency, high performance, and powerful language support for stored procedures and triggers.

For information on how to install FirebirdSQL, follow this article.

FastCGI Development Kit

whttpd

C++ is growing, as per new standard C++11 now it supports threading. C++11 introduce a new thread library. The new standard define utilities for starting and managing threads. It also contains utilities for synchronization like mutexes and other locks, atomic variables, and other utilities. We will get through this so called C++11 threading by series of articles “C++11 Concurrency”.

To compile the sample code, we need a C++11 compiler. In my case, I use GCC 4.8.1 (on Slackware Linux) or MinGW counterpart (on Windows 8.1). To activated C++11 support features, we need to pass the “-std=c++11″ option to GCC, for example:

g++ filename.cpp -o program -std=c++11

C++ Concurrency

see <atomic>, <condition_variable>, <future>, <mutex>, and <thread> for reference.

Starting from C++11, there are five more headers added to the standard library to support concurrency. They are:

  • atomic
  • condition_variable
  • future
  • mutex
  • thread

These headers are used for multithreading and concurrency. Before the language standard, we would use OS facilities such as POSIX thread or libraries like OpenMP and MPI to enable concurrency using C++.

The <atomic> header defines some definition to allow us using atomic operation. It encapsulate a value whose access is guaranteed to not cause data races and can be used to synchronize memory access among different threads.

The <condition_variable> header defines classes used as condition variable for synchronization.

The <future> header facilitates synchronization access to value.

The <mutex> header is used for allowing mutual exclusion (mutex) of concurrent execution of critical sections of code.

The <thread> header defines thread class, used for spawning threads on C++ and also manage threads as we like.

Starting Threads

see also: Multithreading Support in C++11

Starting a new thread is very easy. We need to create an instance of a std::thread and supply a function and thread will automatically be started and executing the function. The function we talk about can be a function pointer or even a lambda expression.

So first, let’s write a simple code to demonstrate the power of C++ thread.

#include <thread>  
#include <iostream>    
using std::cout;  
using std::endl;  
using std::thread;    
void hello(){
   cout << "Hello from thread " << endl;
}    

int main(){      
   thread t1(hello);   
   t1.join();    
 
   return 0;  
}

In the above snippet, we pass a function pointer to thread.

All the threads utilities can be found in the <thread> header. As we see, when constructing a thread instance, we supply a function which will be executed on the fly. The join() method is called to force current thread to wait for the other one. In other word, main thread won’t finish until t1 finish it’s task. If we omit this call, the result is undefined.

We can, however, create more than one thread. To distinguish each of thread, the std::thread class provide a get_id() method which returns an unique id for this thread. We can get a reference to the current thread by std::this_thread variable. Let’s dive in example:

#include <thread>  
#include <iostream>  
#include <vector>    
using std::cout;  
using std::endl; 
using std::thread;  
using std::vector;    

void hello(){      
   cout << "Hello from thread " << this_thread::get_id() << endl;  
}    

int main(){   
   vector<thread> threads;      
   for (int i = 0; i < 5; ++i) { 
      threads.push_back(thread(hello));      
   }        
   for (auto& thread : threads){       
      thread.join();      
   }        

   return 0;  
}

Sample output:

Hello from thread 2  
Hello from thread 4  
Hello from thread 3  
Hello from thread 5  
Hello from thread 6

Our program will start thread one after one and then store them into a vector. This is common strategy to handle several threads.

The threads might interleave. We have no way to control the order of execution of threads. A thread can be preempted at any moment which might lead us to produce following result:

Hello from thread 
Hello from thread 6  
Hello from thread 3  
Hello from thread 5  
2 
Hello from thread 4

So, there is no certainty in which function will be executed first. All decision are taken by OS. The Operating System will decide when resource are available for our threads and execute them on whatever CPU is least occupied.

Aside from function pointer, we can also use lambda expression. Lambda execution is very useful when the executing code is very small thus we don’t necessary need to create a function just for that. We can rewrite the previous code as this one:

#include <thread>  
#include <iostream>  
#include <vector>    
using std::cout;  
using std::endl;  
using std::thread;  
using std::vector;    

int main(){      
   vector<thread> threads;        
   for (int i = 0; i < 5; ++i){          
      threads.push_back(thread([]() {
         cout << "Hello from thread " << this_thread::get_id() << endl; 
      }));
   }
   for (auto& thread : threads){
      thread.join();
   }
   return 0;
}

Threading in Action

Now we will illustrate the power of parallel programming in C++11. We will do something simple yet interesting: parallel merge sort.

C++ Reserved Words

December 11, 2015 | Article | No Comments

C++ reserves some words for its own use and for use in C++ libraries. Thus, we shouldn’t use a reserved word as an identifier in a declaration. Reserved words comes in two categories: keywords and alternative tokens.

This article will list C++ reserved words based on C++11 standard.

C++ Keywords

Keywords are identifiers that form the vocabulary or a programming language. By combining the keyword and following the grammar of C++, we do code with C++. These vocabulary may not be used for other purpose, such as serving as variable names.

The following list show C++’s keywords:

[table “7” not found /]

Alternative Tokens

In addition to keywords, C++ has some alphabetic alternative representations of operators, termed alternative tokens. These, too, are reserved.

[table “8” not found /]

Notes

The meaning of “auto” keyword has changed in C++11. In C++11 standard, the auto keyword is used to declare a variable with type inferred from the expression or value assigned to it.

In addition to keywords, there are two identifiers with special meaning, which maybe used as names of objects or functions, but have special meaning in certain contexts.

override
final

Also, each name that contains a double underscore __ or begins with an underscore followed by an uppercase letter is always reserved to the implementation and should not be used as an identifier. Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace; such names may be used as identifiers in user-defined namespaces, as names of class members, etc.

The name posix is reserved for a future top-level namespace. The behavior is undefined if a program declares or defines anything in that namespace.(since C++11)

Fibonacci Numbers in C\C++

December 11, 2015 | Article | No Comments

In mathematics, Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence:

fibonacci_1

or:

fibonacci_2

In mathematical terms, the Fibonacci sequence Fn is defined by the recurrence relation

fibonacci_3

with seed values

fibonacci_4

Our task is to write a function that returns Fn. Following are different methods to get the nth Fibonacci number

Method 1: Simple and Naive Recursion

A simple recursion that implementing the Fibonacci definition above.

int fib(int n)
{
    if (n <= 1) {
        return n;
    }
    return fib(n-1) + fib(n-2);
}

Time complexity: T(n) = T(n-1) + T(n-2) which is exponential.

This implementation does a lot of repeated work (see below).

                         fib(5)   
                     /             \     
               fib(4)                fib(3)   
             /      \                /     \
         fib(3)      fib(2)         fib(2)    fib(1)
        /     \        /    \       /    \  
  fib(2)   fib(1)  fib(1) fib(0) fib(1) fib(0)
  /    \
fib(1) fib(0)

Extra space: O(n) if we consider function call stack size. Otherwise O(1)

Method 2: Dynamic Programming – Top Down Approach

We can void the repeated work by storing the numbers calculated so far.

/* the real function to compute the fibonacci numbers */
int _fib(int * st, int n)
{
    /* if the value has not stored yet, computed it */
    if (st[n] == -1) {
        st[n] = _fib(st, n-1) + _fib(st, n-2);
    }

    return st[n];
}

/* a wrapper function */
int fib(int n)
{
    /* array to store the fibonacci numbers */
    int st[n+1];
    int res;
    int i;

    st[0] = 0;
    st[1] = 1;
    for (i=2; i<=n; i++) {
        st[i] = -1;
    }

    _fib(st, n);
    return st[n];
}

Time complexity: O(n)

Extra space: O(n)

Method 3: Dynamic Programming – Bottom Up Approach

Another approach which also store the calculated number so far.

int fib(int n)
{
    int st[n+1];
    int i;
    
    st[0] = 0;
    st[1] = 1;
    
    for (i=2; i<= n; i++)
    {
        st[i] = st[i-1] + st[i-2];
    }
    
    return st[n];
}

Time complexity: O(n)

Extra space: O(n)

Method 4: Dynamic Programming – Space Optimized Method 3

We can optimize the space used in method 3 by storing the previous two numbers only because that are all we need to get the next Fibonacci numbers.

int fib(int n)
{
    int a = 0, b = 1, c;
    int i;
    
    for (i=2; i<=n; i++)
    {
        c = a + b;
        a = b;
        b = c;
    }
    return b;
}

Time complexity: O(n)

Extra space: O(1)

Method 5: Using Matrix

This method relies on 2-dimensional system of linear difference equations for Fibonacci sequence. If we n times multiply the matrix M = {{1,1}, {1,0}} to itself (in other word calculate power(M, n)) then we get the (n+1)th Fibonacci number as the element at row and column (0,0) in the resultant matrix.

fibonacci_5

Here is how we do that:

/* Helper functions */
void multiply(int F[2][2], int M[2][2]);
void power1(int F[2][2], int n);

int fib(int n)
{
    /* We don't need to compute the matrix for these values */
    if (n<2)
        return n;
    
    int F[2][2] = {{1,1}, {1,0}};
    
    power(F, n-1);
    return F[0][0];
}

/* multiply 2 matrices F and M of size 2*2 and puts the result back to F */
void multiply(int F[2][2], int M[2][2])
{
    int w = F[0][0] * M[0][0] + F[0][1] * M[1][0];
    int x = F[0][0] * M[0][1] + F[0][1] * M[1][1];
    int y = F[1][0] * M[0][0] + F[1][1] * M[1][0];
    int z = F[1][0] * M[0][0] + F[1][1] * M[1][1];
    
    F[0][0] = w;
    F[0][1] = x;
    F[1][0] = y;
    F[1][1] = z;
}

/* designed for fib only */
void power(int F[2][2], int n)
{
    int i;
    int M[2][2] = {{1,1}, {1,0}};
    
    for (i=2; i<=n; i++)
    {
        multiply(F, M);
    }
}

Time complexity: O(n)

Extra space: O(1)

Method 6: Using Matrix – Optimized Method 5

The method 5 can be optimized to work in O(log n) time complexity. By implementing the Divide and Conquer to power and multiplication, we can do better.

/* Helper functions */
void multiply(int F[2][2], int M[2][2]);
void power(int F[2][2], int n);

int fib4(int n)
{
    if (n<2)
        return n;
    
    int F[2][2] = {{1,1}, {1,0}};
    
    power(F, n-1);
    return F[0][0];
}

/* multiply 2 matrices F and M of size 2*2 and puts the result back to F */
void multiply(int F[2][2], int M[2][2])
{
    int w = F[0][0] * M[0][0] + F[0][1] * M[1][0];
    int x = F[0][0] * M[0][1] + F[0][1] * M[1][1];
    int y = F[1][0] * M[0][0] + F[1][1] * M[1][0];
    int z = F[1][0] * M[0][0] + F[1][1] * M[1][1];
    
    F[0][0] = w;
    F[0][1] = x;
    F[1][0] = y;
    F[1][1] = z;
}

/* designed for fib only, optimized it using Divide and Conquer */
void power(int F[2][2], int n)
{
    if (n==0 || n==1)
        return;
    int M[2][2] = {{1,1}, {1,0}};

    power(F, n/2);
    multiply(F, F);
    if (n%2) {
        multiply(F, M);
    }
}

Time complexity: O(log n)

Extra space: O(log n) if we consider the function call stack size, otherwise O(1)

Templates are a feature of the C++ programming language that allow function and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one. The C++ Standard Library specify a special section for heavy use of template, called Standard Template Library.

Defining a template is like defining a blueprint. When we need a specific instance of function or class, the instance can be made from the generic blueprint. However, the common procedure in C++ which put the definition in a C++ header file and the implementation in a C++ source file can’t be applied to template. This is the behavior which is true for all compiler.

In this article we will discuss about how to separate declaration and implementation of template object (class / function). The article will be divided into three sections: how template is parsed, compilation issue, and the solution methods.

How Template is Parsed

Unlike other code, templates are parsed not only once, but twice. This process is explicitly defined in the C++ standard and although some compilers do ignore this, they are, in effect, non-compliant and may behave differently to what this article describes.

So what are those two processes? They are, let’s say, Point of Declaration (PoD) and Point of Instantiation (PoI).

1. Point of Declaration (PoD)

During the first parse, or Point of Declaration, the compiler checks the syntax of the template and match it with C++ grammar. This way, compiler does not consider the dependent types (the template parameters that form the templates types within the templates).

In real world, we can consider this phase as checking the grammar of a paragraph without checking the meaning of the words (the semantics). Grammatically the paragraph can be correct but the the arrangement of words may have no useful meaning. During the grammar checking phase, we don’t are about the meaning of the words. Our intention only toward the correct syntax.

Now, consider following template code:

template <typename T>
void foo (T const & t)
{
    t.bar();
}

This is syntactically correct. However, at this point we have no idea what type the dependent type T is so we just assume that in all cases of T it is correct to call member bar() on it. Of course, if type T doesn’t have this member then we have a problem but until we know what type T is we don’t know if there is a problem so this code is ok for now.

2. Point of Instantiation (PoI)

At PoD we have declare a template, which we have the blueprint. The instantiation process start at Point of Instantiation. At this point we actually define a concrete type of our template. So consider these 2 concrete instantiations of the template defined above…

foo(1); // this will fail the 2nd pass because an int (1 is an int) does not have a member function called bar()
foo(b); // Assuming b has a member function called bar this instantiation is fine

It is perfectly legal to define a template that won’t be corrected under all circumstances of instantiation. Since code for a template is not generated unless it is instantiated the compiler won’t complain unless we try to instantiate it.

At this point, the semantics are now checked against the known dependent type to make sure that the generated code will be correct. To do this, the compiler must be able to see the full definition of template. If the definition of the template is defined in a different place where it is being instantiated the compiler has no way to perform this check. Thus, the template won’t be instantiated and an error message will be delivered.

Remember that compiler can only see and process one translation unit (module / source code) at a time. Now, if the template is only used in one translation unit and the template is defined in same translation unit, no problem rises.

Now let’s recap what we got so far. If the template definition is in translation unit A and we try to instantiate it in translation unit B the compiler won’t be able to instantiate the template because it can’t see the full definition so it will result in linker errors (undefined symbols). If everything is in one place then it will work. but it is not a good way to write templates.

Template Compilation-Issue

From the first section we know that between template definition should be visible and on the same translation unit with the instantiation. Thus, the following code works:

/** BigFile.cpp **/
#ifndef _FOO_HPP_
#define _FOO_HPP_

template<typename T>
class Foo {
public:
   Foo() { }
   void setValue (T obj_i) { }
   T getValue () { return m_Obj; }

private:
   T m_Obj;
}

#endif

The above code is legal, but we can’t afford to this everytime. Sooner or later we’ll probably end up using the template in other translation units because it is highly unlikely (although not improbable) that we’d go to all the effort of creating a generic template class/function that we’ll only ever use in one place.

Our first effort to separate the declaration and implementation (or definition) would be:

/** Foo.hpp **/
#ifndef _FOO_HPP_
#define _FOO_HPP_

template<typename T>
class Foo {
public:
   Foo();
   void setValue (T obj_i);
   T getValue ();

private:
   T m_Obj;
}

#endif

and

#include "Foo.hpp"

Foo::Foo()
{
}

void Foo::setValue (T obj_i)
{
}

T Foo::getValue()
{
   return m_Obj;
}

Now if we try to implement the template class like a normal class implementation as shown above, it will generate a set of compilation error.

To compile the class without any errors, we need to put the template specific declaration in a source file (.cpp file)

#include "Foo.hpp"

template <typename T>
Foo<T>::Foo()
{
}

template <typename T>
void Foo<T>::setValue (T obj_i)
{
}

template <typename T>
T Foo<T>::getValue()
{
   return m_Obj;
}

We pass the compilation phase (the PoD). However the next problem we have is in linking. With above code, after resolving all the compilation errors, we may face linking error issue while we create an object of this class in any file other than Foo.cpp.

For example we have a client source code which instantiate the template (in different translation unit):

/** Client.cpp **/
#include "Foo.hpp"

int main() {
   Foo<int> temp;

   return 0;
}

We have known that the declaration+definition and instantiation are in different translation unit. The client code in Client.cpp can’t access the template implementation source (Foo.cpp) thus compiler refuse to instantiate the template. Compiler have no idea how to construct the Foo member function. And if we have put the implementation in a source file and made it a separate part of the project, the compiler won’t be able to find it when it is trying to compile the client source file.

The inclusion of Foo header file won’t be sufficient at this time. It only tells the compiler how to allocate the object data and how to build the calls to the member functions, not how to build the member functions. The compiler won’t complain. It will assume that these functions are provided elsewhere, and leave it to the linker to find them. So, when it’s time to link, you will get “unresolved references” to any of the class member functions that are not defined “inline” in the class definition.

So how do we structure our code so that the compiler can see the definition of the template in all translation units where it is instantiated? The solution is really quite simple, put the templates definition somewhere that is visible to all PoIs and that is. All the methods will be described at next section.

Solution Methods

There are different methods to solve our presented problem. We can select from any of these methods depending on which suitable for our application design.

Method 1

We can create an object of a template class in the same source file where it is implemented (TestTemp.cpp). So, there is no need to link the object creation code with its actual implementation in some other file. This will cause the compiler to compile these particular types so the associated class member functions will be available at link time. Here is the sample code:

/** Foo.hpp **/
#ifndef _FOO_HPP_
#define _FOO_HPP_

template<typename T>
class Foo {
public:
   Foo();
   void setValue (T obj_i);
   T getValue ();

private:
   T m_Obj;
}

#endif

and

#include "Foo.hpp"

Foo::Foo()
{
}

void Foo::setValue (T obj_i)
{
}

T Foo::getValue()
{
   return m_Obj;
}

/* We define a function but no need to call this temporary function.
 * It is used only to avoid link error
 *
void TemporaryFunction()
{
   Foo<int> temp;
}

also the client:

/** Client.cpp **/
#include "Foo.hpp"

int main() {
   Foo<int> temp;
   temp.setValue(3);
   int value = temp.getValue();
   return 0;
}

The temporary function in Foo.cpp will solve the link error. No need to call this function because it’s global.

Method 2

In this method we include source file that implements our template in our client source file.

/** Foo.hpp **/
#ifndef _FOO_HPP_
#define _FOO_HPP_

template<typename T>
class Foo {
public:
   Foo();
   void setValue (T obj_i);
   T getValue ();

private:
   T m_Obj;
}

#endif

and

#include "Foo.hpp"

Foo::Foo()
{
}

void Foo::setValue (T obj_i)
{
}

T Foo::getValue()
{
   return m_Obj;
}

also the client:

/** Client.cpp **/
#include "Foo.hpp"
#include "Foo.cpp"

int main() {
   Foo<int> temp;
   temp.setValue(3);
   int value = temp.getValue();
   return 0;
}

Method 3

In this method we include the source file that implements our template class (Foo.cpp) in our header file that declare the template class (Foo.hpp), and remove the source file form the project, not from the folder.

/** Foo.hpp **/
#ifndef _FOO_HPP_
#define _FOO_HPP_

template<typename T>
class Foo {
public:
   Foo();
   void setValue (T obj_i);
   T getValue ();

private:
   T m_Obj;
}

#include "Foo.cpp"

#endif

and

Foo::Foo()
{
}

void Foo::setValue (T obj_i)
{
}

T Foo::getValue()
{
   return m_Obj;
}

also the client:

/** Client.cpp **/
#include "Foo.hpp"

int main() {
   Foo<int> temp;
   temp.setValue(3);
   int value = temp.getValue();
   return 0;
}

Ten C++11 Features You Should Know and Use

December 11, 2015 | Article | No Comments

This article will be a resume to several articles discussing individual subject.

There are lots of new additions to the C++ language and standard library after C++11 standard passed. However, I believe some of these new features should become routing for all C++ developers.

Features we are talking about:

  1. auto & decltype
  2. nullptr
  3. Range-based for loops
  4. Override and final
  5. Strongly-typed enums
  6. Smart pointers
  7. Lambdas
  8. non-member begin() and end()
  9. static_assert and type traits
  10. Move semantics

auto & decltype

More: Improved Typed Reference in C++11: auto, decltype, and new function declaration syntax

Before C++11 era, keyword auto was used for storage duration specification. In the new standard, C++ define clearly the purpose to be type inference. Keyword auto is a placeholder for a type, telling the compiler it has to deduce the actual type of a variable that is being declared from its initializer.

auto I = 42;        // I is an int
auto J = 42LL;      // J is an long long
auto P = new foo(); // P is a foo* (pointer to foo)

Using auto means less code for writing typename. The very convenience use of auto would be inferring type for iterator:

std::map<std::string, std::vector<int>> myMap;
for (auto it = begin(map); it != end(map); ++it)
{
//...
}

Here we save lot of works by order compiler to deduce the type of it.

decltype in other hand is a handy keyword to get type of an expression. Here we can inspect what’s going on this code:

short a = 10;
long b = 655351334;
decltype(a+b) c = 5;

std::cout << sizeof(a) << " " << sizeof(b) << " " << sizeof(c) << std::endl;

When we execute this code using proper C++11 compiler, we got variable c as a type used for summation of a and b. We know that when a short is summed with a long, the short variable will be typecasted automatically to type sufficient enough to hold the result code. And the decltype will give us it’s type.

As said before, decltype is used to get type of an expression, therefore it is valid for use to do this:

int function(int a, int b)
{
	return a * b;
}

int main()
{
	decltype(function(a,b)) c = 10;

	return 0;
}

As long as the expression involved is valid.

Now, in C++ we have a new function declaration syntax. This syntax leverage the power of both auto and decltype. Note that auto cannot be used as the return type of a function, so we must have a trailing return type. In this case auto does not tell the compiler it has to infer the type, it only instructs it to look for the return type at the end of the function.

template <typename T1, typename T2>
auto compose(T1 t1, T2 t2) -> decltype (t1 + t2)
{
	return t1+t2;
}

In above snippet, we compose the return type of function from the type of operator + that sums the values of types T1 and T2.

nullptr

More: Nullptr, Strongly typed Enumerations, and Cstdint

Since the inception of C++, zero is used as the value of null pointers. This is a direct influence from C language. The system itself has drawbacks due to the implicit conversion to integral types.

void function(int a);
void function(void* a);

function(NULL);

Now, which one is being called? On smarter compiler, it will gives error saying “the call is ambiguous”.

C++11 library gives solution for this. Keyword nullptr denotes a value of type std::nullptr_t that represents the null pointer literal. Implicit conversions exists from nullptr to null pointer value of any pointer type and any pointer-to-member types, but also to bool (as false). But no implicit conversion to integral types exists.

void foo(int* p) {}

void bar(std::shared_ptr<int> p) {}

int* p1 = NULL;
int* p2 = nullptr;   
if(p1 == p2)
{
}

foo(nullptr);
bar(nullptr);

bool f = nullptr;
int i = nullptr; // error: A native nullptr can only be converted to bool or, using reinterpret_cast, to an integral type

Using 0 is still valid for backward compatibility.

Range-based for loops

More: C++ Ranged Based Loop

Ever wonder how could we do foreach statement in C++? Joy for us because C++11 now augmented the for statement to support it. Using this foreach paradigm we can iterate over collections. In the new form, it is possible to iterate over C-like arrays, initializer lists, and anything for which the non-member begin() and end() functions are overloaded.

std::map<std::string, std::vector<int>> map;
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
map["one"] = v;

for(const auto& kvp : map) 
{
  std::cout << kvp.first << std::endl;

  for(auto v : kvp.second)
  {
     std::cout << v << std::endl;
  }
}

int arr[] = {1,2,3,4,5};
for(int& e : arr) 
{
  e = e*e;
}

The syntax is not different from “normal” for statement, okay it is a little different.

The for syntax in this paradigm is:

for (type iterateVariable : collection)
{
// ...
}

Override and final

In C++, there isn’t a mandatory mechanism to mark virtual methods as overriden in derived class. The virtual keyword is optional and that makes reading code a bit harder, because we may have to look through the top of the hierarchy to check if the method is virtual.

class Base 
{
public:
   virtual void f(float);
};

class Derived : public Base
{
public:
   virtual void f(int);
};

Derived::f is supposed to override Base::f. However, the signature differ, one takes a float, one takes an int, therefor Base::f is just another method with the same name (and overload) and not an override. We may call f() through a pointer to B and expect to print D::f, but it’s printing B::f.

C++11 provides syntax to solve this problem.

class Base 
{
public:
   virtual void f(float);
};

class Derived : public Base
{
public:
   virtual void f(int) override;
};

Keyword override force the compiler to check the base class(es) to see if there is a virtual function with this exact signature. When we compile this code, it will triggers a compile error because the function supposed to override the base class has different signature.

On the other hand if you want to make a method impossible to override any more (down the hierarchy) mark it as final. That can be in the base class, or any derived class. If it’s in a derived class, we can use both the override and final specifiers.

class Base 
{
public:
   virtual void f(float);
};

class Derived : public Base
{
public:
   virtual void f(int) override final;
};

class F : public Derived
{
public:
   virtual void f(int) override;
}

Function declared as ‘final’ cannot be overridden by ‘F::f’.

Strongly-typed enums

More: Nullptr, Strongly typed Enumerations, and Cstdint

Traditional enums in C++ have some drawbacks: they export their enumerators in the surrounding scope (which can lead to name collisions, if two different enums in the same have scope define enumerators with the same name), they are implicitly converted to integral types and cannot have a user-specified underlying type.

C++11 introduces a new category of enums, called strongly-typed enums. They are specified with the “enum class” keyword which won’t export their enumerators in the surrounding scope and no longer implicitly converted to integral types. Thus we can have a user specified underlying type.

enum class Options { None, One, All };
Options o = Options::All;

Smart pointers

All the pointers are declared in header <memory>

In this article we will only mention smart pointers with reference counting and auto releasing of owned memory that are available:

  • unique_ptr: should be used when ownership of a memory resource does not have to be shared (it doesn’t have a copy constructor), but it can be transferred to another unique_ptr (move constructor exists).
  • shared_ptr: should be used when ownership of a memory resource should be shared (hence the name).
  • weak_ptr: holds a reference to an object managed by a shared_ptr, but does not contribute to the reference count; it is used to break dependency cycles (think of a tree where the parent holds an owning reference (shared_ptr) to its children, but the children also must hold a reference to the parent; if this second reference was also an owning one, a cycle would be created and no object would ever be released).

The library type auto_ptr is now obsolete and should no longer be used.

The first example below shows unique_ptr usage. If we want to transfer ownership of an object to another unique_ptr use std::move. After the ownership transfer, the smart pointer that ceded the ownership becomes null and get() returns nullptr.

void foo(int* p)
{
   std::cout << *p << std::endl;
}
std::unique_ptr<int> p1(new int(42));
std::unique_ptr<int> p2 = std::move(p1); // transfer ownership

if(p1)
  foo(p1.get());

(*p2)++;

if(p2)
  foo(p2.get());

The second example shows shared_ptr. Usage is similar, though the semantics are different since ownership is shared.

void foo(int* p)
{
   std::cout << *p << std::endl;
}
void bar(std::shared_ptr<int> p)
{
   ++(*p);
}
std::shared_ptr<int> p1(new int(42));
std::shared_ptr<int> p2 = p1;

foo(p2.get());
bar(p1);   
foo(p2.get());

We can also make equivalent expression for first declaration as:

auto p3 = std::make_shared<int>(42);

make_shared<T> is a non-member function and has the advantage of allocating memory for the shared object and the smart pointer with a single allocation, as opposed to the explicit construction of a shared_ptr via the contructor, that requires at least two allocations. In addition to possible overhead, there can be situations where memory leaks can occur because of that. In the next example memory leaks could occur if seed() throws an error.

void foo(std::shared_ptr<int> p, int init)
{
   *p = init;
}
foo(std::shared_ptr<int>(new int(42)), seed());

No such problem exists if using make_shared.

The last sample shows usage of weak_ptr. Notice that you always must get a shared_ptr to the referred object by calling lock(), in order to access the object.

auto p = std::make_shared<int>(42);
std::weak_ptr<int> wp = p;

{
  auto sp = wp.lock();
  std::cout << *sp << std::endl;
}

p.reset();

if(wp.expired())
  std::cout << "expired" << std::endl;

Lambdas

More: Guide to Lambda Closure in C++11

Lambda is anonymous function. It is powerful feature borrowed from functional programming that in turned enabled other features or powered library. We can use lambda wherever a function object or a functor or a std::function is expected.

You can read the expression here.

std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);

std::for_each(std::begin(v), std::end(v), [](int n) {std::cout << n << std::endl;});

auto is_odd = [](int n) {return n%2==1;};
auto pos = std::find_if(std::begin(v), std::end(v), is_odd);
if(pos != std::end(v))
  std::cout << *pos << std::endl;

A bit trickier are recursive lambdas. Imagine a lambda that represents a Fibonacci function. If you attempt to write it using auto you get compilation error:

auto fib = [&fib](int n) {return n < 2 ? 1 : fib(n-1) + fib(n-2);};

The problem is auto means the type of the object is inferred from its initializer, yet the initializer contains a reference to it, therefore needs to know its type. This is a cyclic problem. The key is to break this dependency cycle and explicitly specify the function’s type using std::function.

std::function<int(int)> lfib = [&lfib](int n) {return n < 2 ? 1 : lfib(n-1) + lfib(n-2);};

non-member begin() and end()

Two new addition to standard library, begin() and end(), gives new flexibility. It is promoting uniformity, concistency, and enabling more generic programming which work with all STL containers. These two functions are overloadable, can be extended to work with any type including C-like arrays.

Let’s take an example. We want to print first odd element on a C-like array.

int arr[] = {1,2,3};
std::for_each(&arr[0], &arr[0]+sizeof(arr)/sizeof(arr[0]), [](int n) {std::cout << n << std::endl;});

auto is_odd = [](int n) {return n%2==1;};
auto begin = &arr[0];
auto end = &arr[0]+sizeof(arr)/sizeof(arr[0]);
auto pos = std::find_if(begin, end, is_odd);
if(pos != end)
  std::cout << *pos << std::endl;

With non-member begin() and end() it could be put as this:

int arr[] = {1,2,3};
std::for_each(std::begin(arr), std::end(arr), [](int n) {std::cout << n << std::endl;});

auto is_odd = [](int n) {return n%2==1;};
auto pos = std::find_if(std::begin(arr), std::end(arr), is_odd);
if(pos != std::end(arr))
  std::cout << *pos << std::endl;

This is basically identical code to the std::vector version. That means we can write a single generic method for all types supported by begin() and end().

template <typename Iterator>
void bar(Iterator begin, Iterator end) 
{
   std::for_each(begin, end, [](int n) {std::cout << n << std::endl;});

   auto is_odd = [](int n) {return n%2==1;};
   auto pos = std::find_if(begin, end, is_odd);
   if(pos != end)
      std::cout << *pos << std::endl;
}

template <typename C>
void foo(C c)
{
   bar(std::begin(c), std::end(c));
}

template <typename T, size_t N>
void foo(T(&arr)[N])
{
   bar(std::begin(arr), std::end(arr));
}

int arr[] = {1,2,3};
foo(arr);

std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
foo(v);

static_assert and type traits

static_assert performs an assertion check at compile-time. If the assertion is true, nothing happens. If the assertion is false, the compiler displays the specified error message.

template <typename T, size_t Size>
class Vector
{
   static_assert(Size < 3, "Size is too small");
   T _points[Size];
};

int main()
{
   Vector<int, 16> a1;
   Vector<double, 2> a2;
   return 0;
}

static_assert becomes more useful when used together with type traits. These are a series of classes that provide information about types at compile time. They are available in the <type_traits> header. There are several categories of classes in this header: helper classes, for creating compile-time constants, type traits classes, to get type information at compile time, and type transformation classes, for getting new types by applying transformation on existing types.

In the following example function add is supposed to work only with integral types.

template <typename T1, typename T2>
auto add(T1 t1, T2 t2) -> decltype(t1 + t2)
{
   return t1 + t2;
}

However, there are no compiler errors if one writes

std::cout << add(1, 3.14) << std::endl;
std::cout << add("one", 2) << std::endl;

The program actually prints 4.14 and “e”. But if we add some compile-time asserts, both these lines would generate compiler errors.

template <typename T1, typename T2>
auto add(T1 t1, T2 t2) -> decltype(t1 + t2)
{
   static_assert(std::is_integral<T1>::value, "Type T1 must be integral");
   static_assert(std::is_integral<T2>::value, "Type T2 must be integral");

   return t1 + t2;
}

Move semantics

More: Move Semantics and rvalue references in C++11

C++11 has introduced the concept of rvalue references (specified with &&) to differentiate a reference to an lvalue or an rvalue. An lvalue is an object that has a name, while an rvalue is an object that does not have a name (temporary object). The move semantics allow modifying rvalues (previously considered immutable and indistinguishable from const& types).

A C++ class/struct used to have some implicit member functions: default constructor (only if another constructor is not explicitly defined) and copy constructor, a destructor and a copy assignment operator. The copy constructor and the copy assignment operator perform a bit-wise (or shallow) copy, i.e. copying the variables bitwise. That means if you have a class that contains pointers to some objects, they just copy the value of the pointers and not the objects they point to. This might be OK in some cases, but for many cases you actually want a deep-copy, meaning that you want to copy the objects pointers refer to, and not the values of the pointers. In this case you have to explicitly write copy constructor and copy assignment operator to perform a deep-copy.

What if the object you initialize or copy from is an rvalue (a temporary). You still have to copy its value, but soon after the rvalue goes away. That means an overhead of operations, including allocations and memory copying that after all, should not be necessary.

Enter the move constructor and move assignment operator. These two special functions take a T&& argument, which is an rvalue. Knowing that fact, they can modify the object, such as “stealing” the objects their pointers refer to. For instance, a container implementation (such as a vector or a queue) may have a pointer to an array of elements. When an object is instantiating from a temporary, instead of allocating another array, copying the values from the temporary, and then deleting the memory from the temporary when that is destroyed, we just copy the value of the pointer that refers to the allocated array, thus saving an allocation, copying a sequence of elements, and a later deallocation.

The following example shows a dummy buffer implementation. The buffer is identified by a name (just for the sake of showing a point revealed below), has a pointer (wrapper in an std::unique_ptr) to an array of elements of type T and variable that tells the size of the array.

template <typename T>
class Buffer 
{
   std::string          _name;
   size_t               _size;
   std::unique_ptr<T[]> _buffer;

public:
   // default constructor
   Buffer():
      _size(16),
      _buffer(new T[16])
   {}

   // constructor
   Buffer(const std::string& name, size_t size):
      _name(name),
      _size(size),
      _buffer(new T[size])
   {}

   // copy constructor
   Buffer(const Buffer& copy):
      _name(copy._name),
      _size(copy._size),
      _buffer(new T[copy._size])
   {
      T* source = copy._buffer.get();
      T* dest = _buffer.get();
      std::copy(source, source + copy._size, dest);
   }

   // copy assignment operator
   Buffer& operator=(const Buffer& copy)
   {
      if(this != &copy)
      {
         _name = copy._name;

         if(_size != copy._size)
         {
            _buffer = nullptr;
            _size = copy._size;
            _buffer = _size > 0 > new T[_size] : nullptr;
         }

         T* source = copy._buffer.get();
         T* dest = _buffer.get();
         std::copy(source, source + copy._size, dest);
      }

      return *this;
   }

   // move constructor
   Buffer(Buffer&& temp):
      _name(std::move(temp._name)),
      _size(temp._size),
      _buffer(std::move(temp._buffer))
   {
      temp._buffer = nullptr;
      temp._size = 0;
   }

   // move assignment operator
   Buffer& operator=(Buffer&& temp)
   {
      assert(this != &temp); // assert if this is not a temporary

      _buffer = nullptr;
      _size = temp._size;
      _buffer = std::move(temp._buffer);

      _name = std::move(temp._name);

      temp._buffer = nullptr;
      temp._size = 0;

      return *this;
   }
};

template <typename T>
Buffer<T> getBuffer(const std::string& name) 
{
   Buffer<T> b(name, 128);
   return b;
}
int main()
{
   Buffer<int> b1;
   Buffer<int> b2("buf2", 64);
   Buffer<int> b3 = b2;
   Buffer<int> b4 = getBuffer<int>("buf4");
   b1 = getBuffer<int>("buf5");
   return 0;
}

The default copy constructor and copy assignment operator should look familiar. What’s new to C++11 is the move constructor and move assignment operator, implemented in the spirit of the aforementioned move semantics. If you run this code you’ll see that when b4 is constructed, the move constructor is called. Also, when b1 is assigned a value, the move assignment operator is called. The reason is the value returned by getBuffer() is a temporary, i.e. an rvalue.

You probably noticed the use of std::move in the move constructor, when initializing the name variable and the pointer to the buffer. The name is actually a string, and std::string also implements move semantics. Same for the std::unique_ptr. However, if we just said _name(temp._name) the copy constructor would have been called. For _buffer that would not have been even possible because std::unique_ptr does not have a copy constructor. But why wasn’t the move constructor for std::string called in this case? Because even if the object the move constructor for Buffer is called with is an rvalue, inside the constructor it is actually an lvalue. Why? Because it has a name, “temp” and a named object is an lvalue. To make it again an rvalue (and be able to invoke the appropriate move constructor) one must use std::move. This function just turns an lvalue reference into an rvalue reference.

An alternative implementation:

template <typename T>
class Buffer
{
   std::string          _name;
   size_t               _size;
   std::unique_ptr<T[]> _buffer;

public:
   // constructor
   Buffer(const std::string& name = "", size_t size = 16):
      _name(name),
      _size(size),
      _buffer(size? new T[size] : nullptr)
   {}

   // copy constructor
   Buffer(const Buffer& copy):
      _name(copy._name),
      _size(copy._size),
      _buffer(copy._size? new T[copy._size] : nullptr)
   {
      T* source = copy._buffer.get();
      T* dest = _buffer.get();
      std::copy(source, source + copy._size, dest);
   }

   // copy assignment operator
   Buffer& operator=(Buffer copy)
   {
       swap(*this, copy);
       return *this;
   }

   // move constructor
   Buffer(Buffer&& temp):Buffer()
   {
      swap(*this, temp);
   }

   friend void swap(Buffer& first, Buffer& second) noexcept
   {
       using std::swap;
       swap(first._name  , second._name);
       swap(first._size  , second._size);
       swap(first._buffer, second._buffer);
   }
};

Installing Wt on Ubuntu

December 9, 2015 | Article | No Comments

Wt (pronounces as witty) is a C++ library for developing web application.

This article will discuss about installation of Wt on Ubuntu Linux, using package manager in specific. The prebuilt packages are made always updated to the newest Wt version so it would be the best for starting learning Wt.

Installing from the Official Package

Since Ubuntu Intrepid (8.10), official packages for Ubuntu are available. To install Wt, run:

aptitude install witty witty-dbg witty-dev witty-doc

This will automatically install all the required dependencies. If you only want the runtime library, you only need to install the witty package. The witty-dbg package contains the debug versions of the libraries. Make sure you install witty-dev (or libwtwhatever-dev) if you want to develop Wt applications.

The official package is usually a bit outdated due to the stabilization periods Debian and Ubuntu need prior to release. If you want to use the newest version of Wt and not build from source, read on.

Installing from Prebuilt Packages ghdg

Since Wt 2.0.3, unofficial packages for Ubuntu are being built by Pau Garcia i Quiles. To install Wt, add the Wt PPA to your repositories (check “Adding this PPA to your system” in the Wt PPA page). After adding the repository to your system, run:

 $ sudo apt-get update
 $ sudo aptitude install libwt*

This will automatically install all the required dependencies. If you only want the runtime library, you only need to install the libwt24, libwthttp24, libwtext24, etc packages (no -dev, -doc or -dbg packages). The libwt-dbg package contains the debug versions of the libraries.

These packages are built by the maintainer of the official Debian and Ubuntu packages and are always updated to the latest version of Wt. If you want to use the newest version of Wt and not build from source, this is the preferred method.

Please note in the past packages were named witty, witty-dev, witty-doc and witty-dbg. These package still exist but only as transitional packages and will be removed in the future. It is recommended that you install libwt*.

POSIX/ANSI Signal and Signal Handling in C++

December 9, 2015 | Article | No Comments

In Linux and UNIX world, signals is defined as notifications to a process that an event has occurred. In details, a signal is a software interrupt which delivered by process or kernel. Signal occur asynchronously, which means the process doesn’t know ahead of time when a signal will occur.

Signal can be sent in following scenario:

  1. by one process to another process (or to itself)
  2. by kernel to a process

When an application receive signal, it postpone the current activity and work other thing based on interrupt (signal) it receive. Any application has default behavior for some signal, i.e. a process terminated when it receives an interrupt SIGNINT signal by pressing keystroke Ctrl-C. A programmer can modify the behavior by his own code when application receive specific signal, which will be discussed in this article.

This article is tested using following tools & environment:

  1. Slackware64 14.0 (Linux)
  2. gcc 4.7.1

Note that not all signals can be handled.

The Theory

To handle a signal, one should define a callback function to manage the signal. This callback will be registered so it will change the default behavior of signal handler.

There are several types available which can be read on appendix A section.

Sending a signal from terminal can be down using a “kill” command. Despite of the name, kill is not only for killing an application (thought it is when you don’t specify any signal number). To send a signal, the following is the general syntax:

kill -s <signal-number> <pid>

With <signal-number> is the signal number as specified on table appendix A and pid is the process id we want to send to.

Handling a Signal using Signal Function

The easiest way to handling a signal is using signal function to register signal callback.

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

// Define the function to be called when Ctrl-C (SIGINT) signal is sent to process
void callback_sigint(int signum) {
    // prompt that we got SIGINT
    printf("We got signal %d\n", signum);

    // Terminate program
    exit(signum);
}

int main() {
    // Register signal and signal handler
    signal(SIGINT, callback_sigint);

    while (true) {
         printf("Program is processing stuff here.\n");
         sleep(1);
    }
    return 0;
}

Compile and followed by run:

gcc SignalExample.cpp -o SignalExample
./SignalExample

To terminate, press ctrl-c.

As you may suspect, the callback function is callback_sigint(). The callback function always has prototype as:

void signal_name (int signum);

The signum is a variable which hold the signal number we received. Thus, we can use a single signal handle to handle various signal sent. However this is beyond our league, but you should be able to figure it out.

If you are curious, the function signal has prototype:

void (*signal (int sig, void (*func)(int)))(int);

Handling a Signal using Registration and Handling Class

Now let’s see how can be implement the signal handling using Object Oriented Programming.

Here is our header file:

#ifndef __SIGNALHANDLER_HPP__
#define __SIGNALHANDLER_HPP__

#include <stdexcept>
using std::runtime_error

class SignalException : public runtime_error
{
public:
   SignalException(const std::string& _message)
      : std::runtime_error(_message)
   {}
};

class SignalHandler
{
protected:
    static bool mbGotExitSignal;

public:
    SignalHandler();
    ~SignalHandler();

    static bool gotExitSignal();
    static void setExitSignal(bool _bExitSignal);

    void        setupSignalHandlers();
    static void exitSignalHandler(int _ignored);

};
#endif

And this is our implementation:

#include <signal.h>
#include <errno.h>

#include "SignalHandler.hpp

bool SignalHandler::mbGotExitSignal = false;

/**
* Default Contructor.
*/
SignalHandler::SignalHandler()
{
}

/**
* Destructor.
*/
SignalHandler::~SignalHandler()
{
}

/**
* Returns the bool flag indicating whether we received an exit signal
* @return Flag indicating shutdown of program
*/
bool SignalHandler::gotExitSignal()
{
    return mbGotExitSignal;
}

/**
* Sets the bool flag indicating whether we received an exit signal
*/
void SignalHandler::setExitSignal(bool _bExitSignal)
{
    mbGotExitSignal = _bExitSignal;
}

/**
* Sets exit signal to true.
* @param[in] _ignored Not used but required by function prototype
*                     to match required handler.
*/
void SignalHandler::exitSignalHandler(int _ignored)
{
    mbGotExitSignal = true;
}

/**
* Set up the signal handlers for CTRL-C.
*/
void SignalHandler::setupSignalHandlers()
{
    if (signal((int) SIGINT, SignalHandler::exitSignalHandler) == SIG_ERR)
    {
        throw SignalException("!!!!! Error setting up signal handlers !!!!!");
    }
}

And let’s test it:

#include <iostream>
#include "SignalHandle.hpp"
using namespace std;

main()
{
  int iret;

  try
  {
    SignalHandler signalHandler;

    // Register signal handler to handle kill signal
    signalHandler.setupSignalHandlers();

    // Infinite loop until signal ctrl-c (KILL) received
    while(!signalHandler.gotExitSignal())
    {
        sleep(1);
    }

    iret = EXIT_SUCCESS;
  }
  catch (SignalException& e)
  {
    std::cerr << "SignalException: " << e.what() << std::endl;
    iret = EXIT_FAILURE;
  }
  return(iret);
}

To compile, use following commands:

g++ SignalHandle.cpp main.cpp -o Signal

Creating Own Signal Function

Now, let’s going deeper to learn how signal() function is implemented. We will try to mimic it by creating our own function which will behave like what signal() does. Let’s call it as regsignal().

The signal() is implemented using sigaction function. There is also a struct we need to fill, sigaction which allocate and fill.

#ifndef __REG_SIGNAL_HPP__
#define __REG_SIGNAL_HPP__

#include <signal.h>

/* for signal handler */
typedef void Sigfunc(int);

Sigfunc * regsignal(int signo, Sigfunc *func);

#endif

And the implementation

#include "RegSignal.hpp"

Sigfunc *
regsignal(int signo, Sigfunc *func)
{
	struct sigaction	act, oact;

	act.sa_handler = func;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
	if (signo == SIGALRM) {
#ifdef	SA_INTERRUPT
		act.sa_flags |= SA_INTERRUPT;	/* SunOS 4.x */
#endif
	} else {
#ifdef	SA_RESTART
		act.sa_flags |= SA_RESTART;		/* SVR4, 44BSD */
#endif
	}
	if (sigaction(signo, &act, &oact) < 0)
		return(SIG_ERR);
	return(oact.sa_handler);
}
/* end signal */

A little trivia.  signal() is historical function that predates Posix.1 and different implementations provide different signal semantics when it is called. Posix has explicitly spells out the semantics and recommend to use sigaction() and sigaction structure.

Appendix A

Types of signals:

SIGHUP 1 Hangup (POSIX)Report that user’s terminal is disconnected. Signal used to report the termination of the controlling process.
SIGINT 2 Interrupt (ANSI)Program interrupt. (Ctrl-C)
SIGQUIT 3 Quit (POSIX)Terminate process and generate core dump
SIGILL 4 Illegal Instruction (ANSI)Generally indicates that the executable file is corrupted or use of data where a pointer to a function was expected.
SIGTRAP 5 Trace trap (POSIX)
SIGABRT SIGIOT 6 Abort (ANSI) / IOT trap (4.2 BSD) Process detects error and reports by calling abort.
SIGBUS 7 BUS error (4.2 BSD)Indicates an access to an invalid address.
SIGFPE 8 Floating-Point arithmetic Exception (ANSI)This includes division by zero and overflow.The IEEE Standard for Binary Floating-Point Arithmetic (ANSI/IEEE Std 754-1985) defines various floating-point exceptions.
SIGKILL 9 Kill, unblockable (POSIX)Cause immediate program termination. Can not be handled, blocked or ignored.
SIGUSR1 10 User-defined signal 1
SIGSEGV 11 Segmentation Violation (ANSI)Occurs when a program tries to read or write outside the memory that is allocated for it by the operating system, dereferencing a bad or NULL pointer. Indicates an invalid access to valid memory.
SIGUSR2 12 User-defined signal 2
SIGPIPE 13 Broken pipe (POSIX)Error condition like trying to write to a socket which is not connected.
SIGALRM 14 Alarm clock (POSIX)Indicates expiration of a timer. Used by the alarm() function.
SIGTERM 15 Termination (ANSI)This signal can be blocked, handled, and ignored. Generated by “kill” command.
SIGSTKFLT 16 Stack fault
SIGCHLD SIGCLD 17 Child status has changed (POSIX)Signal sent to parent process whenever one of its child processes terminates or stops.
SIGCONT 18 Continue (POSIX)Signal sent to process to make it continue.
SIGSTOP 19 Stop, unblockable (POSIX)Stop a process. This signal cannot be handled, ignored, or blocked.
SIGTSTP 20 Keyboard stop (POSIX) Interactive stop signal. This signal can be handled and ignored. (ctrl-z)
SIGTTIN 21 Background read from tty (POSIX)
SIGTTOU 22 Background write to tty (POSIX)
SIGURG 23 Urgent condition on socket (4.2 BSD)Signal sent when “urgent” or out-of-band data arrives on a socket.
SIGXCPU 24 CPU limit exceeded (4.2 BSD)
SIGXFSZ 25 File size limit exceeded (4.2 BSD)
SIGVTALRM 26 Virtual Time Alarm (4.2 BSD).Indicates expiration of a timer.
SIGPROF 27 Profiling alarm clock (4.2 BSD)Indicates expiration of a timer. Use for code profiling facilities.
SIGWINCH 28 Window size change (4.3 BSD, Sun)
SIGIOSIGPOLL 29 I/O now possible (4.2 BSD)Pollable event occurred (System V).

Signal sent when file descriptor is ready to perform I/O (generated by sockets)

SIGPWR 30 Power failure restart (System V)
SIGSYS 31 Bad system call

As seen on /usr/include/bits/signum.h

You can also list the signal available by invoking following command:

kill -l

Appendix B

Useful function:

  • signal – ANSI C signal handling
  • raise – sends a signal to the current process
  • strsignal – return string describing signal (GNU extension)
  • psignal – print signal message
  • sigaction – POSIX signal handling functions (see the coding example)
  • sigsetops – POSIX signal set operations
  • sigvec – BSD software signal facilities
  • alarm – set an alarm clock for delivery of a signal

Building a Dynamic-Link Library using C++ MinGW

December 9, 2015 | Article | No Comments

Dynamic-Link Library or DLL, is implementation of shared library concept in Microsoft Windows and OS/2 operating system. Usually it has file extension DLL. The file formats for DLL are the same as for executable file – that is Portable Executable (PE) for 32-bit and 64-bit Windows, and New Executable (NE) for 16-bit Windows.

In this article, we will discuss about how we can create a .dll file using C++ and compiler MinGW.

The Files

We will use three files: my_dll.h, my_dll.cpp, and client.cpp

The my_dll.h is a header file where we place declarations:

// my_dll.h
#ifndef _MY_OWN_DLL_H__
#define _MY_OWN_DLL_H__
#ifdef __cplusplus
extern "C" {
#endif

#ifdef BUILD_MY_DLL
#define MY_DLL __declspec(dllexport)
#else
#define MY_DLL __declspec(dllimport)
#endif

void __stdcall MY_DLL hello(const char*s);

int MY_DLL Double(int x);

#ifdef __cplusplus
}
#endif

// This function is not declared extern "C"
void MY_DLL CppFunc(void);

// NOTE: This class must not be declared extern "C"
class MY_DLL MyClass {
public:
   MyClass() {}
   virtual ~MyClass() {}
   void func(void);
};
#endif

This one for my_dll.cpp will hold the implementation detail:

// my_dll.cpp
#include <stdio.h>
#include "my_dll.h"

__stdcall void hello(const char *s) {
   printf("Hello %s\n", s);
}
int Double(int x) {
   return (2*x);
}
void CppFunc(void) {
   puts("We are calling CppFunc");
}

void MyClass :: func(void) {
   puts("We are calling func() from class MyClass");
}

Those two files will be compiled as a single .dll file. Another source file, client.cpp, will be used as a “client”. This file will be compiled as a single executable file which will depend on the .dll file.

// client.cpp
#include <stdio.h>
#include "my_dll.h"

int main() {
   hello("World");
   printf("%d\n", Double(135));
   CppFunc();

   MyClass a;
   a.func();

   return 0;
}

Building DLL

To build the DLL, use following commands:

g++ -c -DBUILD_MY_DLL my_dll.cpp
g++ -shared -o my_dll.dll my_dll.o -Wl,--out-implib,libmy_dll.a

The -DBUILD_MY_DLL will be passed to compiler (see the .h file). This is because DLL’s functions need to be declared as “dllexport”, meaning that they will be “exported” from the DLL and available to client applications.

The “-shared” options tells the linker to create a DLL instead of an .exe, and the “–out-implib” linker option causes an import library to be created which is used later on.

Note:

The import library created by the “–out-implib” linker option is required if and only if the DLL shall be interfaced from some C/C++ compiler other than the MinGW toolchain. The MinGW toolchain is perfectly happy to directly link against the created DLL.

Building a Client Executable

To build the client, use following commands:

g++ -c client.cpp
g++ -o client.cpp client.o -L. -lmy_dll

The option “-L.” will add . (current directory) to where library will be searched. As we use current directory to store all the example, we will use this option. Then the “-lmy_dll” will tell the linker to link this code with library “libmy_dll.a”. The linker will search directory in current directory (remember the -L.) first.

We may built the same executable without an import library using following command:

g++ -o client.cpp client.o my_dll.dll

If this method work for your application, then there is usually no need for an import library. In my case, the application which doesn’t use import library result in smaller size.

Buildign and Using a DLL without the dllexport / dllimport Attributes

If you pass the “-no-undefined” and “–enable-runtime-pseudo-reloc” options to the linker, then you don’t have to add dllimport or dllexport attributes to the source code that the DLL is made with. All functions are imported /exported automatically by default, just like in unix;

There is an important thing to note with the above example functions:
Both hello(const char *) and Double(int) are surrounded by

#ifdef __cplusplus
extern "C" {
#endif
and

#ifdef __cplusplus
}
#endif

In the header file. This has the rather important consequence that their exported names use C-style name mangling (i.e. their names are “as is”).

The function CppFunc( void ) is not inside an extern “C” {…} block and thus uses C++-style name mangling. This has the consequence that the exported name depends on the compiler used to generate the DLL. It is by design that such generated names are different from compiler to compiler.

The important and often overlooked consequence of this is that from the above DLL only the functions hello(const char *) and Double(int) are seamlessly callable from e.g. MS VC++ while CppFunc(void) is not (assuming the DLL is created by MinGW).

A similar statement goes for C++ classes exported in a DLL, i.e. for the class MyClass. For further reading search the Web for the keyword “ABI” and possible in conjunction with “C++”. See also MixObjects.

A way to circumvent this problem is either using COM or create C-style wrapper functions to encapsulate the C++ ABI.

Social media & sharing icons powered by UltimatelySocial