Difference between revisions of "C Compilers"

From Mech
Jump to navigationJump to search
Line 66: Line 66:
== Installing Visual Studio ==
== Installing Visual Studio ==


While we recommend the standard gcc c compiler, you may also use the compiler that comes with Microsoft Visual Studio. While free, it is a several GB download and install, so beware.
While we recommend the standard gcc c compiler, you may also use the compiler that comes with Microsoft Visual Studio. It is free, but a several GB download and install, so beware.


Do a search for the latest version of Microsoft Visual Studio Express for Windows Desktop. This year it is [http://www.microsoft.com/en-us/download/details.aspx?id=40787 version 2013].
Do a search for the latest version of Microsoft Visual Studio Express for Windows Desktop. This year it is [http://www.microsoft.com/en-us/download/details.aspx?id=40787 version 2013].
Line 75: Line 75:


[[Image:vs2.png|thumb|500px|Download the .exe file.|center]]
[[Image:vs2.png|thumb|500px|Download the .exe file.|center]]

Run the file, and install like any other program. This may take a while.

Visual Studio is a full IDE - powerful and convenient, but overkill for our needs. Instead of running VS Express, look for VS2013 x86 Native Tools Command Prompt.

[[Image:vs3.png|thumb|500px|Native Tools Command Prompt.|center]]

Make a new Hello World program to test the compiler. I changed mine to say "Hello world 2!" and saved it as hello2.c in my c:\example folder.

Run the Native Tools Command Prompt, and change directories to the example folder. Check to make sure your old hello.c and hello.exe are still there, with your new hello2.c. Use the command 'cl hello2.c' to compile the program, and run it with 'hello2'.

[[Image:vs4.png|thumb|500px|Hello2 with VS.|center]]

Npw you can program with Visual Studio in c!

Revision as of 12:57, 30 December 2013

An easy way to test C code without uploading onto a microcontroller is to run it directly on your computer. An IDE is not necessary, you can compile and run directly from the command line!

For Windows, we recommend gcc from MinGW, or the Microsoft compiler that comes with Visual Studio.

For Mac, we recommend Xcode.

Installing MinGW

Go to the MinGW homepage and click the "Download Installer" button in the top right of the screen.

Download MinGW.

This will take you to a sourceforge.net link. You do not have to click anything here (it's all ads), the download will start automatically after 5 seconds.

Run mingw-get-setup.exe.

mingw-get-setup.exe.

Click install. Let the files be saved in C:\MinGW, and say Continue.

Save in default location.

It will take a minute to install. When it is complete, click Continue.

MinGW Manager Setup Tool Installation complete.

This will bring you to the MinGW Installation Manager, where you select the Package you want to install. We are only interested in gcc, not fortan or g++ or any of that, so select 'mingw32-base' and say 'Mark for installation'. In the top left, click Installation -> Apply Changes, and click apply in the pop-up window.

Select mingw32-base to get gcc.

This will download the package, extract and install gcc. When complete, click Close.

Scheduled Changes complete.

You can close the Installation Manager.

Verify that you have gcc.exe installed by opening C:\MinGW\bin.

gcc.exe.

Before we compile code, we need to tell Windows where to find gcc.exe. Open the Control Panel, and click System and Security, click System, and click on Advanced system settings.

Control Panel -> System and Security -> System.

Click the Environment Variables... button.

Environment Variables...

Under System variables, find Path.

Environment Variables -> Path

Click Edit..., and at the end, add C:\MinGW\bin; and say OK to all the windows.

Add C:\MinGW\bin; to the Path

Now we can compile some code! In Windows, I like to use Notepad++ as a text editor. Here I have saved our HelloWorld code as hello.c in C:\example.

hello.c in Notepad++

Now I can open the Command Window (cmd.exe), use 'cd' to change the directory to C:\example, use 'dir' to verify that hello.c is there, and use 'gcc' to compile the code (gcc hello.c -o hello). This creates hello.exe, so if I type 'hello', the code runs, and I see "Hello World!"

Compiled Hello World!

Now you are ready to start coding.

Installing Visual Studio

While we recommend the standard gcc c compiler, you may also use the compiler that comes with Microsoft Visual Studio. It is free, but a several GB download and install, so beware.

Do a search for the latest version of Microsoft Visual Studio Express for Windows Desktop. This year it is version 2013.

Microsoft Visual Studio Express for Windows Desktop.

There are two installation options - download a .iso file, cut it to DVD, and install from the DVD, or download a .exe file and run it. I suggest the .exe file. Select it and click Next.

Download the .exe file.

Run the file, and install like any other program. This may take a while.

Visual Studio is a full IDE - powerful and convenient, but overkill for our needs. Instead of running VS Express, look for VS2013 x86 Native Tools Command Prompt.

Native Tools Command Prompt.

Make a new Hello World program to test the compiler. I changed mine to say "Hello world 2!" and saved it as hello2.c in my c:\example folder.

Run the Native Tools Command Prompt, and change directories to the example folder. Check to make sure your old hello.c and hello.exe are still there, with your new hello2.c. Use the command 'cl hello2.c' to compile the program, and run it with 'hello2'.

Hello2 with VS.

Npw you can program with Visual Studio in c!