How do I create a weak function via Keil Cx51?
Have you taken a look through the options available?
http://www.keil.com/support/man/docs/c51/
I don't recell there being such an option, but then I'd never felt the need for it - why do you feel the need for it?
Yes, But I can't find any information I can use. I want to create a function like putchar() that the user can modify if they need.
Weak functions look like a useful feature of a toolchain, but they are actually not - really. I have seem them being abused by so many who considered themselves are "developers", only to produce unmaintainable, unbeliable junk code. In other words: don't.
So just provide it as source - then the user can modify/replace/remove that source as required.
Or provide it in a library (a true pre-built library) - the linker will only take stuff from the library that is otherwise undefined ...
Note that a "WEAK" attribute just allows the user to completely replace the function - it does not help if the user wants to keep the function, but just modify it ...
Thanks for your suggestion. However, it would release the function description instead of the source code to the user because of the application issue.
For example, it would have a prototype hook function with the arguments, and then the system handling will depend on the result of the return. The user would create the hook function for themselves need.
If you really want to do it, then an alternative might be to simply have your code module contain a function to set a callback function pointer and call that function if (for example) non-null. OK, pointers in C51 aren't very efficient, but I suspect your application doesn't go for being ultra efficient.
I gave two suggestions - which one are you referring to?
"However, it would release the function description instead of the source code to the user because of the application issue."
But my first suggestion was precisely to "release" the source code!
"For example, it would have a prototype hook function with the arguments, and then the system handling will depend on the result of the return. The user would create the hook function for themselves need."
I don't follow what you're saying there - but I don't think what you're describing would be covered by "weak" functions anyhow ?
Also remember that the 8051 is a very simple controller, from a simpler age - not really intended for doing "clever" stuff. If you really want to do "clever" stuff, there are far better controllers available these days ...
"I don't follow what you're saying there - but I don't think what you're describing would be covered by "weak" functions anyhow?"
The project of the company I work in. There is a kernel in the project and the kernel would need to call a function to the user to indicate that there is an event happens.
If the user needs to handle the event, then it needs to port the code in the function. otherwise, the user doesn't need to handle the event. they could do nothing about the function.
That is why I want to use the weak function. I can create the function which it calls to the user as a weak function to do default handling. The user can choose the behavior of the function is default or they want to do more.
So implementing a callback mechanism would be a possible solution.
"If the user needs to handle the event, then it needs to port the code in the function. otherwise, the user doesn't need to handle the event. they could do nothing about the function."
Then having the default handler in a library (a true, pre-built, binary) would solve that - the linker will only take the function from the library if it isn't provided by the user.
Providing it as source would also work - the user only has to touch the "default" source when they want to do specific handling. This is what you'd have to do anyhow with a "WEAK" attribute.
A callback would also be possible but, as already noted, function pointers are problematic in C51.
How about C++ on a C51 :-)
Filip Natronce would be the man to ask about that
http://www.keil.com/forum/10074/
"Then having the default handler in a library (a true, pre-built, binary) would solve that - the linker will only take the function from the library if it isn't provided by the user."
Yes, I just want to create a library-like kernel. Then providing a function description like putchar() for users that can modify it if they want to. If users want to use the kernel I make, they just include the head file then they can use the function about the kernel. Because some kind of function in the kernel is necessary for the application. So the user couldn't allow renaming that functions. But if they want to do more things. They can create the function with the same function names, then modify it.