Global Variables

This is a discussion on Global Variables within the C Programming forums, part of the General Programming Boards category; Hey guys, I get the following error when I try to compile my code: [Linker error] undefined reference to 'outgoing_response' ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    37

    Global Variables

    Hey guys,

    I get the following error when I try to compile my code:

    [Linker error] undefined reference to 'outgoing_response'

    I get this error when compiling file example-635.c
    It contains the code

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include "typedefs.h"
    #include "serial.h"
    #include "cf_packet.h"
    #include "show_packet.h"
    #include "TTYINFO.H"
    //#include "MTTTY.H"
    
    #define MAX_WRITE_BUFFER        1024
    #define MAX_READ_BUFFER         2048
    char gszPort[10];
    
    //============================================================================
    int main(int argc, char* argv[])
      {
      printf("Ultra-simple CFA-635 command-line communications example.\n");
      printf("Crystalfontz America, Inc. http://www.crystalfontz.com\n\n");
      printf("Usage:\n");
      printf("%s PORT BAUD\n");
      printf("PORT is COM1\n");
      printf("BAUD is 115200\n");
    
      /*//If only 0 or 1 parameter is entered, prompt for the missing parameter(s)
      if(argc < 3)
        {
          printf("\nMISSING A PARAMETER. Enter both PORT and BAUD.\n\n");
          return(0);
        }*/
    
      //Check for optional "clear" parameter and set flag if found
      /*int
        cleardisplay=0;
      if((argc > 3)&&(!strcmp(argv[3],"clear"))) cleardisplay=1;*/
    
    
    /*  int
        baud;
      //default the baud to 115200
      if(strcmp(argv[2],"19200"))
        baud=115200;
      else
        baud=19200;*/
    
      if(SetupCommPort())
        {
        printf("Could not open port.");
        return(1);
        }
      else
        printf("Comm Port Opened");
    
      //For some reason, Linux seems to buffer up data from the LCD, and they are sometimes
      //dumped at the start of the program. Clear the serial buffer.
      //while(BytesAvail())
      //  GetByte();
    
    //Outgoing command packets. Either clear the screen
    //or send our line information
    //**********************************************
    /*  if(cleardisplay)
      {
        outgoing_response.command = 6;
        outgoing_response.data_length = 0;
        send_packet();
      }
      else
      {*/
        //Send line 1 to the 635 using command 31
        outgoing_response.command = 31;
        outgoing_response.data[0]=0; //col
        outgoing_response.data[1]=0; //row
        memcpy(&outgoing_response.data[2],">>>This is line 1<<<",20);
        outgoing_response.data_length = 22;  //the col & row position + the 20 char data length
        send_packet();
    
    		//CFA-635 communications protocol only allows
    		//one outstanding packet at a time. Wait for the response
    		//packet from the CFA-635 before sending another
    		//packet.
    		int
    		k;
    		int
    		timed_out;
    		timed_out = 1; //default timed_out is true
    		for(k=0;k<=10000;k++)
    		if(check_for_packet())
    			{
    			ShowReceivedPacket();
    			timed_out = 0; //set timed_out to false
    			break;
    			}
    		if(timed_out)
    		printf("Timed out waiting for a response.\n");
    
        //Send line 2 to the 635 using command 31
        outgoing_response.command = 31;
        outgoing_response.data[0]=0; //col
        outgoing_response.data[1]=1; //row
        memcpy(&outgoing_response.data[2],">>>This is line 2<<<",20);
        outgoing_response.data_length = 22;
        send_packet();
    
    		timed_out = 1; //default timed_out is true
    		for(k=0;k<=10000;k++)
    			if(check_for_packet())
    				{
    				ShowReceivedPacket();
    				timed_out = 0; //set timed_out to false
    				break;
    				}
    		if(timed_out)
    			printf("Timed out waiting for a response.\n");
    
        //Send line 3 to the 635 using command 31
        outgoing_response.command = 31;
        outgoing_response.data[0]=0; //col
        outgoing_response.data[1]=2; //row
        memcpy(&outgoing_response.data[2],">>>This is line 3<<<",20);
        outgoing_response.data_length = 22;
        send_packet();
    
    		timed_out = 1; //default timed_out is true
    		for(k=0;k<=10000;k++)
    			if(check_for_packet())
    				{
    				ShowReceivedPacket();
    				timed_out = 0; //set timed_out to false
    				break;
    				}
    		if(timed_out)
    			printf("Timed out waiting for a response.\n");
    
        //Send line 4 to the 635 using command 31
        outgoing_response.command = 31;
        outgoing_response.data[0]=0; //col
        outgoing_response.data[1]=3; //row
        memcpy(&outgoing_response.data[2],">>>This is line 4<<<",20);
        outgoing_response.data_length = 22;
        send_packet();
    
    		timed_out = 1; //default timed_out is true
    		for(k=0;k<=10000;k++)
    			if(check_for_packet())
    				{
    				ShowReceivedPacket();
    				timed_out = 0; //set timed_out to false
    				break;
    				}
    		if(timed_out)
    			printf("Timed out waiting for a response.\n");
      //}
    //**********************************************
    
      printf("\nCtrl-C to exit.\n");
      printf("Updated display, now waiting for packets\n\n");
    
    /*  while(!cleardisplay)
       {
         usleep(100000);  // 1/10 second
         if(check_for_packet())
           ShowReceivedPacket();
       }*/
    
    /*  if(cleardisplay) printf("Display Cleared.\n");
      printf("Done.\n\n");*/
      BreakDownCommPort();
      //Uninit_Serial();
      return 0;
      }
    //============================================================================
    HANDLE SetupCommPort()
    {       
        //
        // get tty settings from settings dialog
        //
        UpdateTTYInfo();
    
        //
        // open communication port handle
        //
        COMDEV( TTYInfo ) = CreateFile( gszPort,  
                                          GENERIC_READ | GENERIC_WRITE, 
                                          0, 
                                          0, 
                                          OPEN_EXISTING,
                                          FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
                                          0);
    
        if (COMDEV(TTYInfo) == INVALID_HANDLE_VALUE) {   
            ErrorReporter("CreateFile");
            return NULL;
        }
    
        //
        // Save original comm timeouts and set new ones
        //
        if (!GetCommTimeouts( COMDEV(TTYInfo), &(TIMEOUTSORIG(TTYInfo))))
            ErrorReporter("GetCommTimeouts");
    
        //
        // Set port state
        //
        UpdateConnection();
    
        //
        // set comm buffer sizes
        //
        SetupComm(COMDEV(TTYInfo), MAX_READ_BUFFER, MAX_WRITE_BUFFER);
    
        //
        // raise DTR
        //
        if (!EscapeCommFunction(COMDEV(TTYInfo), SETDTR))
            ErrorReporter("EscapeCommFunction (SETDTR)");
    
        //
        // start threads and set initial thread state to not done
        //
        StartThreads();
    
        //
        // set overall connect flag
        //
        CONNECTED( TTYInfo ) = TRUE ;
    
        return COMDEV(TTYInfo);
    }
    
    //==============================================================================
    the 'outgoing_response' variable is defined in the following file 'cf_packet.h' and as you can see cf_packet.h is included in 'example-635.c'

    Code:
    //============================================================================
    // 635 Linux Demonstration Code.
    // 635_packet,h: Ultra-simple 635 command-line communications example
    // Copyright 2005, Crystalfontz America, Inc. Written by Brent A. Crosby
    // www.crystalfontz.com, brent@crystalfontz.com
    //============================================================================
    #define MAX_DATA_LENGTH 22
    //============================================================================
    #define MAX_COMMAND 35
    //============================================================================
    typedef struct
      {
      ubyte
        command;
      ubyte
        data_length;
      ubyte
        data[MAX_DATA_LENGTH];
      WORD_UNION
        CRC;
      }COMMAND_PACKET;
    //============================================================================
    word get_crc(ubyte *bufptr,word len,word seed);
    //============================================================================
    extern COMMAND_PACKET
      incoming_command;
    extern COMMAND_PACKET
      outgoing_response;
    //============================================================================
    ubyte check_for_packet(void);
    void send_packet(void);
    //============================================================================
    Can anyone tell me how to declare the variable 'outgoing_response' in cf_packet.h' so that it can be recognised by any other file especially 'example-635.c'?

    Any help is greatly appreciated

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Within one (and only one) of your source files within your project, and after #include'ing "cf_packet.h", add the line
    Code:
    COMMAND_PACKET   outgoing_response;
    at file scope (i.e. outside a function).

    Should you ever use the variable incoming_command in code, you'll also have to do the same thing with that variable.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    37
    Hi grumpy,

    thanks for your reply

    Is this what you mean:

    Add COMMAND_PACKET outgoing response to exapmle-635.c like so?

    example-635.c
    Code:
    // Copyright 2005, Crystalfontz America, Inc. Written by Brent A. Crosby
    // www.crystalfontz.com, brent@crystalfontz.com
    //============================================================================
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include "typedefs.h"
    #include "serial.h"
    #include "cf_packet.h"
    #include "show_packet.h"
    #include "TTYINFO.H"
    //#include "MTTTY.H"
    
    #define MAX_WRITE_BUFFER        1024
    #define MAX_READ_BUFFER         2048
    
    COMMAND_PACKET outgoing_response;
    
    char gszPort[10];
    
    //============================================================================
    int main(int argc, char* argv[])
      {
      printf("Ultra-simple CFA-635 command-line communications example.\n");
      printf("Crystalfontz America, Inc. http://www.crystalfontz.com\n\n");
      printf("Usage:\n");
      printf("%s PORT BAUD\n");
      printf("PORT is COM1\n");
      printf("BAUD is 115200\n");
    And leave cf_packet.h unchanged?

    If this is the case is cf_packet.h a useless file?

    Forgive my noviceness, but I don't fully understand why this is the case?

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    37
    Sorry,

    I also have a

    [Linker error] undefined reference to 'send packet'

    and tried your solution to 'outgoing_response' as follows

    I added
    void send_packet(void);

    to example-635.c as a global function outside the main() function just below COMMAND_PACKET outgoing_response;

    Can you tell me how to fix this error?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    You've declared send_packet() .... which tells the compiler you promise to implement it somewhere. You then call it. However, you have not implemented it. The linker recognises you have not honoured your promise ..... therefore the error message. Somewhere within your example-635.c you need something that looks like;
    Code:
    void send_packet(void)
    {
         /*   presumably some code here to send a packet */
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. scope of global variables
    By laertius in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 01:59 AM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 03:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 09:28 AM
  5. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 04:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21