| ... | ... | @@ -38,16 +38,15 @@ How to do it well |
|
|
|
|
|
|
|
### Conditional compilation
|
|
|
|
|
|
|
|
"Conditional compilation" in short is what its name suggests: compile depending on a condition. There are two ways to do this:
|
|
|
|
- Include <stdlib.h> (this is important!) and use `getenv()`. For example:
|
|
|
|
You have seen compiler guards before in header files in the form of
|
|
|
|
|
|
|
|
```
|
|
|
|
if (getenv("INPUT")) {
|
|
|
|
freopen(getenv("INPUT"), "r", stdin);
|
|
|
|
freopen(getenv("OUTPUT"), "w", stdout);
|
|
|
|
} // if
|
|
|
|
#ifndef VAR
|
|
|
|
#define VAR
|
|
|
|
...
|
|
|
|
#endif
|
|
|
|
```
|
|
|
|
- You can also use compiler guards. You may have seen this before in header guards in the form of `#ifndef PATH_TO_FILE`. You can read more about it [here](https://msdn.microsoft.com/en-us/library/ew2hz0yd.aspx). On Mac OS, the operating system macro is `__APPLE__`
|
|
|
|
You can read more about these [here](https://msdn.microsoft.com/en-us/library/ew2hz0yd.aspx). On Mac OS, the operating system macro is `__APPLE__`.
|
|
|
|
|
|
|
|
```
|
|
|
|
#ifdef __APPLE__
|
| ... | ... | @@ -56,5 +55,15 @@ How to do it well |
|
|
|
#endif
|
|
|
|
```
|
|
|
|
|
|
|
|
This will compile with `freopen()` *only* when the operating system is *Mac OS*, so when your code compiles on CAEN, those lines are ignored.
|
|
|
|
|
|
|
|
### Using environment variables
|
|
|
|
|
|
|
|
Another solution is to include \<stdlib.h\> (this is important!) and use `getenv()`, which lives in stdlib.h.
|
|
|
|
|
|
|
|
```
|
|
|
|
if (getenv("INPUT")) {
|
|
|
|
freopen(getenv("INPUT"), "r", stdin);
|
|
|
|
freopen(getenv("OUTPUT"), "w", stdout);
|
|
|
|
} // if
|
|
|
|
``` |
|
|
\ No newline at end of file |