0

In a library, I have the following in a header:

// button.hpp

class ExtraButtons
{
public:
    static inline void show() { setShown(true); }
    static inline void hide() { setShown(false); }
    static void setShown(bool shown);
};

and the following in a source file:

#include "button.hpp"

void ExtraButtons::setShown(bool shown)
{
}

The library compiles fine. However, when I'm using the library and I include button.hpp, I get this error from the setShown calls in show() and hide():

undefined reference to `ExtraButtons::setShown(bool)'

I can fix the problem by removing the inline from show() and hide() and defining them in the source file, like normal, OR by making setShown inline as well. Why is this the case? Are these the only solutions?

6
  • 1
    If you want this to link, you'll need to provide both object files to the linker. How are you invoking the linker? Commented Sep 4, 2013 at 18:49
  • I find it hard to believe that inline would have such a big effect. In my experience, inline is more a reminder to yourself that you expect the function to be compiled inline, rather than something else. Commented Sep 4, 2013 at 18:52
  • 2
    @MrLister: The purpose of inline is to allow definitions in multiple translation units (which is often necessary for the compiler to be able to inline it). But you're right that that shouldn't make a difference here. Commented Sep 4, 2013 at 19:07
  • That bool const & in the error message is a bit suspicious, when the type is just bool in the code. Are you sure you've shown us the exact code you're compiling? Have you changed the header and not recompiled some files that include it? Commented Sep 4, 2013 at 19:09
  • @MikeSeymour Sorry, the bool const & was a mistake on my part. I had just changed that in the code. Commented Sep 4, 2013 at 19:17

1 Answer 1

1

It looks like either you aren't linking the library binary into your project, or your actual definition for setShown is inline void ExtraButtons::setShown(bool shown) (note that it's accidentally marked inline in the source file).

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

Comments

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.