Page 1 of 1

Editing Gertboard Test Progs

Posted: Sun Oct 07, 2012 7:49 pm
by SquarePeg
I've built one of Gert's fine Getboards and have run all the progs & tests successfully :D Now I'm trying to modify the motor test program to learn a bit more. Simple things to start with, like changing the rate at which the motor increases & decreases speed etc. I'm having trouble compiling...

I have upgraded to the latest version of Wheezy. (My gut feeling is that my problems may well be associated).

I made a copy of motor.c and named it motor_01.c
I made simple mods to the file motor_01.c and then to compile and link, ran:

gcc -o motor_01 motor_01.c and got errors.

I undid the mods to motor_01.c and ran gcc again and still got the errors even though it was now a direct copy of motor.c. Errors as below:

[email protected] ~/gertb/gertboard_sw $ gcc -o motor_01 motor_01.c
/tmp/ccd54CfW.o: In function `setup_gpio':
motor_01.c:(.text+0xa4): undefined reference to `gpio'
/tmp/ccd54CfW.o: In function `main':
motor_01.c:(.text+0xf8): undefined reference to `setup_io'
motor_01.c:(.text+0x118): undefined reference to `setup_pwm'
motor_01.c:(.text+0x140): undefined reference to `force_pwm0'
motor_01.c:(.text+0x154): undefined reference to `long_wait'
motor_01.c:(.text+0x15c): undefined reference to `set_pwm0'
motor_01.c:(.text+0x194): undefined reference to `long_wait'
motor_01.c:(.text+0x1a8): undefined reference to `long_wait'
motor_01.c:(.text+0x1b0): undefined reference to `set_pwm0'
motor_01.c:(.text+0x1e4): undefined reference to `pwm_off'
motor_01.c:(.text+0x204): undefined reference to `force_pwm0'
motor_01.c:(.text+0x224): undefined reference to `long_wait'
motor_01.c:(.text+0x22c): undefined reference to `set_pwm0'
motor_01.c:(.text+0x264): undefined reference to `long_wait'
motor_01.c:(.text+0x278): undefined reference to `long_wait'
motor_01.c:(.text+0x280): undefined reference to `set_pwm0'
motor_01.c:(.text+0x2c8): undefined reference to `pwm_off'
motor_01.c:(.text+0x2d4): undefined reference to `restore_io'
motor_01.c:(.text+0x300): undefined reference to `gpio'
collect2: ld returned 1 exit status

I'm sure I am doing something very stupid - but to date I have not been able to fathom what I'm doing wrong.

Any pointers would be gratefully received.

Re: Editing Gertboard Test Progs

Posted: Sun Oct 07, 2012 7:59 pm
by SN
you need to link in some missing libraries - I assume there is a makefile for the supplied programs which will show you what you're missing

Re: Editing Gertboard Test Progs

Posted: Sun Oct 07, 2012 9:42 pm
by SquarePeg
SN wrote:you need to link in some missing libraries - I assume there is a makefile for the supplied programs which will show you what you're missing
Got it - thanks for pointing me in the right direction! Now I'm off to learn about makefiles, linking etc. For the time being I have stuck with just editing the original file and running make. I'll go off and study all things ggc later..

Re: Editing Gertboard Test Progs

Posted: Sun Nov 25, 2012 11:49 pm
by Ottnik
I got the same problem :D

edited the ocole file for driving a stepper motor ( just for fun ;) ) an named it stepper.c

by trying to compile the file i got similar errors
by looking at the makefile i should include gb_common.h ( already is !) and gb_spi.h

but that doesnt fix it , it seems the compiler doesnt know these libraries

im stuck here :?

any hints ?

Re: Editing Gertboard Test Progs

Posted: Mon Nov 26, 2012 3:24 pm
by Gert van Loo
If you look very careful you will see that those errors come from the linker.
The linker program is called 'ld' and the line which tells you so is this one:
"collect2: ld returned 1 exit status"

What you are doing is using routines in one file which are defined in a different file.
I also from the error message can see which file:
"motor_01.c:(.text+0xf8): undefined reference to `setup_io'"
So you have a file called "motor_01.c" which uses a function named "setup_io".
But when the final result was linked together, the linker could not find the actual routine 'setup_io'.


You did NOT get an error or warning message when you compiled motor_01.c from that I conclude
that you told 'motor_01.c' how the function "setup_io" looks like.
You did that probbaly by using an include file.
Now this is the crunch of the matter:
A definition like
void setup_io() ;
only tells the compiler "Somewhere there is a function which is called "setup_io" which has type void and does not require any arguments.
Compare it to 'here is a photo of the guy you are looking for to do the job. He should look like that'
You will still need that function itself. (Compare again: you still need the guy itself to do the job)
So you should add the actual function to you code.
There are two ways of doing that:
Easiest: Copy and paste the function code into your motor_01.c file.

Complex: Compile and link the file which has the function in it.
This is an example from the Makefile how to compile and link a file:
Step 1 : compile the file into an intermediate file (called an object file):

gertboard_leds.o : gertboard_leds.c gertboard_demo.h
gcc -c gertboard_leds.c

The input file is the gertboard_leds.c
The intermediate result is the gertboard_leds.o

Later you want to combine all the intermediate files together which is done here:
gertboard_demo : gertboard_demo.o gertboard_leds.o gertboard_pwm.o gertboard_spi.o gertboard_buttons.o gertboard_motor.o
gcc -o gertboard_demo gertboard_demo.o gertboard_leds.o gertboard_pwm.o gertboard_spi.o gertboard_buttons.o gertboard_motor.o

Maybe somebody can write a little article for the Magpie how to use C-compiler Make etc.