|
|
View previous topic :: View next topic |
Author |
Message |
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
Replacing fputc() with custom function |
Posted: Thu Jun 01, 2006 7:24 am |
|
|
Hi,
I'd like to replace fputc with a custom function that calls my own routines for IO. My understanding is that when I do this fprintf() etc should make use of it as well.
I'm creating my own fputc but the compiler seems to be linking in its own version, not mine. Is it possible to do this with CCS? My compiler version is: 3.245
cheers
ed |
|
|
Ttelmah Guest
|
|
Posted: Thu Jun 01, 2006 7:42 am |
|
|
Don't try to use names that are already in use. In C++, you can do this, 'overriding' the existing function, but in C, this is not allowed.
I can't actually see any reason why you'd need to do this, for what you describe!. If you want to print to your own function, then use code like:
Code: |
void my_putc (int val) {
//output value here
}
printf(my_putc,"This will be sent to your function/n");
|
Best Wishes |
|
|
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
Cheers |
Posted: Thu Jun 01, 2006 8:28 am |
|
|
Hi,
Cheers for the tip I didn't realise you could do that. I'm sure you can do this kind of function replacement with C as I've done it before on an ARM processor. Its not about C++ style function overloading but about the compiler choosing to use the last instance of the function it sees rather than the original one.
I'll try the trick with printf.
cheers
ed |
|
|
Ttelmah Guest
|
|
Posted: Thu Jun 01, 2006 9:25 am |
|
|
OK. Yes, you should be able to do this with a true 'function', just as with a variable, where the 'local' copy should have priority over a version defined further out. However two problems arise. A lot of the CCS 'functions', are actually macros, rather than functions, and therefore require redefinition using the define ability, not a function name, and all of the 'library' functions, are defined at the same level as the normal function definitions (there is not a linker - yet...), which is normally required for true redefinition like this. Basically, in a 'linked' compiler, the external functions, are linked, when a local version does not already exist. CCS, is not a 'linked' language at present.
So you can redefine fputc, if required, with:
#define fputc(x,y) myfunc(x,y)
However the printf ability, is usually easier. :-)
Best Wishes |
|
|
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
cheers Ttelmah |
Posted: Thu Jun 01, 2006 9:30 am |
|
|
cheers Ttelmah, what a great answer!
ed |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|