0

I have created header file abc.hpp which contains a namespace of multiple functions.

When I use single source file which includes the header and call the function defined in namespace it works well, but when I call the same function in project where I have to include this header in multiple places, compilation gives me multiple definition error.

My other files are created as library and get executed.


abc.hpp:

#ifndef __ABC_HPP__
#define __ABC_HPP__

namespace abc {

    void sendtext(const char* msg)
    {
        create_context(mycontext);
        send_context(create_context,msg)
    }

    void sendtextwitherrno(const char* msg, int errn)
    {
        create_context(mycontext);
        send_context(create_context,msg, errn)
    }
};

#endif

test.cpp:

#include"abc.hpp"
int main()
{
    abc::sendtext("hello world");
    abc::sendtextwitherrno("hello error no",140);
    return 0;
}
5
  • 1
    If you put a function definition in a header, better mark it inline. Commented Jul 11, 2020 at 8:06
  • i understand, when i create namespace and call the function from multiple files, it give me error of multiple definition. But i have this requirement to create a namespace and use it all along the project. i am stuck to solve this Commented Jul 11, 2020 at 8:12
  • Inline function is also giving me multiple definition error Commented Jul 11, 2020 at 8:19
  • It cannot possibly do that, you are doing something wrong. Commented Jul 11, 2020 at 9:20
  • with inline here without inline here Commented Jul 11, 2020 at 9:29

1 Answer 1

2

You can treat header files as if they are copy-pasted into your implementation files. So by putting function definitions into your headers, those definitions appear in multiple translation units (roughly, compiler .cpp files). This is not allowed - how is the compiler supposed to tell between them if they end up different?

Usually you would have a .h/.cpp pair, with function declarations going in the .h, and function definitions going in the .cpp. This way the definition only appears once in your whole program.

I can split your code like this:

abc.hpp:

#ifndef __ABC_HPP__
#define __ABC_HPP__
// function **declarations**
namespace abc{
    void sendtext(const char* msg);
    void sendtextwitherrno(const char* msg, int errn);
}
#endif

abc.cpp:

#include "abc.hpp"
#include "context.hpp" // I guess this might be where create_context and send_context come from
// function **definitions**
namespace abc{
    void sendtext(const char* msg){
        create_context(mycontext); // not sure where mycontext is supposed to live?
        send_context(create_context,msg)
    }

    void sendtextwitherrno(const char* msg, int errn){
        create_context(mycontext);
        send_context(create_context,msg, errn);
    }
}

test.cpp:

#include "abc.hpp"
int main(){
    abc::sendtext("hello world");
    abc::sendtextwitherrno("hello error no",140);
    return 0;
}

Another way is to mark the functions as inline - this tells the compiler you are deliberately repeating the function definition all over the place, and you'd better be sure the definition is always identical. How sure are you? Are there different macros or different compiler flags in place in different places the function is used?

Sign up to request clarification or add additional context in comments.

1 Comment

thank for reply, i am trying to create a wrapper functions of a protocol, which will be used as a generic to logging the error. and i thought a simple approch of declaring a namespace which initialise all the protocol specific routine and helps user to pass a simple text, rest the wrapper will take

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.