Friday, August 6, 2010

Compiling a V-USB (AVR-USB) example program with Visual C++




Well, I used MinGW / MSYS to compile some AVR-USB programs when I was working for IICT. Setting up MinGW is in windows is extremely tedious, especially if you live in a country where 30kBps download speed is considerably high. The offline installation of MinGW is equally confusing. I was considering an alternate development environment for windows, and Visual C++ can be considered as a great option.



Some of my students today visited me with some problems compiling a V-USB commandline example. Although I managed to solve it, I had to do it with considerable amount of hacks to the v-usb libraries. They were using visual C++ version 6. Now I am going to show some steps to compile an example of V-USB from Microsoft Visual C++ 2010 Express Edition.

Both online and offline installer for Visual C++ express edition is available from the website. The express edition is free to download and use for evaluation and education: http://www.microsoft.com/express/downloads/

In case you don’t know yet, v-usb can be downloaded from obdev’s website at: http://www.obdev.at/products/vusb/download.html

To set up the VC++ environment, you’ll also need libusb-win32 drivers. Download them from: http://sourceforge.net/projects/libusb-win32/


For this blogpost, I am using these files:

vusb-20100715.tar.gz

libusb-win32-bin-1.2.1.0.zip

downloaded from the respective sites, and the latest version at the time of writing this blogpost. I’ve also downloaded and installed Microsoft Visual C++ 2010 Express edition.

I am using 32 bit version of Windows 7, with intel processor. If you use 64 bit version of windows or AMD processor, you have to select apropriate libraries.

Open the libusb-win32-bin-1.2.1.0.zip with 7-zip and extract contents of folder libusb-win32-bin-1.2.1.0.zip\libusb-win32-bin-1.2.1.0\lib\msvc\ to C:\Program Files\Microsoft Visual Studio 10.0\VC\lib , (note, the library folder to be copied for 64 bit version is msvc_x64). Also extract the content of libusb-win32-bin-1.2.1.0\include\ to C:\Program Files\Microsoft Visual Studio 10.0\VC\include

Read this file: libusb-win32-bin-1.2.1.0\bin\libusb-win32-bin-README.txt to know how to install the bin folder content.

Open Microsoft Visual Studio Express edition, and create a new Win32 Console Application Project using the wizard. Use all default settings of wizard.



Now, using windows explorer, open the folder where the project is created using the wizard. My project title is ledcomm. By default, it will be in \Documents\Visual Studio 2010\Projects\ledcomm\ledcomm

Open the vusb-20100715.tar.gz and browse to this folder:





Copy the contents of commandline folder into the folder where you created the project (X:\Documents\Visual Studio 2010\Projects\ledcomm\ledcomm). The folder should now look like:




Copy the folder “firmware” vusb-20100715.tar\vusb-20100715\examples\custom-class\ to X:\Documents\Visual Studio 2010\Projects\ledcomm\ (note: it is one level up the folder where we copied command line). You can use this folder to create a AVR studio project later.



In the project explorer, remove all source and header files from the project:




Now add the following source files and header files to the project



ledcomm\set-led.c

ledcomm\opendevice.h

ledcomm\opendevice.c

firmware\requests.h

firmware\usbconfig.h


The project explorer window now looks like this:

image

Go to project properties and check if the Pre Compiled header is disabled:


image


At this point, if you hit F5 (Run), you’ll get this debug result:

1>------ Build started: Project: ledcomm, Configuration: Debug Win32 ------
1> set-led.c
1>X:\documents\visual studio 2010\projects\ledcomm\ledcomm\set-led.c(84): warning C4013: 'strcasecmp' undefined; assuming extern returning int
1> opendevice.c
1> Generating Code...
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_control_msg referenced in function _usbGetStringAscii
1>set-led.obj : error LNK2001: unresolved external symbol _usb_control_msg
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_get_string_simple referenced in function _usbGetStringAscii
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_close referenced in function _usbOpenDevice
1>set-led.obj : error LNK2001: unresolved external symbol _usb_close
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_strerror referenced in function _usbOpenDevice
1>set-led.obj : error LNK2001: unresolved external symbol _usb_strerror
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_open referenced in function _usbOpenDevice
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_get_busses referenced in function _usbOpenDevice
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_find_devices referenced in function _usbOpenDevice
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_find_busses referenced in function _usbOpenDevice
1>set-led.obj : error LNK2019: unresolved external symbol _strcasecmp referenced in function _main
1>set-led.obj : error LNK2019: unresolved external symbol _usb_init referenced in function _main
1>X:\documents\visual studio 2010\Projects\ledcomm\Debug\ledcomm.exe : fatal error LNK1120: 10 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


1>------ Build started: Project: ledcomm, Configuration: Debug Win32 ------
1> set-led.c
1>X:\documents\visual studio 2010\projects\ledcomm\ledcomm\set-led.c(84): warning C4013: 'strcasecmp' undefined; assuming extern returning int
1> opendevice.c
1> Generating Code...
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_control_msg referenced in function _usbGetStringAscii
1>set-led.obj : error LNK2001: unresolved external symbol _usb_control_msg
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_get_string_simple referenced in function _usbGetStringAscii
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_close referenced in function _usbOpenDevice
1>set-led.obj : error LNK2001: unresolved external symbol _usb_close
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_strerror referenced in function _usbOpenDevice
1>set-led.obj : error LNK2001: unresolved external symbol _usb_strerror
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_open referenced in function _usbOpenDevice
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_get_busses referenced in function _usbOpenDevice
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_find_devices referenced in function _usbOpenDevice
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_find_busses referenced in function _usbOpenDevice
1>set-led.obj : error LNK2019: unresolved external symbol _strcasecmp referenced in function _main
1>set-led.obj : error LNK2019: unresolved external symbol _usb_init referenced in function _main
1>X:\documents\visual studio 2010\Projects\ledcomm\Debug\ledcomm.exe : fatal error LNK1120: 10 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I just pasted this error message so that people who have a hard time finding a solution quickly hits this page.

Firstly, VC++ doesn’t have strcasecmp, so replace the strcasecmp command with strcmp command. There goes the case insensitivity!


Go to project properties and check if the Pre Compiled header is disabled:





At this point, if you hit F5 (Run), you’ll get this debug result:
1>------ Build started: Project: ledcomm, Configuration: Debug Win32 ------
1> set-led.c
1>X:\documents\visual studio 2010\projects\ledcomm\ledcomm\set-led.c(84): warning C4013: 'strcasecmp' undefined; assuming extern returning int
1> opendevice.c
1> Generating Code...
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_control_msg referenced in function _usbGetStringAscii
1>set-led.obj : error LNK2001: unresolved external symbol _usb_control_msg
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_get_string_simple referenced in function _usbGetStringAscii
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_close referenced in function _usbOpenDevice
1>set-led.obj : error LNK2001: unresolved external symbol _usb_close
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_strerror referenced in function _usbOpenDevice
1>set-led.obj : error LNK2001: unresolved external symbol _usb_strerror
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_open referenced in function _usbOpenDevice
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_get_busses referenced in function _usbOpenDevice
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_find_devices referenced in function _usbOpenDevice
1>opendevice.obj : error LNK2019: unresolved external symbol _usb_find_busses referenced in function _usbOpenDevice
1>set-led.obj : error LNK2019: unresolved external symbol _strcasecmp referenced in function _main
1>set-led.obj : error LNK2019: unresolved external symbol _usb_init referenced in function _main
1>X:\documents\visual studio 2010\Projects\ledcomm\Debug\ledcomm.exe : fatal error LNK1120: 10 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I just pasted this error message so that people who have a hard time finding a solution quickly hits this page.

Firstly, VC++ doesn’t have strcasecmp, so replace the strcasecmp command with strcmp command. There goes the case insensitivity!

Now project>project properties, Configuration Options>Linker>Input>Additional Dependencies



Now Press F7 to build the project.


1>------ Build started: Project: ledcomm, Configuration: Debug Win32 ------
1> set-led.c
1> ledcomm.vcxproj -> x:\documents\visual studio 2010\Projects\ledcomm\Debug\ledcomm.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========


Now you can run the created exe file:




In this way, you can use Visual C++ 2010 Express Edition to compile the


5 comments:

  1. Hi Sajid,
    Good help to get me (a first timer for VC++) running till the last window. However, I encounter this problem:
    'ledcomm.exe': Loaded 'C:\WINDOWS\system32\libusb0.dll', Cannot find or open the PDB file


    Tried a lot of different things but did not work. The build is succesful.
    How to solve this ?

    Thanks,
    Abhishek

    ReplyDelete
  2. I want to learn in micro controller of c programming I f you any help me .. i want to payment for learning its. i am at now mohammadpur dhaka.-- my cell no.- 01714009480

    ReplyDelete
  3. i did everything but i still get errors... linking errors...i renamed the strcasecmp with strcmp, i also added libusb.lib to the dependencies... but the errors remain the same...could you please help me?

    ReplyDelete
  4. thank you very much, I have made my program ( from http://codeandlife.com )using eclipse from three days but the problem is that I didn't know where I should write “usbtest on” and “usbtest off”, and from the images you uploaded I knew that the location is cmd

    ReplyDelete