Windows dev - fordsfords/fordsfords.github.io GitHub Wiki

I'm Unix-oriented C programmer. (Not C++, C.) But I sometimes like to make my code portable to Windows as well.

I have access to a Windows laptop with VS 2013. But I don't use it often enough to justify learning to be productive in it. All I really want is to run the compiler, not learn a whole software development ecosystem. So this procedure let me develop roughly like I do on Unix, and just use VS as a compiler.

I use GitHub Desktop, not git. And I use cygwin for my command-line (big surprise). Edit with vi in cygwin.

Note that git tries to do the right thing with line endings. So when you clone a project onto Windows, source file line endings are converted to cr/lf, and when you clone it onto Mac, the line endings are lf. For the most part, this is fine, BUT THIS INCLUDES SHELL SCRIPTS, WHICH MAKES CYGWIN VERY UNHAPPY! Change the .gitattributes file to be:

# Auto detect text files and perform LF normalization
* text=auto

*.sh text eol=lf

Of course, this means you have to name all your shell scripts to end with ".sh", which I do anyway, so that's fine.

This procedure assumes your GitHub project name is "nstm", and that the C files are right in the main directory. (I know, that doesn't scale well for bigger projects. I don't do bigger projects for Windows.)

  • From Cygwin: cd into GitHub project directory.
  • Start VS
  • File -> New -> Project
  • on left: expand "Visual C++", select Win32
    • Win32 Console Application
    • Name: nstm
    • Location (leave at c:\users\sford\documents\visual studio 2013\Projects)
    • Check: Create directory for solution.
    • "OK"
      • Next
      • Uncheck precompiled header
      • Uncheck SDK checks
      • Finish
  • "X" out the "nstm.cpp" file
  • From Cygwin: cpvs.sh (copies files into /cygdrive/c/users/sford/Documents/Visual\ Studio\ 2013/Projects/nstm/nstm)
  • From VS: If "solution explorer" is not displayed on right, View -> Solution Explorer
  • From solution explorer on right: right-click nstm, Add -> Existing Item
    • control-click the .c and .h files
    • Add
  • From solution explorer on right: right-click "nstm.cpp", remove
    • Delete
  • From solution explorer on right: right-click "stdafx.cpp", remove
    • Delete
  • File -> Save all
  • From solution explorer on right: right-click nstm, rebuild
  • From Cygwin: tstvs.sh (runs the executable)
Later, when restarting VS:
  • File -> Recent projects and solutions -> .../nstm.sln
Here's cpvs.sh:
#!/bin/sh
# cpvs.sh - copy *.c, *.h to visual studio project of same name.

DIR=`pwd`
PROJ=`basename $DIR`
VSPATH="/cygdrive/c/users/sford/Documents/Visual Studio 2013/Projects/$PROJ"
SRCPATH="$VSPATH/$PROJ"
EXEPATH="$VSPATH/Debug/$PROJ.exe"

if [ ! -d "$SRCPATH" ]; then echo "cpvs.sh: ERROR, SRCPATH not found: '$SRCPATH'"; exit 1; fi

cp *.c *.h "$SRCPATH/"

Here's tstvs.sh:

#!/bin/sh
# tstvs.sh - run visual studio program

DIR=`pwd`
PROJ=`basename $DIR`
VSPATH="/cygdrive/c/users/sford/Documents/Visual Studio 2013/Projects/$PROJ"
SRCPATH="$VSPATH/$PROJ"
EXEPATH="$VSPATH/Debug/$PROJ.exe"

if [ ! -f "$EXEPATH" ]; then echo "cpvs.sh: ERROR, EXEPATH not found: '$EXEPATH'"; exit 1; fi

"$EXEPATH"
⚠️ **GitHub.com Fallback** ⚠️