0
void* l = dlsym(lib,"_ZN11Environment9LibLogger14log_processingEiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEjS6_z"); 

void (*log_fcn)(int level, std::string format, unsigned int line_no, std::string file_name, ...) = reinterpret_cast<void (*log_fcn)>(l);

above log_fcn is a function pointer i am casting it but facing error

273K
  • 29,503
  • 10
  • 41
  • 64
  • `void (*log_fcn)` is not the type you want to cast to. You want `void (*)(int, std::string, unsigned int, std::string, ...)`. I would suggest using an alias. – Miles Budnek May 12 '22 at 03:18
  • extern void (*log_fcn)(int level,std::string, unsigned int, std::string, ...); is function pointer deceleration in its .h file – adarsh kumar May 12 '22 at 03:19
  • @MilesBudnek can you please suggest to use alias and get it. – adarsh kumar May 12 '22 at 03:21
  • 1
    `using LogFunction = void(*)(int, std::string, unsigned int, std::string, ...); LogFunction log_fcn = retinterpret_cast(l);` – Miles Budnek May 12 '22 at 03:28
  • @MilesBudnek copied and used same getting linker error. – adarsh kumar May 12 '22 at 03:34
  • _Some linker error_ cannot be solved via type-cast. – Lorinczy Zsigmond May 12 '22 at 04:22
  • 1
    This seems like a new profile for [this one](https://stackoverflow.com/users/17182382/m-n-adarsh-kumar). The question is a follow up question of [warning: inline variables are only available with ‘-std=c++17’ or ‘-std=gnu++17’ how to suppress it?](https://stackoverflow.com/questions/72181312/warning-inline-variables-are-only-available-with-std-c17-or-std-gnu17/72182461#72182461). – Jason May 12 '22 at 04:50
  • @anooprana yes it's my personal account I wanted to know why conversion is not happening so out of interest posted it. – adarsh kumar May 12 '22 at 05:21
  • @LorinczyZsigmond what would be desireable solution for this. – adarsh kumar May 12 '22 at 05:24
  • You should quote the linker error message you got; it might be completely unrelated to the type-cast – Lorinczy Zsigmond May 12 '22 at 05:59
  • @LorinczyZsigmond getting as below. /usr/include/c++/9/ostream:622:more undefined references to log_fcn [abi:cxx11] follow – adarsh kumar May 12 '22 at 06:06
  • Some object module [the one that contains the definition of functor-variable `log_fcn`] is missing from linkage list. – Lorinczy Zsigmond May 12 '22 at 07:01
  • `log_fcn` is a variable, not a type. Why don't you use the answer you accepted [here](https://stackoverflow.com/questions/72184187/return-address-of-dlsym-and-address-of-function-pointer-assigned)? – molbdnilo May 12 '22 at 08:01
  • @molbdnilo in previous question I just wanted to print the value of log_fcn to check the address what it holds. How I can I use here? – adarsh kumar May 12 '22 at 10:09

1 Answers1

0

You seem to want

void (*log_fcn)(int level, std::string format, unsigned int line_no, std::string file_name, ...) = 
   reinterpret_cast<decltype(log_fcn)>(l);
273K
  • 29,503
  • 10
  • 41
  • 64