Language - C macOS GCC: Difference between revisions

From Phidgets Support
No edit summary
No edit summary
Line 8: Line 8:
Welcome to using Phidgets with C! By using C, you will have access to the complete Phidget22 API, including events.
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. GCC is readily available on macOS, and can be used to compile C programs from the terminal.
GCC is a compiler system originally written for GNU, and is the standard compiler on unix-like operating systems. GCC is readily available on macOS, and can be used to compile C programs from the terminal.
|{{TOC limit|2}}
|{{TOC limit|2}}
|}
|}
{{Language_-_C_Intro|macOS|GCC}}
{{Language_-_C_Intro|macOS|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. You likely have gcc installed on your macOS machine already, but if not, you can easily get it by downloading [https://developer.apple.com/xcode/ Xcode].


Next, select an example that will work with your Phidget:
==Using the Code Samples==
*{{SampleCode|C|C Examples}}
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.


[[Image:C_Sample_Code_Download.png|link=|center|800px]]


To compile the example program, enter the following command in the terminal:
<syntaxhighlight lang='bash'>
gcc example.c ../Common/PhidgetHelperFunctions.c -o example -F /Library/Frameworks -framework Phidget22 -I /Library/Frameworks/Phidget22.framework/Headers -I ../Common
</syntaxhighlight>
Finally, run the program by entering the following command in the terminal:
<syntaxhighlight lang='bash'>
./example
</syntaxhighlight>
[[Image:c_mac_gcc.png|link=|center|850px]]


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


==Editing the Examples==
==Compile and Run==
{{Language_-_C_Editing_The_Examples}}


==Setting up a New Project==
To compile C programs, you will need gcc. You likely have gcc installed on your macOS machine already, but if not, you can easily get it by downloading [https://developer.apple.com/xcode/ Xcode].
To compile C programs, you will need gcc. You likely have gcc installed on your macOS machine already, but if not, you can easily get it by downloading [https://developer.apple.com/xcode/ Xcode].
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 include the Phidget C library, add the following line to your code:
<syntaxhighlight lang='C'>
#include <phidget22.h>
</syntaxhighlight>


To compile the program, enter the following command in the terminal, substituting "example" for the name of your C file:
To compile the program, enter the following command in the terminal, substituting "example" for the name of your C file:
Line 58: Line 37:
</syntaxhighlight>
</syntaxhighlight>


Success! The project now has access to Phidgets.
Success! The project is now running with Phidgets.


{{Language Page What's Next}}
{{Language Page What's Next}}

Revision as of 21:14, 16 October 2019

Language - C

macOS 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 originally written for GNU, and is the standard compiler on unix-like operating systems. GCC is readily available on macOS, and can be used to compile C programs from the terminal.

Install Phidget Drivers for macOS

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

  1. You will need the Phidgets macOS 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

To compile C programs, you will need gcc. You likely have gcc installed on your macOS machine already, but if not, you can easily get it by downloading Xcode.

To compile the program, enter the following command in the terminal, substituting "example" for the name of your C file:

gcc example.c -o example -F /Library/Frameworks -framework Phidget22 -I /Library/Frameworks/Phidget22.framework/Headers

Finally, run the program by entering the following command in the terminal:

./example

Success! The project is now running with 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