Compiling and Running a C Program in Bash on Windows 10

Perhaps the best known deep neural network tool is TensorFlow. I use Windows machines the vast majority of the time, but TensorFlow is designed for Linux systems. So, instead of trying to get TensorFlow to work directly on Windows (which is possible, but very tricky), I thought I’d try to run TensorFlow inside a Bash shell running on Windows. You can think of Bash on Windows as a little Linux environment (Ubuntu Linux to be more specific).

I knew that I was going to run into many problems, so I figured I’d experiment a bit first by getting a C language program to compile and run in Bash on Windows. The first step was to install Bash on Windows. There are several good guides you can find on the Internet but briefly: Control Panel | Programs | Windows Features | “Windows Subsystem for Linux (Beta)” | OK. After a reboot, Run | Bash -> Do you want to install? -> Y. Then after the install completes, you must create a Linux User (I used “jamccaff”) and a password for the user.

Next I needed a C compiler. In a Bash shell I entered the command:

$ sudo apt-get install gcc

The ‘$’ is the Bash prompt. The sudo means “superuser do” which is sort of a Linux “Run as administrator”. The apt-get is “Advanced Packaging Tool” and “gcc” is the GNU C/C++ compiler. The Linux subsystem stores files in hidden directory C:\Users\(user)\AppData\lxss so I’m assuming that gcc went there.


To create my test program, I installed the Notepad++ editor. I probably could have used the built-in Notepad but because of the differences between Windows newlines and Linux newlines I figured Notepad++ might be a safer choice. Installing Notepad++ is quick and easy. I selected the 32-bit version, again because I figured it’d be a safer choice.

Next I launched Notepad++ and typed in a minimal C program and named it aloha.c and then saved it on my Windows filesystem at C:\Data\Junk\C_Programs — the Bash shell can access the Windows filesystem so I didn’t have to save my file in some exotic location.

Next, in the Bash shell, I navigated to the directory containing the aloha.c file by using Linux cd commands. In the Linux system this directory is /mnt/c/Data/Junk/C_Programs. I compiled and ran my C program by entering the commands:

$ gcc -o aloha.o aloha.c
$ ./aloha.o
Aloha World!


And it worked. OK, now, when I find some free time, I’ll continue the process of trying to compile and run TensorFlow in Bash on Windows.


This entry was posted in Machine Learning. Bookmark the permalink.