1

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.

9
  • 6
    Put the definition in its own .cpp file, or mark it inline in the .h. Commented Jul 1, 2021 at 8:28
  • 1
    Declare the function static Commented Jul 1, 2021 at 8:31
  • @JanGabriel That won't help. static only guards a definition from being accessed from other translation units, but here we have definitions in multiple TUs Commented Jul 1, 2021 at 8:34
  • I'm not sure if this is the best duplicate, but here's one: Is it a good practice to define C++ functions inside header files? Commented Jul 1, 2021 at 8:37
  • 1
    @Yksisarvinen Actually, it will help. With static, 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. Commented Jul 1, 2021 at 9:11

2 Answers 2

5

You have two options:

  • You move the implementation of the function in a separate .c/.cpp file, leaving in the header file only the declaration. Then you compile this source file, and link it with the rest of the program. Example:

func.h

#ifndef func_h
#define func_h
#include<string>

void *funct(char *cName);
#endif

func.cpp

#include "func.h"

void *funct(char *cName)
{
    std::string name = cName;
    cName+= ".extension";
}

You compile and link as usual, e.g.

g++ -c -o func.o func.cpp
g++ -c -o main.o main.cpp
g++ -o prog main.o func.o
  • You specify the keyword inline in the definition of the function. This allows appearance of the function in multiple compile units. See this question.
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @AlexSeceleanu if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this.
2

func.cpp:

#include "func.h"

void *funct(char *cName)
{
  std::string name = cName;
  cName+= ".extension";
}

func.h:

#ifndef func_h
#define func_h
#include<string>
    
void *funct(char *cName);
#endif

Then in your build script (CMakeLists.txt, or a simple Makefile), compile both .cpp files and link them into one executable.

You may want to put using namespace std; or using std::string; (check the latter syntax; I usually use the former), in which case you don't have to write std::.

1 Comment

Please don't recommend using namespace std;, it is widely considered to be a bad practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.