Editing Gertboard Test Progs

5 posts
by SquarePeg » Sun Oct 07, 2012 7:49 pm
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:

pi@raspberrypi ~/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.
Posts: 7
Joined: Thu Jun 14, 2012 7:24 am
by SN » Sun Oct 07, 2012 7:59 pm
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
Steve N – binatone mk4->intellivision->zx81->spectrum->cbm64->cpc6128->520stfm->pc->raspi ?
User avatar
Posts: 1008
Joined: Mon Feb 13, 2012 8:06 pm
Location: Romiley, UK
by SquarePeg » Sun Oct 07, 2012 9:42 pm
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..
Posts: 7
Joined: Thu Jun 14, 2012 7:24 am
by Ottnik » Sun Nov 25, 2012 11:49 pm
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 ?
Posts: 2
Joined: Thu Jul 19, 2012 3:15 pm
by Gert van Loo » Mon Nov 26, 2012 3:24 pm
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.
User avatar
Posts: 1838
Joined: Tue Aug 02, 2011 7:27 am