Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pragma once in .cpp file

Tags:

c++

pragma

Recently reading some pieces of code I encountered several .cpp files that contained #pragma once in the beginning of file. I know that it is usually used in .h files as guards.

What are the cases when #pragma once should/can/must be used in .cpp files?

like image 347
mouse_00 Avatar asked May 06 '26 16:05

mouse_00


1 Answers

#pragma once shouldn't be used in source files, its one goal is to act as include guard. It won't do much harm .cpp files are normally going to be "scanned" once during compilation anyway. Note: Clang tidy will warn you if you do it.

Warning clang-diagnostic-pragma-once-outside-header #pragma once in main file   
like image 74
Pepijn Kramer Avatar answered May 08 '26 06:05

Pepijn Kramer