1

I have a header file with all the declarations, one .cpp file that is compiled to a .o file and a .cpp file with the main method.

The strange thing is, the program won't compile (I'm using g++ on cygwin) when a certain function in the .o file is declared inline in the header; only when declared normally.

So, do inline functions have to be declared in the same file that they're used?

2
  • 3
    The definition for an inline function has to be visible in every translation unit that uses it. This usually means putting the definition in a header file. Commented Mar 7, 2014 at 12:00
  • 1
    @Simple That should be an answer. Commented Mar 7, 2014 at 12:00

1 Answer 1

3

So, do inline functions have to be declared in the same file that they're used?

All functions (and, in general, all named entities) must be declared before use; an inline function must also be defined in every translation unit in which it is odr-used.

For this reason, they are usually defined in headers, so that the definition can be included in more than one translation unit.

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

1 Comment

I see! Thanks, that should be easy to fix.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.