Epistemic Review
Confidence: 70%
Importance
Low. This is more of a quick and handy reference for when I encounter something similar in the future
Background Context
I don’t remember the context accurately but I was trying to figure out how to get SDL2 running in Windows. I was trying to port my satellite simulator C++ code into Windows from Linux to work on it but it failed because SDL2 wasn’t being detected
Sources
#pragma
is a compiler directive that directs the compiler to perform actions in specified ways. Pragmas can affect the behaviour of the compiler
This adds libname
to the list of library dependencies as if you have manually added it
There’s another important use of #pragma
, especially #pragma once
. It functions like an include guard, ensuring that the program file it’s in gets compiled only once. This is important in files where the header file it resides in is mentioned in multiple program files/header files. Moreover, almost all the major and popular C/C++ compilers except for SDCC accept its usage.
No need to compile header files
I was being a rock and I did
gcc program.c header.h -o test
where#pragma once
was inheader.h
. Naturally, I got a warning →warning: #pragma once in main file
. This warning is harmless and the solution is to not pass header files to gcc
The name “pragma
” comes from the word “pragmatic” and was used far back in ALGOL 68 as pragmats, which were compiler directives. Think of it as telling the compiler to act pragmatically