C++ extern keyword on functions. Why no just include the header file?

前端 未结 5 1735
无人及你
无人及你 2020-12-12 16:13

If I understand it correctly this means

extern void foo();

that the function foo is declared in another translation unit.

1) Why no

5条回答
  •  鱼传尺愫
    2020-12-12 17:07

    It already means that without the extern keyword. Functions have external linkage by default, unless you declare them static.

    Using function prototypes is okay but it is easy get it wrong. The linker error you'll get isn't that easy to diagnose when you redefine the function implementation. The linker doesn't know where to look, it is your job to give it an object file that contains the function definition to keep it happy.

提交回复
热议问题