Yolinux.com Tutorial

MS/Visual Studio and Visual C++ Tips, Best Practices and Pitfalls in a Linux Environment:

Tips and best practices for users of the Microsoft Visual C++ IDE when integrating in a cross platform world. This tutorial covers some pitfalls this tool causes. (Baseline MS/VStudio 7.1 or MS/VC++ 6.0)

Many corporate environments supply software developers with Microsoft Visual Studio Pro and tools so this is a topic one can not ignore. It is my hope that these tips will be used so that Microsoft Visual C++ users won't mess up the files on your project too badly.


Related YoLinux tutorials:

°Software development tools

°STL: vector, list

°STL: string

°C++ memory corruption, leaks

°Advanced VI

°Emacs and C/C++

°C++ Info, links

°Clearcase Commands

°YoLinux Tutorials Index



Free Information Technology Magazine Subscriptions and Document Downloads


Free Information Technology Software and Development Magazine Subscriptions and Document Downloads


Tips and Best Practices:
  1. When developing with Microsoft IDE's, please DO NOT USE PRE-COMPILED HEADERS for cross platform code. i.e. Don't use stdafx.h. It doesn't always work on non-Microsoft platforms as it is not part of the C++ standard.
    After creating the MS/Visual C++ project select:
    • "Project" + "Settings" + "C++" tab:
    • Category: "Precompiled Headers". Check "not using precompiled headers"

  2. Using the editor in MS/VC++ will sometimes remove the "end of file" as perceived by UNIX (ctrl-Z vs ctrl-D) You might leave a blank line at the end of your files. Not just a carriage return but actual blank spaces on the last line.

  3. PITFALL: MS/Visual studio will add ^M (control M) as a line termination. It has been used by some to edit UNIX shell scripts. The first line of a UNIX shell script denotes the shell used.
     
    #!/bin/bash
    After editing with Microsoft IDE:
     
    #!/bin/bash^M
    When trying to execute the shell script the system responds vaguely:
     
    ": bad interpreter: No such file or directory"

  4. Using VI to "clean" or "restore" a source code file to usibility:

    Remove ^M:

     
    :1,$ s/^V^M//

    Repair Tab indentation. MS/VC++ uses a tab to represent 4 spaces (default). Most other systems use a Tab for 7 spaces.
    Turn Tabs into four blank spaces to remove abiguity and restore indentation:

     
    :1,$ s/^VTab/ /g

  5. Use white spaces for indentation instead of Tabs:

    • Visual C++ 6.0: Select "Tools" + "Options..." + the tab marked "Tabs". Select the option "Insert spaces" to avoid the automatic insertion of tabs for indentation. this will be applied to all files for "File type:" "Default".
      Select the "OK" button.

    • Visual Studio 7.1: Select "Tools" + "Options...". Select from the tree the folder "Text Editor" + "C/C++" (or which ever type is suitable) + "Tabs". Select the radio button "Insert Spaces"

      Visual Studio setting for spaces instead of tabs

  6. Restore or "repair" sections of code indented by tabs:
    • Select code to "restore". (ctrl-a for the whole file)
    • Select "Edit" + "Advanced" + "Untabify Selection"

  7. Tip to view white spaces in file:
    Select "Edit" + "Advanced" + "View white space"
    or
    • Visual C++ 6.0: Ctrl+Shift+8
    • Visual Studio 7.1: Ctrl-R, Ctrl-W

  8. Use macros to fix MS/Windows platform dependancies: (i.e.)
     
    #ifdef WIN32
    char *file_system_root = "C:\\";
    char *dir_separator = "\";
    #else
    char *file_system_root = "/";
    char *dir_separator = "/";
    #endif
    If using GTK+ cross platform API you can also use the C macro G_DIR_SEPARATOR.

    And vice-versa:

     
    #ifndef WIN32
    #include <syslog.h>
    #endif
  9. If using the Subversion CM system, configure Subversion to remove "^M" upon check-in. See the YoLinux Tutorial: Subversion Server and Trac Server Installation and Configuration.

My choice is to avoid the Microsoft IDE editor altogether but if you insist, please be aware of the previous pitfalls and fixes.

Return to http://YoLinux.com for more Linux links, information and tutorials
This page is the YoLinux Tutorial Index
Feedback Form

Copyright © 2004, 2006 by Greg Ippolito