Cross platform coding issues in C++ for Windows/linux

 
Cross platform coding issues in C++ for Windows/linux

Some of code issues traced and workout for Windows/linux by C++

1. Accessing system related functionalities:

  • Windows and Linux have following different memory structure. So need to develop separate code for these platform
  • for example:
    #include <sys/sysinfo.h>
    ..
    struct sysinfo memInfo; //used to get memory info

    In windows compilation it raised error. So, This function is Linux-specific, and should not be used in programs intended to be portable.
  • To get Windows related memory structure, include ‘Windows.h’
  • ..Tips to skip compilation error following lines can be used:
    #ifdef __linux__
    //
    #else
    //
    #endif

 

2. Unordered map:

  • Normal code:
    #include <unordered_map>

    unordered_map<long,long> map_FAZIT3130;
  • Cross platform code:
    #include <tr1/unordered_map>
    using namespace std::tr1;

    unordered_map<long,long> map_FAZIT3130;

 

3. Threads:

  • Compiler: MinGW (“Minimalistic GNU for Windows”)
  • MinGW hasn’t implemented threads for Windows yet.
  • Standard thread is not working well in windows platform. So need to use ‘Pthread’ for both windows, linux platforms.
  • Conversion of code to/fro std:thread and pthread is simple. Hence both having simple identical codes.
  • std:thread:
    #include <thread>

    void processFile(long threadid){ //thread function
    //some code
    }

    std::thread threads[NUM_THREADS]; //thread declaration
    threads[threadindex] = std::thread(processFile, threadindex); //thread creation
    usleep(100);

    for(int t=0; t<NUM_THREADS; t++) { //thread must be joined after creation
    threads[t].join();
    }
  • pthread:
    #include <pthread.h>

    void processFile(long threadid){ //thread function
    //some code
    pthread_exit(NULL); //thread must be exited here
    }

    pthread_t threads[NUM_THREADS]; //thread declaration
    int rc = pthread_create(&threads[t], NULL, processFile, threadindex);
    //here join not be needed** also in compilation -pthread must be specified
    Ex: g++ -std=c++0x -o bin/main -pthread main.cpp
  • If your target platform is Linux only – c++11 std::thread is fine; if you also need Windows or other UNIX – boost::thread is the way to go (OR) can use Pthread

 

4. Directory creation

  • //For directory creation we can use the following function:
    void make_directory(const char* name)
    {
    #ifdef __linux__
    mkdir(name, 777);
    #else
    _mkdir(name);
    #endif
    }

 

5. Some more points:

  • Can’t calculate the size of pointer array in c++
  • delete() of fixed arrays raises segmentation fault in unix, but in windows compilation its working
  • To calculate size of the fixed size array:
    lngSize = sizeof(arr_temp) / sizeof(arr_temp[0]);
  • Unique function returns address of last element of modified array after duplicates removed.
    To find valid elements in array after duplicated removed:
    noele=unique(arr_temp, arr_temp + arraysize)-arr_temp;  //arr_temp is address of first element
Guruntamil Pandian G
Latest posts by Guruntamil Pandian G (see all)