0

Using below function in one of the .h file. if i am using function deceleration without inline getting multiple definitions error and now after adding inline getting above warning so help me to suppress the warning i mean how to fix the warning.

inline void (*log_fcn)(int level, std::string format, unsigned int line_no, std::string file_name, ...);
273K
  • 29,503
  • 10
  • 41
  • 64
  • I removed the C tag since your code is clearly C++. – Nate Eldredge May 10 '22 at 05:54
  • Do you insist one whatever older C++ version you are currently compiling with? Why don't you switch to one of those which do support what you are trying to use? What I mean is that you do not want to suppress the warning, you want to fix it. – Yunnosch May 10 '22 at 05:55
  • 1
    Please provide a [mcve]. `log_fcn` is not a function, but a variable ( of type function pointer). – Quimby May 10 '22 at 05:55
  • 2
    Note that this line does not declare a *function*, it declares a *function pointer*, which does not make sense to be `inline`. – Nate Eldredge May 10 '22 at 05:55
  • yes i need to fix it. – M N Adarsh Kumar May 10 '22 at 05:56
  • `log_fcn` is a pointer to a function and not a name of the function. That is, you're not actually declaring a function named `log_fcn`. – Jason May 10 '22 at 05:56
  • So how about setting up for building with C++17? Are you aware of your configs which seem to force an older version? Can you show it? I.e. how exactly do you compile/build? – Yunnosch May 10 '22 at 05:57
  • Anoop Rana yes its a function pointer later point i am giving address of one function to it. – M N Adarsh Kumar May 10 '22 at 06:00
  • build is with automake and normall g++ compiler bases Yunnosch. – M N Adarsh Kumar May 10 '22 at 06:01
  • @MNAdarshKumar Are you sure that you're using C++17? If yes then this should work. Also refer to [How do inline variables work](https://stackoverflow.com/questions/38043442/how-do-inline-variables-work). **Inline variables are a C++17 feature.** – Jason May 10 '22 at 06:02
  • Please only tag the version of c++ you are using – Alan Birtles May 10 '22 at 06:03
  • @AnoopRana i am using c++ 11 how can i upgrade it. – M N Adarsh Kumar May 10 '22 at 06:13
  • @MNAdarshKumar For one you can use manually the command `g++ -std=c++17 main.cpp -o your_program` to compile your program with C++17 instead of C++11. Also refer to [Compiling C++11 with g++](https://stackoverflow.com/questions/10363646/compiling-c11-with-g). In the above linked question, just change c++11 to c++17 wherever applicable. – Jason May 10 '22 at 06:16
  • @AnoopRana i am using automake to build how it can be done – M N Adarsh Kumar May 10 '22 at 06:38
  • Why do you feel the need for an inline variable? What's wrong with a "normal" global variable? – molbdnilo May 10 '22 at 07:37
  • @MNAdarshKumar You can use `extern` with C++11 as shown below in my answer. – Jason May 10 '22 at 07:46

2 Answers2

2

Using below function in one of the .h file.

It is not a function. It is a variable of function pointer type.

how to fix the warning

Option 1: Use C++17 or later language version.

Option 2: Don't use C++17 features like inline variables. To use a non-inline namespace scope variable, you can declare it extern in the header, and define it in one translation unit.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • /usr/include/c++/9/ostream:622: undefined reference to `log_fcn[abi:cxx11]' /usr/bin/ld: component_CTL/src/BoardSelect.o: in function `Environment::BoardSelect::parse_board_name(std::__cxx11::basic_string, std::allocator >)': /home/adarsh/GIT_NEW_COMPONENTCTL/bosch-linuxandroid-dev/code/component_CTL/src/BoardSelect.cpp:86: undefined reference to `log_fcn[abi:cxx11]' /usr/bin/ld: component_CTL/src/BoardSelect.o:/usr/include/c++/9/ostream:622: more undefined references to `log_fcn[abi:cxx11]' follow getting above error message after using extern – M N Adarsh Kumar May 10 '22 at 11:15
  • @MNAdarshKumar Did you do `and define it in one translation unit.`? – eerorika May 10 '22 at 11:23
  • void (*log_fcn)(int level, std::string format, unsigned int line_no, std::string file_name, ...); log_fcn = reinterpret_cast(l); getting error for this defination – M N Adarsh Kumar May 10 '22 at 12:25
0

inline variables are a C++17 feature. But since you're using C++11 you can make use of extern as shown below:

header.h

#ifndef MYHEADER_H
#define MYHEADER_H

#include <string>
//THIS IS A DECLARATION. Note the use of extern keyword here. 
extern void (*log_fcn)(int level, std::string format, unsigned int line_no, std::string file_name, ...);

#endif 

source.cpp

#include "header.h"
void func(int level, std::string format, unsigned int line_no, std::string file_name, ...)
{
    
}
//this is definition
void (*log_fcn)(int level, std::string format, 
                unsigned int line_no, std::string file_name, ...) = func;
//------------------------------------------------------------------^^^^--->initializer

main.cpp

#include "header.h"

int main()
{
    
}

Working demo.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • arana /usr/include/c++/9/ostream:622: undefined reference to log_fcn[abi:cxx11]' /usr/bin/ld: component_CTL/src/BoardSelect.o: in function Environment::BoardSelect::parse_board_name(std::__cxx11::basic_string, std::allocator >)': /home/adarsh/GIT_NEW_COMPONENTCTL/bosch-linuxandroid-dev/code/component_CTL/src/BoardSelect.cpp:86: undefined reference to log_fcn[abi:cxx11]' /usr/bin/ld: component_CTL/src/BoardSelect.o:/usr/include/c++/9/ostream:622: more undefined references to log_fcn[abi:cxx11]' follow getting above error message after using extern – M N Adarsh Kumar May 10 '22 at 11:16
  • @MNAdarshKumar In my answer i have given a link to a [working demo](https://wandbox.org/permlink/HoGPWZ6SSGdCv3dx), just copy-paste from that demo and that should work. The error that you're getting is a *linker error* that suggest that you had not placed the definition of `log_fcn` in a source file(.cpp). So just add a corresponding definition in the source file. – Jason May 10 '22 at 11:19
  • @MNAdarshKumar Don't forget to add the **definition** `void (*log_fcn)(int level, std::string format, unsigned int line_no, std::string file_name, ...);` in the source file and the **declaration** `extern void (*log_fcn)(int level, std::string format, unsigned int line_no, std::string file_name, ...);` in the header file. – Jason May 10 '22 at 11:27
  • void (*log_fcn)(int level, std::string format, unsigned int line_no, std::string file_name, ...); log_fcn = reinterpret_cast(l); – M N Adarsh Kumar May 10 '22 at 12:25