This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Tool/software: TI C/C++ Compiler
I've been puzzled as to why overriding functions with pragma weak statements, as suggested by the HALCoGen-generated code, has not been working for me. After some investigation, it seems my issue is that my pragma weak functions are in a static library.
In a minimal example project, the core looks like this:
/////////////// better_test_func.c /////////////// int test_func(void) { return 100; } /////////////// better_test_func.h /////////////// #ifndef BETTER_FUNC_BETTER_TEST_FUNC_H_ #define BETTER_FUNC_BETTER_TEST_FUNC_H_ int test_func(void); #endif /* BETTER_FUNC_BETTER_TEST_FUNC_H_ */ /////////////// calls_test_func.c /////////////// #pragma WEAK(test_func) int test_func(void) { return 0; } void calls_test_func(void) { int a = test_func(); }
This works in a normal application project. When transplanted into a static library, with no obvious configuration differences besides the minimum to get the build to succeed, the weak version of the function is used instead.
I'm a little hazy as to whether one would expect to be able to reliably override a weak static-library function in a project using that library, but I would have expected to be able to use pragma weak within a library. Can someone enlighten me as to the reason for this not working?
Thanks!
I am unable to reproduce this behavior. Here is how I tried to reproduce it.
C:\work>type inside_lib.c #pragma WEAK(weak_func) int weak_func(void) { return 0; } void calls_weak_func(void) { int a = weak_func(); } C:\work>armcl -mv7r4 --code_state=16 --opt_level=off inside_lib.c C:\work>armar -r w.lib inside_lib.obj ==> building archive 'w.lib' C:\work>type main.c void calls_weak_func(void); int weak_func(void) { return 10; } int main() { calls_weak_func(); } C:\work>armcl -mv7r4 --code_state=16 --opt_level=off main.c -z -o main.out -m main.map w.lib <Linking>
Then I inspect main.map. Find the address of weak_func. Then determine that it comes from main.obj, and not inside_lib.obj.
I use ARM compiler version 18.12.3.LTS.
Please send me a similar test case which demonstrates the problem behavior.
Thanks and regards,
-George
Thanks for the info. I'm using the TI v18.1.3.LTS compiler.
So my example correctly overrides when I do the strong function implementation in the application project as your example does. Previously I had been putting both the weak and strong versions in the library. I have tried putting the strong implementation in my real parent project (a static library) but it doesn't immediately work.
What are the general rules for using pragma weak with static libraries?
If it's supposed to work, I've attached an example where the strong version is in the library but it is not used.
In general what I'm trying to do is create hooks in a library such that test functionality can be inserted by using an alternate project but the same core code. I realize there are quite a number of ways to do this. It's seeming that I may be better off even just disabling/enabling files in the Project Explorer view of projects, even if only because it's generally more readily clear what version of a function is being used. Still, I'd very much like to know what I'm doing wrong, and it's also potentially applicable to working with the HALCoGen-generated code.
Thanks again!
dduncan said:What are the general rules for using pragma weak with static libraries?
I was unable to find any firm documentation on this particular point. I found a wikipedia article that mentions it, but I couldn't find any documentation to back it up.
dduncan said:I've attached an example where the strong version is in the library but it is not used.
Thank you. I can reproduce the behavior. I verified that GCC tools behave the same way. So, while it isn't want you want, it is consistent.
I think of it this way. The main application code calls the function calls_test_func. The function calls_test_func is in a library. The function calls_test_func in turn calls test_func. The function test_func is a weak function defined in the same module in the library. The linker satisfies the open reference to test_func with the first symbol it finds in the library, without regard for whether it is weak. In this case, it finds a definition of weak_func in a library module that is already brought in. The alternative is, for every weak function, to search all the libraries in the link, just in case a strong form of the function is available. If implemented, that means the linker runs longer. Perhaps much longer.
dduncan said:In general what I'm trying to do is create hooks in a library such that test functionality can be inserted by using an alternate project
I probably misunderstand. I don't know why you need weak functions for that. Just specify a set of functions and their interfaces. The core code calls those functions. Different static libraries provide different implementations of those functions that adhere to the specified interface.
Thanks and regards,
-George