Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Wrapping C functions with GNU linker
- //number.cpp
- int giveMeANumber() {
- return rand() % 6 + 1;
- }
- //test.cpp
- extern "C" int __wrap_rand(void) {
- return 4;
- }
- void unitTest() {
- assert giveMeANumber() == 5;
- }
- $ g++ test.cpp -o test number.o -Xlinker --wrap=rand
- //number.cpp
- int foo() {
- //some complex calculations I would like to mock
- }
- int giveMeANumber() {
- return foo() % 6 + 1;
- }
- //test.cpp
- extern "C" int __wrap_foo(void) {
- return 4;
- }
- $ g++ test.cpp -o test number.o -Xlinker --wrap=foo
- $ cat x.cc
- #include <iostream>
- using namespace std;
- int giveMeANumber();
- int main() {
- cerr << giveMeANumber() << endl;
- return 0;
- }
- $ cat y.cc
- int giveMeANumber() {
- return 0;
- }
- extern "C" int __wrap__Z13giveMeANumberv() {
- return 10;
- }
- $ g++ -c x.cc y.cc && g++ x.o y.o -Wl,--wrap=_Z13giveMeANumberv && ./a.out
- 10
Add Comment
Please, Sign In to add comment