What is Weak linkage?
Answers
Answered by
0
Hey Friends here is the best answer of your question------------------^^^^^^^----------------------
When linking C programs there are (in general) only a couple of errors you’re likely to see. If, for example, you have two functions in different files, both with external linkage, then the files will compile okay, but when you link you’ll likely see an error along these lines:....
Most of the time this makes sense and is as expected; however there is a particular instance where it gets in the way.
If we need to supply a code framework where we need placeholders (stubs) for someone else to fill in at a later date, it can sometimes mean developing complex makefiles and/or conditional compilation to allow new code to be introduced as seamlessly as possible.
However, there is a hidden gem supported by most linkers called “weak linkage”. The principle of weak linkage is that you can define a function and tag it as (surprisingly) weak, e.g.
// foo_weak.c
__weak int foo(void)
{
// ...
return 1;
}
This then can be called from the main application:
// main.c
int foo(void);
int main(void)
{
foo();
while(1);
}
This project can build built as normal:....
Hope it will help you-------------------------------
mind me as BRAINLIES----------------DEAR
****************(ROYAL RAJPUT)******************
When linking C programs there are (in general) only a couple of errors you’re likely to see. If, for example, you have two functions in different files, both with external linkage, then the files will compile okay, but when you link you’ll likely see an error along these lines:....
Most of the time this makes sense and is as expected; however there is a particular instance where it gets in the way.
If we need to supply a code framework where we need placeholders (stubs) for someone else to fill in at a later date, it can sometimes mean developing complex makefiles and/or conditional compilation to allow new code to be introduced as seamlessly as possible.
However, there is a hidden gem supported by most linkers called “weak linkage”. The principle of weak linkage is that you can define a function and tag it as (surprisingly) weak, e.g.
// foo_weak.c
__weak int foo(void)
{
// ...
return 1;
}
This then can be called from the main application:
// main.c
int foo(void);
int main(void)
{
foo();
while(1);
}
This project can build built as normal:....
Hope it will help you-------------------------------
mind me as BRAINLIES----------------DEAR
****************(ROYAL RAJPUT)******************
Answered by
0
Weak linkage refers to the linking error is C programming while linking two functions having external linkages.
However, the functions will compile together easily but won't be linked. You would see the weak linkage error as like the following lines.
linking… weak_linkage.axf:
Error: L6200E: Symbol foo multiply defined (by foo.o and foo2.o).
Target not created A symbol can be said weak linkage using special types of compilers command.
Weak linkage is usually placed in order to implement a replaceable spot or creating a void in the function.
However, the functions will compile together easily but won't be linked. You would see the weak linkage error as like the following lines.
linking… weak_linkage.axf:
Error: L6200E: Symbol foo multiply defined (by foo.o and foo2.o).
Target not created A symbol can be said weak linkage using special types of compilers command.
Weak linkage is usually placed in order to implement a replaceable spot or creating a void in the function.
Similar questions