Setting up Code::Blocks IDE to use the D2XX library
This is not exactly the third installment of the tutorial.You can skip this section if you are comfortable in using Code::Blocks IDE or if you prefer to program using the command line as shown in the previous two articles ( 1 , 2 ).This article teaches to configure Code::Blocks IDE(13.12) with MinGW (GCC) Compiler and to link D2XX library from FTDI with it.
Code::Blocks is a free open source IDE for C,C++ and Fortran,It is built on a plugin framework by which its functionality can be extended.It does not comes with a built in compiler or debugger and depends upon third party compilers like GCC or Microsoft C Compiler for code compilation.In this example we are using GCC from MinGW.
First thing to do is to download MinGW (GCC) Compiler from its website and install it on your system.After which download the Code::Blocks IDE from its website and install it .If you have installed the compilers ( GCC or Microsoft C Compiler) at their default location the IDE will automatically detect and configure the compilers.In this case we are using GCC ,You can also use the Microsoft C compiler but it is not covered here.
Creating a C Project
Double click on the Code::Blocks icon the desktop which would open up the welcome screen.On the welcome screen itself you can see an option to "Create a new project" ,click on that to start the new project wizard.Alternativly you can go to menu bar and select "File → New → Project " to start the wizard.
Clicking that would bring the "New from Template " window from their click on " Projects " and then select "Empty Project" inside Category.
In the next window give your project a name (here FTDI) and select the folder in which you want your files to reside.
Click next to choose the compiler you want to use .If you have properly configured your MinGW installation as detailed in the first tutorial ,select the "GNU GCC Compiler " under the compiler option and click Finish.
It is possible to select other compilers by using the drop down menu under the compiler selection.Code Blocks would display a number of supported compatible compilers .Please note that just because it is listed in the drop down menu ,doesn't mean that the compiler is installed .For example in the below image the compiler is showing Digital Mars Compiler,Open Watcom compiler ,Cygwin etc ,none of which is installed.The only one's installed are GCC and Microsoft Visual C++ 2010.
Adding Files to C Project
We are going to use the same source file(dtr.c) from the 2nd tutorial for this example
Go to " File → New → Empty File " to create a new C file .A dialog will pop up asking whether you want to add the file to the current project,Click Yes and save the file as "dtr.c" inside your project.If you check on the left ,inside the Workspace you will notice that a new folder called "Sources" has appeared and under which you can see your newly created file "dtr.c".Please note that the folder "Sources" is a virtual folder and is created to manage your project,it doesn't exist in the file system.You can check it by opening your project folder.Copy the code for dtr.c from the website and paste it inside your source file and save it .
Alternatively if have you already downloaded the file "dtr.c",copy it to your project folder FTDI and then go to " Workspace → FTDI " right click on FTDI and use " Add Files " to add dtr.c to your project.
Now copy ftd2xx.h header file and ftd2xx.lib to your project folder FTDI and then go to " Workspace → FTDI " right click on FTDI and use " Add Files.... " to add "ftd2xx.h" header file to your project.Do not add the library (ftd2xx.lib) using "add files..." options it should be linked separately.Under the Workspace you can see the ftd2xx.h header file under a seperate folder called "Headers".
If you compile or build your project at this stage you will get an error as the library (ftd2xx.lib) is not linked with your project.
Linking the Library in Code::Blocks
Go to Menu "Settings → Compiler..." ,
It will open a window titled "Compiler settings ".Now press "Add" button below "Link Libraries" under the "Linker settings " tab .
This will open a dialog box called " Add Library " , navigate towards ftd2xx.lib and press OK.Now the library along with the path will be added to the " Link Libraries " section.
Now Click on the "Search directories " tab near "Linker settings".Under "Linker settings" there will be three sub tabs "compiler", "Linker", "Resource Compiler".
Select "Linker" tab and "Add " the path to the library ftd2xx.lib.
Now press ok and to check whether your library is linked correctly ,go to the Menu " Build → Build " or press "CTRL + F9" to build your project.If your code compiles correctly and no error messages of type "Cannot locate ftd2xx.lib" is displayed .We have successfully linked the library.
Now you can start modifying the dtr.c to suit your needs.