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!
Baking Pi and extending in C/C++
23 posts
- Posts: 15
- Joined: Mon Jul 09, 2012 3:33 pm
- Location: Brasov, Romania
C can use decorated name of routines.
This decoration indicate parameters, return type, ...
Try to put
__attribute__((naked)) void myGetGpioAddress()
Philippe
This decoration indicate parameters, return type, ...
Try to put
__attribute__((naked)) void myGetGpioAddress()
Philippe
- Posts: 90
- Joined: Wed Sep 12, 2012 8:10 am
- Location: Paris
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 
- Posts: 15
- Joined: Mon Jul 09, 2012 3:33 pm
- Location: Brasov, Romania
Ok then declare it as
extern "C"
{
extern void *GetGpioAddress(void);
}
Philippe
extern "C"
{
extern void *GetGpioAddress(void);
}
Philippe
- Posts: 90
- Joined: Wed Sep 12, 2012 8:10 am
- Location: Paris
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
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
Sorry,
extern "C"
{
void *GetGpioAddress(void);
}
or
extern "C" void *GetGpioAddress(void);
Philippe
extern "C"
{
void *GetGpioAddress(void);
}
or
extern "C" void *GetGpioAddress(void);
Philippe
- Posts: 90
- Joined: Wed Sep 12, 2012 8:10 am
- Location: Paris
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
What is the utility of the line with EXPORT_SYMBOL ?
Can tou try to remove it ?
Philippe
Can tou try to remove it ?
Philippe
- Posts: 90
- Joined: Wed Sep 12, 2012 8:10 am
- Location: Paris
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
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
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: 90
- Joined: Wed Sep 12, 2012 8:10 am
- Location: Paris
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 ....
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
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
For example, in my configuration, external "C" is working.
Good luck
Philippe
- Posts: 90
- Joined: Wed Sep 12, 2012 8:10 am
- Location: Paris
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.
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
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
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: 90
- Joined: Wed Sep 12, 2012 8:10 am
- Location: Paris
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.
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.
- Posts: 15
- Joined: Mon Jul 09, 2012 3:33 pm
- Location: Brasov, Romania
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
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: 90
- Joined: Wed Sep 12, 2012 8:10 am
- Location: Paris
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!
Thanks Philippe for your help!
- Posts: 15
- Joined: Mon Jul 09, 2012 3:33 pm
- Location: Brasov, Romania
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
I have downloaded the kernel.img on the RPI
And congratulations, the LED is blinking !!!
Philippe
- Posts: 90
- Joined: Wed Sep 12, 2012 8:10 am
- Location: Paris
That code didn't use the C function, so it was normal to work as it is 
Anyway, I managed to make it work eventually, after reading lots of code on github
Thanks for your help anyway.
Anyway, I managed to make it work eventually, after reading lots of code on github
Thanks for your help anyway.
- Posts: 15
- Joined: Mon Jul 09, 2012 3:33 pm
- Location: Brasov, Romania
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.
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 78 times
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: 52
- Joined: Fri Dec 14, 2012 2:04 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: 52
- Joined: Fri Dec 14, 2012 2:04 am
If you are using C (not c++) the format of an external function declaration is:
extern rtype fnname(params);
extern rtype fnname(params);
RISC OS, Giving the control of your computer to you.
- Posts: 646
- Joined: Thu Dec 15, 2011 6:39 am
- Location: USA