Thread: USB1: Undefined reference to...

  1. #1
    Registered User
    Join Date
    Dec 2009

    USB1: Undefined reference to...

    I'm a student who is teaching himself C and trying to apply it to program something for his WorkStudy.

    My code is USB1.c and the USD_USB.h, USD_USB.lib, and USD_USB.dll is provided to me by the company who made a device that captures data from various encoders.

    I have obtained the USD_USB files from their official website and wrote a simple code USB1.c to test it out along with the device. However, I ran into this stubborn error and have been stuck for a while. I am currently trying to solve this problem during my winter break: XD

    here is USB1.c
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <USD_USB.h>
    int main()
        //printf("Hello world!\n");
        BYTE NumberOfUSB1s;
        BYTE ModAddress[16];
        NumberOfUSB1s = USB1Init();
            BYTE i;
            for(i = 0; i< NumberOfUSB1s; i++)
                int result;
                result = USB1ReturnModuleAddress(i, &ModAddress[i]);
                if(result == 1)
                    printf("good address");
                    printf("bad address");
        return 0;
    Here is USD_USB.h

    typedef struct {
    	BYTE low;				// Intel processors use little-endian order
    	BYTE mid_low;
    	BYTE mid_high;
    	BYTE high;
    	} LONG_BYTES;			// 4 consecutive bytes, for use in breaking up long's
    typedef union {
    	unsigned long whole;	// this allows programmer access to entire long
    	LONG_BYTES piece;		// or each byte
    	} FRACTURED_ULONG;		// fractured unsigned long
    typedef struct {
    	BYTE low;				// Intel processors use little-endian order
    	BYTE high;
    	} SHORT_BYTES;			// 2 consecutive bytes, for use in breaking up short's
    typedef union {
    	unsigned short whole;	// this allows programmer access to entire long
    	SHORT_BYTES piece;		// or each byte
    	} FRACTURED_USHORT;		// fractured unsigned short
    |    Definitions                                                         |
    // misc constant definitions
    #define MAX_ERROR_MSG_LENGTH	512
    #define MAX_INC_COUNTS			0x1000000
    #define FAILURE					FALSE		// must be zero
    #define SUCCESS					TRUE
    #define INVALID_DEVICE			-1
    #define INVALID_MODADDRESS		-1
    #define DEVICE_NAME_BASE	"\\\\.\\USDUSBB0"
    #define MAX_DEVICES			16		// max devices on USB bus
    #define MAX_ADDRESSES		256		// address can be any valid single byte value
    #define IOCTLCODE			0x222084
    #define IOCTLCODE_SEI		0x222094
    #define IOCTLCODE_INT		0x222098
    #define IOCTLCODE_BUF		0x22209C
    #define IOCTLCODE_FLUSH		0x2220A0		/* flush buffer command to driver */
    #define MAX_BUFFER_LENGTH	64
    #define	MAX_NUM_ENCODERS	4		// max number of encoders per device on USB bus
    #define MAX_CHANNEL_MASK	0x03	// mask to limit encoder number to valid bits
    #define SEEPROM_USER_AREA	64		/* user area starts at location 64 */
    #define SEEPROM_USER_SIZE	64      /* locations 64-127 are available for user definition */
    #define SEI_FRAMING_ERROR	0x01	/* if set, then there has been a framing error on SEI port */
    #define SEI_OVERRUN_ERROR	0x02	/* if set, then there has been an overrun error on the SEI port */
    #define SEI_BUSY_ASSERTED	0x04	/* if set, then busy is asserted on SEI port */
    #define SEI_BAUD_2400	0x14
    #define SEI_BAUD_4800	0x13
    #define	SEI_BAUD_9600	0x12
    #define	SEI_BAUD_19200	0x11
    #define	SEI_BAUD_38400	0x10
    #define SEI_BAUD_57600	0x01
    // USB1 command definitions
    #define READ_CNTR			0x01
    #define RESET_CNTR			0x03
    #define RESET_ALL_CNTRS		0x04
    #define SET_CNTR_POS		0x05
    #define SET_CNTR_MAX		0x06
    #define SET_CNTR_MODE		0x07
    #define SEI_SET_BAUD		0x10
    #define SEI_RESET			0x11
    #define SEI_READ_STATUS		0x12
    #define READ_INFO			0x20
    #define READ_INDEX_ENABLE	0x21
    #define SET_INDEX_ENABLE	0x22
    #define SAVE_PARAMETERS		0x23
    #define READ_INPUT_PORT		0x24
    #define SET_OUTPUT_PORT		0x25
    #define READ_MODULE_ADDRESS	0x26
    #define SET_MODULE_ADDRESS	0x27
    #define READ_OUTPUT_PORT	0x28
    #define	READ_USER_EEPROM	0x29
    #define WRITE_USER_EEPROM	0x2A
    #define	READ_LAST_STATUS	0x2B
    #define SET_OUTPUT_BITS		0x2C
    #define	CLEAR_OUTPUT_BITS	0X2D
    #define RESET_HISTORY_BUFF	0x30
    #define GET_HISTORY_BUFF_STATUS	0x31
    #define SET_SAMPLE_RATE		0x32
    #define GET_SAMPLE_RATE		0x33
    #define SET_EVENTS			0x34
    #define GET_EVENTS			0x35
    #define	START_ACQUISITION	0x36
    #define	STOP_ACQUISITION	0x37
    #define SET_TIMESTAMP		0x39
    #define READ_POSITION_QUAL	0x3a
    #define SET_POSITION_QUAL	0x3b
    #define	SET_ACQUIRE_MODE	0x3c
    |	 Exported Functions Defined:                                         |
    LONG _stdcall USB1IO(UCHAR ModAddress, ULONG ioCtl, UCHAR* BufOut, ULONG SizeOut, UCHAR* BufIn, ULONG SizeIn);
    int _stdcall USB1Init(void);
    BOOL _stdcall USB1ReturnModuleAddress(UCHAR DeviceNumber, PUCHAR pucAddress);
    BOOL _stdcall USB1Close(void);
    BOOL _stdcall USB1GetIncPosition(UCHAR ModAddress, UCHAR Encoder, LONG* ulResult);
    BOOL _stdcall USB1GetAllPosition(UCHAR ModAddress, ULONG* pulPosition, ULONG *pulTimeStamp, UCHAR *pucParallelInput);
    BOOL _stdcall USB1GetBufferedPosition(UCHAR ModAddress, UCHAR Encoder, LONG* ulResult, BOOL *pbDataAvailable);
    BOOL _stdcall USB1GetAllBuffered(UCHAR ModAddress, ULONG* ulPosition, ULONG *ulTimeStamp, UCHAR *ucParallelInput, BOOL *pbDataAvailable);
    BOOL _stdcall USB1GetIncPositionExtended(UCHAR ModAddress, UCHAR Encoder, LONG* lPos, LONG* lMaxCount, LONG* lTimeStamp, PUCHAR pucCounterMode, PUCHAR pucStatus);
    BOOL _stdcall USB1ResetIncPosition(UCHAR ModAddress, UCHAR Encoder);
    BOOL _stdcall USB1ResetAllIncPositions(UCHAR ModAddress);
    BOOL _stdcall USB1SetIncPosition(UCHAR ModAddress, UCHAR Encoder, ULONG Data);
    BOOL _stdcall USB1SetIncMaxCount(UCHAR ModAddress, UCHAR Encoder, ULONG Data);
    BOOL _stdcall USB1SetIncMode(UCHAR ModAddress, UCHAR Encoder, UCHAR Mode, UCHAR Prescale);
    BOOL _stdcall USB1SetSEIBaudRate(UCHAR ModAddress, ULONG ulBaudRate);
    BOOL _stdcall USB1ResetSEI(UCHAR ModAddress);
    BOOL _stdcall USB1GetSEIStatus(UCHAR ModAddress, UCHAR *Result);
    BOOL _stdcall USB1GetFactoryInfo(UCHAR ModAddress, USHORT *pusModel, USHORT *pusVersion, USHORT *pusConfiguration,
    								 ULONG *pulSN, UCHAR *pucMonth, UCHAR *pucDay, USHORT *pusYear);	// get info of device
    BOOL _stdcall USB1GetIndexRegister(UCHAR ModAddress, PUCHAR pucIndex);
    BOOL _stdcall USB1SetIndexRegister(UCHAR ModAddress, UCHAR ucNewIndex);
    BOOL _stdcall USB1SaveParameters(UCHAR ModAddress);
    BOOL _stdcall USB1GetInputPort(UCHAR ModAddress, PUCHAR pucResult);
    BOOL _stdcall USB1SetOutputPort(UCHAR ModAddress, UCHAR Data);
    BOOL _stdcall USB1SetModuleAddress(UCHAR ucOldModAddress, UCHAR ucNewModAddress);
    BOOL _stdcall USB1GetLastErrorAnsi(LPSTR pszResultString, ULONG ulLength);
    BOOL _stdcall USB1GetLastErrorVB(LPSTR pszDestination, size_t lngMaxCount);
    BOOL _stdcall USB1FlushHistoryBuffer(UCHAR ModAddress);
    BOOL _stdcall USB1GetHistoryBufferStatus(UCHAR ModAddress, PUCHAR pucStatus);
    BOOL _stdcall USB1SetHistoryBufferRate(UCHAR ModAddress, ULONG ulRate);
    BOOL _stdcall USB1GetOutputPort(UCHAR ModAddress, PUCHAR pucOutput);
    BOOL _stdcall USB1ReadUserEEPROM(UCHAR ModAddress, ULONG ulLocation, ULONG ulLength, PUCHAR pucData);
    BOOL _stdcall USB1WriteUserEEPROM(UCHAR ModAddress, ULONG ulLocation, ULONG ulLength, PUCHAR pucData);
    BOOL _stdcall USB1SetOutputBits(UCHAR ModAddress, UCHAR Data);
    BOOL _stdcall USB1ClearOutputBits(UCHAR ModAddress, UCHAR Data);
    BOOL _stdcall USB1GetHistoryBufferRate(UCHAR ModAddress, PULONG pulPeriod);
    BOOL _stdcall USB1SetEvents(UCHAR ModAddress, PUCHAR pucTrigger, UCHAR ucTrigAnd, PUCHAR pucQualifier, UCHAR ucQualAnd, ULONG ulNumberOfSamples);
    BOOL _stdcall USB1GetEvents(UCHAR ModAddress, PUCHAR pucTrigger, PUCHAR pucTrigAnd, PUCHAR pucQualifier, PUCHAR pucQualAnd, PULONG pulNumberOfSamples);
    BOOL _stdcall USB1StartAcquisition(UCHAR ModAddress);
    BOOL _stdcall USB1StopAcquisition(UCHAR ModAddress);
    BOOL _stdcall USB1GetAcquisitionStatus(UCHAR ModAddress, PULONG pulSamplesToCollect, PULONG pulSamplesRemaining, PUCHAR pucStatus);
    BOOL _stdcall USB1GetDllVersion(PUCHAR pucDllVerHigh, PUCHAR pucDllVerLow);
    BOOL _stdcall USB1CommandResponseSEI(UCHAR ModAddress, PUCHAR pucCommand, ULONG ulCommandLength, PUCHAR pucResponse, PULONG pulResponseLength);
    BOOL _stdcall USB1SetTimeStamp(UCHAR ModAddress, ULONG ulNewTS);
    BOOL _stdcall USB1GetPositionQual(UCHAR ModAddress, PUCHAR pucQualByte);
    BOOL _stdcall USB1SetPositionQual(UCHAR ModAddress, UCHAR ucNewQualByte);
    BOOL _stdcall USB1SetAcquireMode(UCHAR ModAddress, UCHAR ucNewMode);
    |	 Internal Functions Defined:                                         |
    #ifdef _DEBUG
    #define PRINT(x)	OutputDebugString(x)
    #define PRINT(x)
    USB_USD.lib and dll are available at their website:
    US Digital | Support Software USB1

    I receive the following errors:

    Compiling: USB1.c
    Linking console executable: bin\Debug\USB1.exe
    obj\Debug\USB1.o: In function `main':
    C:/Users/kimo3/Documents/UNC/workstudy/codeblocks projects/USB1/USB1.c:15: undefined reference to `_USB1Init@0'
    C:/Users/kimo3/Documents/UNC/workstudy/codeblocks projects/USB1/USB1.c:22: undefined reference to `_USB1ReturnModuleAddress@8'
    collect2: ld returned 1 exit status
    I prefer to work in Code::Blocks
    But I've tried compiling this in MS Visual 2008 express
    in console using MinGW's gcc.exe
    and in Eclipse with C developer addon (I learned Java from school classes)
    All of them produced the same errors.

    What am I doing wrong? (probably something noobish XP.. hopefully.... TT__TT)

    Thanks in advance ^.^

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    You probably want "" instead of <> around your USB incude. You may also need to set up the correct linking for your .dll files you're using, which is likely compiler/IDE specific.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2009
    I have tried replacing "<,>" with "","". and th eresult was the same.
    And about linking dll and lib files, I have followed the code:block tutorial on how to do so, which bascially was setting the IDE to look for lib and dll files in user set directories.

    If anyone out there is using code:blocks or any other IDE, can you plz explain to me in depth how to link the dlls and libs?


  4. #4
    Registered User
    Join Date
    Dec 2009
    Looks like database error caused roll backs, which made the last couple posts to go *poof*
    Salem has suggested that the MinGW gcc compiler is incompatible with libraries compiled by Microsoft's compiler, which is is what we were assuming the USD_USB libraries were compiled with.
    He also mentioned that there is a way to convert a microsoft compiled library to be compatible with MinGW compilers.

    I googled for a while and found the following website with instructions how to do so:
    Linking MSVC Libraries with MinGW Projects Out of Hanwell

    I got the necessary tools and attempted the above procedure:
    1. I generated the definition file of the USD_USB.lib library.
    ?USB1ClearOutputBits@@YGHEE@Z	@39
    ?USB1Close@@YGHXZ	@4
    ?USB1CommandResponseSEI@@YGHEPAEK0PAK@Z	@47
    ?USB1FlushHistoryBuffer@@YGHE@Z	@31
    ?USB1GetAcquisitionStatus@@YGHEPAK0PAE@Z	@45
    ?USB1GetAllBuffered@@YGHEPAK0PAEPAH@Z	@28
    ?USB1GetAllPosition@@YGHEPAK0PAE@Z	@27
    ?USB1GetBufferedPosition@@YGHEEPAJPAH@Z	@26
    ?USB1GetDllVersion@@YGHPAE0@Z	@46
    ?USB1GetEvents@@YGHEPAE000PAK@Z	@42
    ?USB1GetFactoryInfo@@YGHEPAG00PAKPAE20@Z	@19
    ?USB1GetHistoryBufferRate@@YGHEPAK@Z	@40
    ?USB1GetHistoryBufferStatus@@YGHEPAE@Z	@32
    ?USB1GetIncPosition@@YGHEEPAJ@Z	@9
    ?USB1GetIncPositionExtended@@YGHEEPAJ00PAE1@Z	@10
    ?USB1GetIndexRegister@@YGHEPAE@Z	@20
    ?USB1GetInputPort@@YGHEPAE@Z	@23
    ?USB1GetLastErrorAnsi@@YGHPADK@Z	@29
    ?USB1GetLastErrorVB@@YGHPADI@Z	@30
    ?USB1GetOutputPort@@YGHEPAE@Z	@35
    ?USB1GetPositionQual@@YGHEPAE@Z	@50
    ?USB1GetSEIStatus@@YGHEPAE@Z	@18
    ?USB1Init@@YGHXZ	@2
    ?USB1ResetAllIncPositions@@YGHE@Z	@12
    ?USB1ResetIncPosition@@YGHEE@Z	@11
    ?USB1ResetSEI@@YGHE@Z	@17
    ?USB1ReturnModuleAddress@@YGHEPAE@Z	@3
    ?USB1SaveParameters@@YGHE@Z	@22
    ?USB1SetAcquireMode@@YGHEE@Z	@52
    ?USB1SetEvents@@YGHEPAEE0EK@Z	@41
    ?USB1SetHistoryBufferRate@@YGHEK@Z	@33
    ?USB1SetIncMaxCount@@YGHEEK@Z	@14
    ?USB1SetIncMode@@YGHEEEE@Z	@15
    ?USB1SetIncPosition@@YGHEEK@Z	@13
    ?USB1SetIndexRegister@@YGHEE@Z	@21
    ?USB1SetModuleAddress@@YGHEE@Z	@25
    ?USB1SetOutputBits@@YGHEE@Z	@38
    ?USB1SetOutputPort@@YGHEE@Z	@24
    ?USB1SetPositionQual@@YGHEE@Z	@51
    ?USB1SetSEIBaudRate@@YGHEK@Z	@16
    ?USB1SetTimeStamp@@YGHEK@Z	@48
    ?USB1StartAcquisition@@YGHE@Z	@43
    ?USB1StopAcquisition@@YGHE@Z	@44
    ?USB1TestCall@@YGJXZ	@34
    and obtained above, however, there was no underscored stdcalls, or @ordinal's. So I did not know what to do. I assumed that the question mark infront of each function calls were abnormal. So I tried removing those.
    I regenerated the USD_USB.lib using the dll tool and the new edited definition file and tried to compile the program using MinGW's gcc compiler. But the result was the same "undefined reference to..."
    I tried the above steps with "definition file without ?? and @(insert number here)" and with unchanged definition file, but the results were the same.

    so in summary now I am lost in this program of mine again.... HELP!!! TT__TT

    Thanks in advance.

  5. #5
    Registered User
    Join Date
    Dec 2009
    So after, half giving up trying to convert libararies to be compatible with MinGW compiler...
    I have tried writing the USB1.c as C++ file and then compiling and running.

    Surprisingly when I do so it compiles and runs fine, despite the fact that the manual tells me that it should be written in visual basic, C/C++ environment.


    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <windows.h>
    #include "USD_USB.h"
    using namespace std;
    int main()
    	char text[20];
    	BYTE NumberOfUSB1s;
        BYTE ModAddress[16];
        cout<<"Hello world!\n" << endl;
        NumberOfUSB1s = USB1Init();
            BYTE i;
            for(i = 0; i< NumberOfUSB1s; i++)
                int result;
                result = USB1ReturnModuleAddress(i, &ModAddress[i]);
                if(result == 1)
                    printf("good address");
                    printf("bad address");
    		cout<<"no USB1 connected"<<endl;
    	fgets(text, sizeof text, stdin);
    	cout<< text<< endl;
        return 0;
    If this is the case, I'll have to learn C++ instead of C....
    Any idea why this is so?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well the program you've posted is a horrible mix of everything.

    In half a dozen lines, you have

    #include <conio.h>
    An archaic DOS header file

    #include <stdio.h>
    #include <stdlib.h>
    Standard C header files

    #include <iostream>
    Standard C++ header files

    #include <windows.h>
    OS header files

    #include "USD_USB.h"
    Library header files

    using namespace std;
    C++ specific statement.

    If you rip out all the C++ statements - that's easy, just replace cout with printf, then you're back to good old C.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2009
    yeah it is a horrible mixture of both, since it was orginally C only, but I added cout and compiled it in C++ for the sake of proving the point.

    It seems like that the libraries given to me by the company are only compatible with C++ and not with C since it gives undefined reference to the functions(methods) in the library when compiled as C, and compiles fine when compiled as C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

Tags for this Thread