Home > Software design >  warning: inline variables are only available with ‘-std=c 17’ or ‘-std=gnu 17’ how to suppress it?
warning: inline variables are only available with ‘-std=c 17’ or ‘-std=gnu 17’ how to suppress it?

Time:05-10

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, ...);

CodePudding user response:

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.

CodePudding user response:

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.

  • Related