Difference between revisions of "Language - C Windows Visual Studio"

From Phidgets Support
Jump to: navigation, search
Line 1: Line 1:
[[Category:Language]]{{NoTitle}}
+
{{NoTitle}}
 
{|
 
{|
 
|style="vertical-align:middle; width: 60%;"|
 
|style="vertical-align:middle; width: 60%;"|
Line 12: Line 12:
 
|}
 
|}
  
{{Language_-_C_Intro|Windows|Visual Studio}}
+
{{Language_-_C_Intro|Windows|Visual Studio|link=}}
  
==Use Our Examples==
+
==Using Phidgets in Your Programs==
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 [https://www.visualstudio.com/ Microsoft Visual Studio].
+
  
 +
There are two main ways you can go about adding Phidgets to your programs in Visual Studio. You can either start from a working project provided by the Phidgets code sample generator, or you can add the Phidgets libraries to an existing Visual Studio project.
  
Now that you have Microsoft Visual Studio installed, select an example that will work with your Phidget:
+
Select your preferred method below for instructions:
*{{SampleCode|C|C Examples}}
+
  
  
Open the example project by unzipping the example folder, and opening the <code>ExampleName.sln</code> file in the ExampleName folder.
+
<tabber>
 +
Using Code Samples=
 +
{{Finding Code Samples|C}}
  
Once the project is open, start the example by pressing the ''Local Windows Debugger'' button:
+
==Using the Code Samples==
  
 +
You can download a working '''Visual Studio''' project for '''C''' directly from the [{{server}}/?view=code_samples&lang=C Code Samples] page. Simply make your selections, and click the '''Visual Studio Project''' button under '''Downloads'''.
  
[[Image: c_vs_run.png|link=|center]]
+
[[File:C_Visual_Studio_Sample_Download.jpg|center|border|800px]]  
  
  
The application will open the Phidget, list basic information about the Phidget, and demonstrate the Phidget's functionality. Here is an example of an Accelerometer channel on a Spatial Phidget:
+
Open the example project and start the example by pressing the ''Start'' button:
  
 +
[[File:Csharp_visualstudio_run.png ‎|link=|center]]
  
[[Image: c_vs_output.PNG|link=|center]]
+
|-|
 
+
Adding Phidgets to an Existing Project=
 
+
===Unresolved External Symbol===
+
If you try to compile the program and the linker throws a number of errors called <code>unresolved external symbol</code>, then it is likely the project is trying to run using the wrong platform. Select your platform from the drop-down list, as shown in the image below.
+
 
+
[[Image:VS_Select_Platform.jpg|link=|center]]
+
 
+
{{Edit_the_Examples}}
+
 
+
==Editing the Examples==
+
{{Language_-_C_Editing_The_Examples}}
+
  
 
==Setting up a New Project==
 
==Setting up a New Project==
Line 52: Line 45:
 
Create a new Win32 Console application:
 
Create a new Win32 Console application:
  
[[Image:C_vs_newproject.PNG|link=|center|850px]]
+
[[Image:C_vs_newproject.PNG|link=|center|800px]]
  
  
Line 62: Line 55:
 
If you are using a 64-bit machine, select x64, otherwise, keep x86:
 
If you are using a 64-bit machine, select x64, otherwise, keep x86:
  
[[Image:C_vs_configuration.png|link=|center|850px]]
+
[[Image:C_vs_configuration.png|link=|center|800px]]
  
  
 
Next, add a new item to your source folder:
 
Next, add a new item to your source folder:
  
[[Image:C_vs_additem.png|link=|center|850px]]
+
[[Image:C_vs_additem.png|link=|center|800px]]
  
  
 
Give the source file a descriptive name and continue:
 
Give the source file a descriptive name and continue:
  
[[Image:C_vs_addsource.PNG|link=|center|850px]]
+
[[Image:C_vs_addsource.PNG|link=|center|800px]]
  
  
 
Access the project's properties:
 
Access the project's properties:
  
[[Image:C_vs_propertie.png|link=|center|850px]]
+
[[Image:C_vs_propertie.png|link=|center|800px]]
  
  
Line 84: Line 77:
  
  
[[Image:C_vs_additionalinclude.png|link=|center|850px]]
+
[[Image:C_vs_additionalinclude.png|link=|center|800px]]
  
  
Line 91: Line 84:
  
  
[[Image:C_vs_additionadepend.png|link=|center|850px]]
+
[[Image:C_vs_additionadepend.png|link=|center|800px]]
  
 
Finally, include the Phidget library in your code, and any other header files:
 
Finally, include the Phidget library in your code, and any other header files:
Line 99: Line 92:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
[[Image:C_vs_finished.PNG|link=|center|850px]]
+
[[Image:C_vs_finished.PNG|link=|center|800px]]
  
Success! The project now has access to Phidgets.
 
  
== Common Issues ==
+
Success! The project now has access to Phidgets.
  
{{Collapse
+
</tabber>
|
+
* Linker error when using examples with a non-English version of Windows
+
|The example projects, by default finds the phidget22.h and phidget22.lib in ${SystemDrive}\Program Files\Phidgets\Phidget22. If you are using a non US-English version of Windows, the Phidget drivers may be installed into a different location. To resolve, you will have to modify the paths to these two files.
+
}}
+
  
 
{{Language Page What's Next}}
 
{{Language Page What's Next}}

Revision as of 21:05, 16 October 2019

Language - C

Windows with Visual Studio

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

Visual Studio is an IDE provided by Microsoft that can be used to develop code in a wide variety of programming languages, including C/C++.

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

Using Phidgets in Your Programs

There are two main ways you can go about adding Phidgets to your programs in Visual Studio. You can either start from a working project provided by the Phidgets code sample generator, or you can add the Phidgets libraries to an existing Visual Studio project.

Select your preferred method below for instructions:


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

You can download a working Visual Studio project for C directly from the Code Samples page. Simply make your selections, and click the Visual Studio Project button under Downloads.

C Visual Studio Sample Download.jpg


Open the example project and start the example by pressing the Start button:

Csharp visualstudio run.png

Setting up a New Project

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. To begin:


Create a new Win32 Console application:

C vs newproject.PNG


Select an empty project and finish:

C vs emptyproject.PNG


If you are using a 64-bit machine, select x64, otherwise, keep x86:

C vs configuration.png


Next, add a new item to your source folder:

C vs additem.png


Give the source file a descriptive name and continue:

C vs addsource.PNG


Access the project's properties:

C vs propertie.png


Next, navigate to Configuration Properties -> C/C++ -> General and add the following line to the additional include directories:

  • C:\Program Files\Phidgets\Phidget22


C vs additionalinclude.png


Navigate to Configuration Properties -> Linker -> Input and add the following line to the additional dependencies:

  • C:\Program Files\Phidgets\Phidget22\phidget22.lib


C vs additionadepend.png

Finally, include the Phidget library in your code, and any other header files:

#include <phidget22.h>
C vs finished.PNG


Success! The project now has access to Phidgets.

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