简体   繁体   中英

Make two functions have the same memory address in C++

I am building some shared libraries to test different algorithms. The header of all shared libraries should look like this:

extern "C" {
  void f1(params);
  void f2(params);
}

where both functions have exactly the same parameters.

In one of the implementations, I realized that I would need need to have f1 == f2 , but I want them to share the same memory space.

Is there a way to do that? I know that function pointers usually do the trick, but this time I need to follow the shared library standard.

It's platform specific. For GCC, you can do this:

extern "C" {
  void f1(params);
  void f2(params) __attribute__((weak, alias("f1")));
}

Ref: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM