Difference between revisions of "Language - C Windows GCC"

From Phidgets Support
Jump to: navigation, search
(MinGW)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{NoTitle}}
+
[[Category:Language]]{{NoTitle}}
{{Language_-_C_Dev_Environment_Table}}
+
 
{|
 
{|
 
|style="vertical-align:middle; width: 60%;"|
 
|style="vertical-align:middle; width: 60%;"|
Line 15: Line 14:
 
{{Language_-_C_Intro|Windows|GCC}}
 
{{Language_-_C_Intro|Windows|GCC}}
  
==Use Our Examples==
+
{{Finding Code Samples|C}}
One of the best ways to start programming with Phidgets is to use our example code as a guide. In order to run the examples, you will need to download and install either [http://www.mingw.org/ MinGW] or [https://www.cygwin.com/ Cygwin].
+
  
 +
==Using the Code Samples==
 +
To use the code sample from the [{{server}}/?view=code_samples&lang=C Code Samples] page, you can click the '''Download Example''' button to download a C file with the sample code.
  
Now that you have either MinGW or Cygwin installed, select an example that will work with your Phidget:
+
[[Image:C_Sample_Code_Download.png|link=|center|border|800px]]
*{{SampleCode|C|C Examples}}
+
  
===Cygwin===
 
  
If you are using Cygwin, navigate to the folder where the example is and open the command prompt. Enter the following command to compile the example:
+
Once you have the example, you can compile and run your code:
  
====Cygwin x86====
+
==Compile and Run==
<syntaxhighlight lang='bash'>
+
gcc example.c ../Common/PhidgetHelperFunctions.c -o example -I"/cygdrive/c/Program Files/Phidgets/Phidget22" -I"../Common" -L"/cygdrive/c/Program Files/Phidgets/Phidget22/x86" -lphidget22
+
</syntaxhighlight>
+
 
+
====Cygwin x64====
+
<syntaxhighlight lang='bash'>
+
gcc example.c ../Common/PhidgetHelperFunctions.c -o example -I"/cygdrive/c/Program Files/Phidgets/Phidget22" -I"../Common" -L"/cygdrive/c/Program Files/Phidgets/Phidget22" -lphidget22
+
</syntaxhighlight>
+
 
+
===MinGW===
+
 
+
If you are using MinGW, navigate to the folder where the example is and open the command prompt. Enter the following command to compile the example:
+
<syntaxhighlight lang='bash'>
+
gcc example.c ../Common/PhidgetHelperFunctions.c -o example -I"C:/Program Files/Phidgets/Phidget22" -I"../Common" -L"C:/Program Files/Phidgets/Phidget22/x86" -lphidget22
+
</syntaxhighlight>
+
 
+
After running the commands above for either Cygwin or MinGW, an executable file called ''example.exe'' will be created. Enter the following command to run the example:
+
<syntaxhighlight lang='bash'>
+
example.exe
+
</syntaxhighlight>
+
 
+
{{Edit_the_Examples}}
+
 
+
==Editing the Examples==
+
{{Language_-_C_Editing_The_Examples}}
+
 
+
==Write Code==
+
{{Language_-_C_Write_Code}}
+
 
+
==Setting up a New Project==
+
 
In order compile C programs from the command prompt, you will need to download and install either [http://www.mingw.org/ MinGW] or [https://www.cygwin.com/ Cygwin].
 
In order compile C programs from the command prompt, you will need to download and install either [http://www.mingw.org/ MinGW] or [https://www.cygwin.com/ Cygwin].
  
When you are building a project from scratch, or adding Phidget functionality to an existing project, you'll need to configure your development environment to properly link the Phidget C library.
+
Next, navigate to the folder where your code is and open the command prompt.
  
To include the Phidget C library, add the following line to your code:
+
The specific command you will use depends on your compiler of choice. Select the tab for your compiler below, and enter the command within to compile your program, substituting "example" for the name of your C file:
<syntaxhighlight lang='C'>
+
#include <phidget22.h>
+
</syntaxhighlight>
+
 
+
===Cygwin===
+
 
+
If you are using Cygwin, navigate to the folder where the example is and open the command prompt. Enter the following command to compile your program, substituting "example" for the name of your C file:
+
  
====Cygwin x86====
+
<tabber>
 +
Cygwin x86=
 
<syntaxhighlight lang='bash'>
 
<syntaxhighlight lang='bash'>
 
gcc example.c -o example -I"/cygdrive/c/Program Files/Phidgets/Phidget22"-L"/cygdrive/c/Program Files/Phidgets/Phidget22/x86" -lphidget22
 
gcc example.c -o example -I"/cygdrive/c/Program Files/Phidgets/Phidget22"-L"/cygdrive/c/Program Files/Phidgets/Phidget22/x86" -lphidget22
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
|-|
====Cygwin x64====
+
Cygwin x64=
 
<syntaxhighlight lang='bash'>
 
<syntaxhighlight lang='bash'>
 
gcc example.c -o example -I"/cygdrive/c/Program Files/Phidgets/Phidget22" -L"/cygdrive/c/Program Files/Phidgets/Phidget22" -lphidget22
 
gcc example.c -o example -I"/cygdrive/c/Program Files/Phidgets/Phidget22" -L"/cygdrive/c/Program Files/Phidgets/Phidget22" -lphidget22
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
|-|
===MinGW===
+
MinGW=
 
+
If you are using MinGW, navigate to the folder where the example is and open the command prompt. Enter the following command to compile the example:
+
 
<syntaxhighlight lang='bash'>
 
<syntaxhighlight lang='bash'>
 
gcc example.c -o example -I"C:/Program Files/Phidgets/Phidget22" -L"C:/Program Files/Phidgets/Phidget22/x86" -lphidget22
 
gcc example.c -o example -I"C:/Program Files/Phidgets/Phidget22" -L"C:/Program Files/Phidgets/Phidget22/x86" -lphidget22
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</tabber>
  
After running the commands above for either Cygwin or MinGW, an executable file called ''example.exe'' will be created. Enter the following command to run the example:
+
 
 +
After running the commands above for either Cygwin or MinGW, an executable file called ''example.exe'' will be created. Enter the following command to run the program:
 
<syntaxhighlight lang='bash'>
 
<syntaxhighlight lang='bash'>
 
example.exe
 
example.exe
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Success! The project now has access to Phidgets.
+
{{Language Page What's Next}}
 
+
{{Language_-_C_Further_Reading}}
+

Latest revision as of 21:07, 16 October 2019

Language - C

Windows with GCC

Welcome to using Phidgets with C! By using C, you will have access to the complete Phidget22 API, including events.

GCC is a compiler system for originally written for GNU, and is the standard compiler on unix-like operating systems. It is available on Windows by using tools like MinGW or Cygwin to allow compilation of C programs from the command line.

Install Phidget Drivers for Windows

Before getting started with the guides below, ensure you have the following components installed on your machine:

  1. You will need the Phidgets Windows Drivers

Finding Code Samples

To find the code sample to use for your Phidget, navigate to the Code Samples page and select your device from the drop-down menu.

C sample code.jpg


Once you select your device, the code sample generator will give you a working code sample, and a selection of options to customize it to your needs.

Using the Code Samples

To use the code sample from the Code Samples page, you can click the Download Example button to download a C file with the sample code.

C Sample Code Download.png


Once you have the example, you can compile and run your code:

Compile and Run

In order compile C programs from the command prompt, you will need to download and install either MinGW or Cygwin.

Next, navigate to the folder where your code is and open the command prompt.

The specific command you will use depends on your compiler of choice. Select the tab for your compiler below, and enter the command within to compile your program, substituting "example" for the name of your C file:

gcc example.c -o example -I"/cygdrive/c/Program Files/Phidgets/Phidget22"-L"/cygdrive/c/Program Files/Phidgets/Phidget22/x86" -lphidget22

gcc example.c -o example -I"/cygdrive/c/Program Files/Phidgets/Phidget22" -L"/cygdrive/c/Program Files/Phidgets/Phidget22" -lphidget22

gcc example.c -o example -I"C:/Program Files/Phidgets/Phidget22" -L"C:/Program Files/Phidgets/Phidget22/x86" -lphidget22


After running the commands above for either Cygwin or MinGW, an executable file called example.exe will be created. Enter the following command to run the program:

example.exe

What's Next?

Now that you have set up Phidgets to work with your programming environment, we recommend you read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.Next Arrow.png