Convert a Visual Studio 2010 project to a Linux project

In short

The major steps are:

  1. Remove stdafx.h and improve dependency
  2. Modify signature for “main” function
  3. Setup Makefile in Linux system.
  4. Modify implementation details

Modify signature for “main” function

Modify the main function name from “int _tmain(int argc, _TCHAR* argv[])” to “int main()”. “_tmain” is not well recognized by Linux compilers like g++.

Remove stdafx.h and improve dependency

For Visual Studio projects, stdafx.h is used as a ‘precompiled header’ for projects with Windows dependencies. Despite that its existence does not hinder Linux compilation, it’s not a common file for the Linux system. See here for more details.

To remove this file, first check the project’s properties. Right click on project name->Properties->Configuration properties->C/C++ -> Precompiled Headers. Set the value of ‘Precompiled header’ to ‘Not Using Precompiled Headers’

Next step is to remove dependencies on ‘stdafx.h’ and ‘stdafx.cpp’ . Organize header files according to Google Style Guide and here.

Substitute ‘pragma once’

The syntax for #ifndef is:

# ifndef \_HEADER\_NAME_H

# define \_HEADER\_NAME_H

.. {Your code here} ..

# endif

According to wikipedia:

In the C and C++ programming languages, #pragma once is a non-standard but widely supported preprocessor directive designed to cause the current source file to be included only once in a single compilation. Thus, #pragma once serves the same purpose as #include guards, but with several advantages, including: less code, avoidance of name clashes, and sometimes improved compile speed.

Considering ‘pragma once’ is widely supported, whether to substitute or not becomes more a problem of personal taste.

Modify implementation details

  1. nullptr is not supported by g++ unless C++0x support is specified. See http://stackoverflow.com/questions/10033373/c-error-nullptr-was-not-declared-in-this-scope-in-eclipse-ide
  2. sprintf_s is none-standard function and not supported by g++. Reference Use “snprintf” instead. Example: snprintf(charArrayAgain, 2, "%d", number);from here
  3. most important: g++ is more strict on standards. Following stuff are not accepted by g++ by passes Visual Studio compilation with warning:

    • Implicit conversion from a non-const object to a const object.
    • Giving an rvalue as input where a non-const reference is required. This is similar to a. For a and b, see here for reference.
    • multiple layer of template: List<List<edge>>, the >> is not supported by g++ unless c++ 0x support specified.
    • Conversion between string and char[] . Some implicit conversion is supported by Microsoft but g++ holds tight on it.
    • friend class Sth; the class keyword is mandatory for g++
  4. The meaning for -Wreorder of g++: here

Leave a Reply

Your email address will not be published. Required fields are marked *