Thread: undefined reference to system() function

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    6

    undefined reference to system() function

    Alright, I have been searching all over the place for a solution for this and have been stuck for some hours now. I am working with a very specific piece of hardware but that shouldn't have anything to do with the actual compilation of my program.

    Code:
    /** Includes */
    #include <avr/io.h>
    #include <util/delay.h>
    #include "adc.h"
    #include <stdlib.h>
    #include <stdio.h>
    /** Constants */
    #define F_CPU 1000000U
    
    /** Global Variables */
    
    /** Functions */
    
    /** The initialize() function initializes all of the Data Direction Registers for the Wunderboard. Before making changes to DDRx registers, ensure that you have read the peripherals section of the Wunderboard user guide.*/
    void initialize (void) {
    	/** Port A is the switches and buttons. They should always be inputs. ( 0 = Input and 1 = Output )*/
    	DDRA=0b00000000;
    
    	/** Port B has the LED Array color control, SD card, and audio-out on it. Leave DDRB alone. ( 0 = Input and 1 = Output )*/
    	DDRB=0b11000000;
    
    	/** Port C is for the 'row' of the LED array. They should always be outputs. ( 0 = Input and 1 = Output )*/
    	DDRC=0b11111111;
    
    	/** Port D has the Serial on it. Leave DDRB alone. ( 0 = Input and 1 = Output )*/
    	DDRD=0b00000000;
    
    	/** Port E has the LED Array Column control out on it. Leave DDRE alone. ( 0 = Input and 1 = Output )*/
    	DDRE=0b00000111;
    
    	/** Port F has the accelerometer and audio-in on it. Leave DDRF alone. ( 0 = Input and 1 = Output )*/
    	DDRF=0b00000000;
    }
    
    void clearArray(void)
    {
    	PORTB &= ~((1 << PB6) | (1 << PB7));	// Disable latches
    	PORTC = 0x00;
    	PORTB |= (1 << PB6) | (1 << PB7);		// Enable latches
    	PORTB &= ~((1 << PB6) | (1 << PB7));	// Disable latches
    }
    
    /** Main Function */
    
    int main (void) {
    	/** Local Varibles */
    	int a= ((0b11000111-0b10110111)/0b00001000);
    	unsigned char q=1<<((read_adc(6)-0b10110111)/a);    //Y Axis
    	unsigned char w=((read_adc(5)-0b10110111)/a);       //X Axis
    	initialize();
    	clearArray();
    	
    	while(1){
    		
    		q=1<<((read_adc(6)-0b10110111)/a);
    		w=((read_adc(5)-0b10110111)/a);
    		
    		//system("xdotool getmouselocation > /home/gordy/Documents/mouseLoc");
    		
    		if(q <= (((0b11000111-0b10110111)/2)+ 0b10111000) && q >= (((0b11000111-0b10110111)/2)- 0b10110110))
    		{
                        //don't move mouse
    		}
    		else if(q > (((0b11000111-0b10110111)/2)+ 0b10111000))
    		{
    		    //mouse will move +Y   
    		}
    		else if(q < (((0b11000111-0b10110111)/2)- 0b10110110))
    		{
    		    //mouse will move -Y
    		}
    		if(w < (((0b11000111-0b10110111)/2)+ 0b10111000) && w > (((0b11000111-0b10110111)/2)- 0b10110110))
            {
                //mouse does not move in the X direction
            }
            if(w > (((0b11000111-0b10110111)/2)+ 0b10111000))
            {
                //mouse will move +X
            }
            if(w < (((0b11000111-0b10110111)/2)- 0b10110110))
            {
                //mouse will move -X
            }
    	}
    
    }//main
    My problem is that when I try to compile, it says
    main.c:95: undefined reference to `system'
    make: *** [main.elf] Error 1
    I am thinking it might have something to do with the makefile (I did not create the makefile)
    But I know that all you should need to use the system command is stdlib.h and I tested it in a separate file and the system function works, just not for this specific problem. Sorry for the specificity of the code, I really appreciate any help, thanks in advance.

  2. #2
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Are you linking libc (the C standard library)? Your compiler should link libc automatically, unless your Makefile specifies otherwise.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    6
    I don't think I am linking the C standard library, I just thought it did it automatically and my makefile might specify otherwise but I'm not sure. How would I link the library? And also, how would I check if the makefile is specifying otherwise?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by gordyvision View Post
    I don't think I am linking the C standard library, I just thought it did it automatically and my makefile might specify otherwise but I'm not sure. How would I link the library? And also, how would I check if the makefile is specifying otherwise?
    Look at your makefile and see what options and libraries you're using. (If you see, for instance, -nodefaultlibs, then that might be a hint.)

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    6
    I noticed the makefile had a -c option but I'm currently not at my computer, I will check more thoroughly in about forty five minutes

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    -c means "compile only do not link", so presumably you have another rule for turning all the object files into an executable. Also: are you intending this to be run on your machine or a microcontroller? If the latter, I don't think you'll be wanting to do system anyway?

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    6
    Well that would be the problem then. It will be run on the microcontroller, however it has a serial port that goes to the computer and I was hoping on using that to send commands in order to manipulate the mouse.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, okay, but that's not what system does -- system sends a call to the host operating system of the thing that is running the program. I don't know of a way to send commands via RS232, though.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    6
    Hmm, I guess if there was some way to send any information to a file on the computer, I could use thatinformation to use the system function on my computer

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Is the code you posted being run on some kind of embedded system? If so please tell us a little bit about this system. What processor, any operating system, what compiler you are using?

    Jim

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I am most assuredly Not A Hardware Guy, but: what you are saying is ludicrous. You cannot run the system function on a different machine than the one that is running the program. If you mean "I will write a completely different second program that will run on my computer and listen on the RS232 port (assuming it exists on my machine and I can talk to it directly) and then process those signals", then you are a lot closer.

    I would expect those two parenthetical if statements (IF you have an RS232 port on your machine and IF you can talk to it directly) are going to be two very large ifs, unless you are running a machine that has not had an upgrade, hardware or software, since about 1998 at the latest.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by gordyvision View Post
    Hmm, I guess if there was some way to send any information to a file on the computer, I could use thatinformation to use the system function on my computer
    You'd be smarter to look into writing a server/client pair that use the RS-232 port properly...
    The server on your host machine would listen to the computer's port and handle commands issued on the uC's port...

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > ensure that you have read the peripherals section of the Wunderboard user guide.
    See this comment in your code?

    Read that to find out how to send bytes down the serial port to your host computer.
    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.

  14. #14
    Registered User
    Join Date
    May 2011
    Posts
    6
    Thank you everyone for your help. And yes, I was planning on writing a completely different program to read the data sent by the Wunderboard. I appreciate all the input, I think I know how to finish the project.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function gives undefined reference bug
    By blue sam3 in forum C++ Programming
    Replies: 1
    Last Post: 12-19-2010, 07:56 AM
  2. undefined reference to a function
    By ChoCo in forum C Programming
    Replies: 1
    Last Post: 10-04-2009, 12:08 AM
  3. undefined reference to function names
    By hellogamesmaste in forum C Programming
    Replies: 5
    Last Post: 08-17-2009, 03:05 AM
  4. Undefined Reference to function?
    By fireonyx in forum C Programming
    Replies: 6
    Last Post: 03-17-2006, 06:15 PM
  5. undefined reference to a function
    By rodrigouy in forum C Programming
    Replies: 7
    Last Post: 01-17-2006, 06:47 AM