I want to use a function that from the "func.h" file in the Wireshark open source project. I need to use the funct() function in multiple .cpp files, but I get the a multiple definition error.
func.h:
#ifndef func_h
#define func_h
#include<string>
void *funct(char *cName)
{
std::string name = cName;
cName+= ".extension";
}
In the .cpp files I include the func.h:
#include "func.h"
And call the funct() function from 2 .cpp files:
funct("program");
What should I do so I don't get the multiple definition error? A workaround is to copy and paste the function defition in every .cpp file and change the function name, but this is ugly.
Many thanks.
.cppfile, or mark itinlinein the.h.staticonly guards a definition from being accessed from other translation units, but here we have definitions in multiple TUsstatic, yes, we have definitions in multiple TUs. But this does not break the ODR, because each definition defines a separate function. Live demo: wandbox.org/permlink/506cFfJKnuMniOrT.