Baking Pi and extending in C/C++


23 posts
by edy_3dz » Thu Dec 27, 2012 11:29 am
Hello everyone and Happy Holidays!

I am following Alex Chadwick's course from the University of Cambridge website. I'm only at lesson 5, but I want to extend functionality in C, and maybe in C++ in the future. My problem is that I didn't manage to mix C and assembly files.
So, this is what I have done so far:
I created a file named test.c, in which I try to call the asm procedure GetGpioAddress. I also created a file test.h where I declare the function as extern void*, but I get an "undeclared reference" error at compile time. I modified the Makefile as Alex Chadwick himself told me to do by email, but without any luck.
I tried to compile only the C file, and than link all the objects file, but I was still getting various errors. But what I want to achieve is to compile and link all the source files, assembly and C/C++ files altogether.
I'll attach the source files of what I have done so far, maybe someone can take a closer look and tell me what I am doing wrong.

LE: I forgot to mention: I use Yagarto Tools on Windows.

Thanks in advance!
template.zip
My "project"
(7.57 KiB) Downloaded 82 times
Posts: 15
Joined: Mon Jul 09, 2012 3:33 pm
Location: Brasov, Romania
by phil95 » Thu Dec 27, 2012 2:44 pm
C can use decorated name of routines.
This decoration indicate parameters, return type, ...
Try to put
__attribute__((naked)) void myGetGpioAddress()
Philippe
Posts: 105
Joined: Wed Sep 12, 2012 8:10 am
Location: Paris
by edy_3dz » Thu Dec 27, 2012 2:51 pm
I didn't even dare to use myGetGpioAddress function in assembly, my current problem is the other way around, using the GetGpioAddress procedure from assembly in C :P
Posts: 15
Joined: Mon Jul 09, 2012 3:33 pm
Location: Brasov, Romania
by phil95 » Thu Dec 27, 2012 3:11 pm
Ok then declare it as
extern "C"
{
extern void *GetGpioAddress(void);
}
Philippe
Posts: 105
Joined: Wed Sep 12, 2012 8:10 am
Location: Paris
by edy_3dz » Thu Dec 27, 2012 3:16 pm
arm-none-eabi-gcc source/test.c -o build/test.o
In file included from source/test.c:1:0:
source/test.h:4:8: error: expected identifier or '(' before string constant
Posts: 15
Joined: Mon Jul 09, 2012 3:33 pm
Location: Brasov, Romania
by phil95 » Thu Dec 27, 2012 3:26 pm
Sorry,
extern "C"
{
void *GetGpioAddress(void);
}
or
extern "C" void *GetGpioAddress(void);
Philippe
Posts: 105
Joined: Wed Sep 12, 2012 8:10 am
Location: Paris
by edy_3dz » Thu Dec 27, 2012 3:35 pm
Exactly the same error. Line 4 column 8 is right before "C", so it seems that it must be some incorrect syntax...
Posts: 15
Joined: Mon Jul 09, 2012 3:33 pm
Location: Brasov, Romania
by phil95 » Thu Dec 27, 2012 4:07 pm
What is the utility of the line with EXPORT_SYMBOL ?
Can tou try to remove it ?
Philippe
Posts: 105
Joined: Wed Sep 12, 2012 8:10 am
Location: Paris
by edy_3dz » Thu Dec 27, 2012 4:28 pm
That was something that I have seen somewhere else for exporting a function. It seems that it's wrong, I removed it, but the error regarding extern "C" remains.
Posts: 15
Joined: Mon Jul 09, 2012 3:33 pm
Location: Brasov, Romania
by phil95 » Thu Dec 27, 2012 4:33 pm
can yo remove the external "C"
remove the line EXPORT_SYMBOL
I think, your only error was the EXPORT_SYMBOL:
for the compiler, it is a call to a function, and that function is not defined
Philippe
Posts: 105
Joined: Wed Sep 12, 2012 8:10 am
Location: Paris
by edy_3dz » Thu Dec 27, 2012 4:39 pm
Removed "C", external "C", the result is always the same:
C:\Users\Edy\AppData\Local\Temp\ccmjQrVp.o: In function `myGetGpioAddress':
test.c:(.text+0x0): undefined reference to `GetGpioAddress'

Please try the suggestions you want to make before posting on the forum, my topic got flooded by assumptions ....
Posts: 15
Joined: Mon Jul 09, 2012 3:33 pm
Location: Brasov, Romania
by phil95 » Thu Dec 27, 2012 4:48 pm
Sorry, but it is not possible, because I don't use the same options as you:
For example, in my configuration, external "C" is working.
Good luck
Philippe
Posts: 105
Joined: Wed Sep 12, 2012 8:10 am
Location: Paris
by edy_3dz » Thu Dec 27, 2012 4:53 pm
What configuration do you use?
Thanks for your time, I appreciate your help, I just don't want to have a long topic, so others can browse through it easily.
Posts: 15
Joined: Mon Jul 09, 2012 3:33 pm
Location: Brasov, Romania
by phil95 » Thu Dec 27, 2012 5:02 pm
I don't use yagarto
I use my own libraries
I don't use make and makefile
I use C++ but only on C mode, but my externals are decorated
Some differences !!!
Normally your test.o is created if you have only a link problem ?
Can you send it ?
Philippe
Posts: 105
Joined: Wed Sep 12, 2012 8:10 am
Location: Paris
by edy_3dz » Thu Dec 27, 2012 5:16 pm
No test.o is generated with make.
I can generate test.o separately with "arm-none-eabi-gcc -nostartfiles source/test.c build/gpio.o -o build/test.o", but it's not something that I want to make in the future, when I'll have multiple .C files. Anyway, even after I build the test.o object file, I have problem with linking:
arm-none-eabi-ld --no-undefined build/gpio.o build/main.o build/systemTimer.o build/test.o -Map kernel.map -o build/output.elf -T kernel.ld
build/test.o: In function `GetGpioAddress':
:(.text+0x4): multiple definition of `GetGpioAddress'
build/gpio.o:(.text+0x0): first defined here
build/test.o: In function `SetGpio':
:(.text+0x5c): multiple definition of `SetGpio'
build/gpio.o:(.text+0x58): first defined here
make: *** [build/output.elf] Error 1

In the attachment you can find the test.o object file.
test.zip
(829 Bytes) Downloaded 61 times
Posts: 15
Joined: Mon Jul 09, 2012 3:33 pm
Location: Brasov, Romania
by phil95 » Thu Dec 27, 2012 7:49 pm
1/ "arm-none-eabi-gcc -nostartfiles source/test.c build/gpio.o -o build/test.o"
Why the build/gpio.o ?
And the syntax of the command is wrong

2/ In the makefile:
for the compilation of c files:
put "$(ARMGNU)-gcc -c $< -o $@"
insteed "$(ARMGNU)-gcc $< -o $@"

OUF !!!
Philippe
Posts: 105
Joined: Wed Sep 12, 2012 8:10 am
Location: Paris
by edy_3dz » Thu Dec 27, 2012 8:39 pm
Putting -c to gcc as a parameter seems to resolve my problems, as no error is raised. But I'm pretty sure something is not right, probably in my code, as the OK led doesn't blink as I wanted. I'll come back the day after tomorrow, when I'll have some time to look into it.

Thanks Philippe for your help!
Posts: 15
Joined: Mon Jul 09, 2012 3:33 pm
Location: Brasov, Romania
by phil95 » Fri Dec 28, 2012 8:32 am
I have compiled (!!!) your code after the -c modification
I have downloaded the kernel.img on the RPI
And congratulations, the LED is blinking !!!
Philippe
Posts: 105
Joined: Wed Sep 12, 2012 8:10 am
Location: Paris
by edy_3dz » Fri Dec 28, 2012 10:01 am
That code didn't use the C function, so it was normal to work as it is :P
Anyway, I managed to make it work eventually, after reading lots of code on github :lol:

Thanks for your help anyway.
Posts: 15
Joined: Mon Jul 09, 2012 3:33 pm
Location: Brasov, Romania
by Chadderz » Fri Dec 28, 2012 10:20 am
Seems I'm a bit late to the party, but for future reference here is a fairly minimal extension of the OK series to use both C and C++. I'll put it on the tutorial when I next update it.
Note that the extern "C" is only needed in C++ to stop name mangling.
Attachments
c_cpp_example.zip
A C/C++ and assembly code example.
(6.12 KiB) Downloaded 133 times
User avatar
Posts: 63
Joined: Thu Aug 30, 2012 12:50 pm
Location: Cambridge, UK
by codyrigney92 » Sat Jan 12, 2013 1:20 am
Thank you for posting the final source! I was looking how to do something like this also and this will definitely help. Do you know what the extent of the c++ libraries you can use? For example, I would think iostream could be useless for console out.
Posts: 57
Joined: Fri Dec 14, 2012 2:04 am
by codyrigney92 » Sun Jan 13, 2013 12:37 am
Alright turns out I couldn't make use of the STL at all. If anyone is successful or has tips please let me know.
Posts: 57
Joined: Fri Dec 14, 2012 2:04 am
by DavidS » Sun Jan 13, 2013 1:32 am
If you are using C (not c++) the format of an external function declaration is:
extern rtype fnname(params);
ARM Assembly Language: For those that want: Simple, Powerful, Easy to learn, and Easy to debug.
User avatar
Posts: 1251
Joined: Thu Dec 15, 2011 6:39 am
Location: USA