Is it possible in C/C++ (or, with any compiler) to overwrite a symbol specifically? For example there's a library a.lib
which contains, besides other functions, the implementation of foo()
. Now, in my program, I want to replace the implementation of foo()
with another function, so that any call to foo()
goes straight to my implementation, rather than the implementation in a.lib
.
Macros are not an option.
Is this possible without modifieng the source of a.lib
?
Yes, probably.
When you do a static link the linker loads symbols from the library if and only if they are needed. If the program, or an earlier library provides that symbol then it won't be on the "needed" list.
A problem arises if the library defined that symbol in the same "object" as other symbols that are needed. A well designed library has one object per symbol (basically, each function is defined in it's own source file, compiled separately, and added to the library individually). A poorly designed library might have objects (source files) that define several functions at once, and then you have to pull in all of them to get just one. Hopefully you don't have this problem.
For dynamic libraries on Linux systems (and other Unix) things are not so clear. You can define the function, and it might get used by your own callers, but callers within the library might still use the library copy. It's all a bit confusing. On Windows, .lib
files probably work similarly to Linux's .a
files, but .dll
files are quite different beasts, with different ways of being confusing.