49 lines
883 B
Markdown
49 lines
883 B
Markdown
|
|
---
|
||
|
|
tags:
|
||
|
|
- C
|
||
|
|
---
|
||
|
|
|
||
|
|
In order to create a file that you can import into other C files you need to
|
||
|
|
create a header file.
|
||
|
|
|
||
|
|
> Remember that this isn't really a module since `.h` files and `#includes` are
|
||
|
|
> just text substition
|
||
|
|
|
||
|
|
In fact you create a `.h` header file which links to a `.c` file which actually
|
||
|
|
contains the code you want to import.
|
||
|
|
|
||
|
|
Let's say we want to create a "module" that handles connecting to WiFi:
|
||
|
|
|
||
|
|
```c
|
||
|
|
// wifi.h
|
||
|
|
|
||
|
|
#ifndef WIFI_H
|
||
|
|
#define WIFI_H
|
||
|
|
|
||
|
|
void wifi_connect(void);
|
||
|
|
|
||
|
|
#endif
|
||
|
|
```
|
||
|
|
|
||
|
|
In the `.h` file we just define the function signature and put checks (so it
|
||
|
|
doesn't get redefined).
|
||
|
|
|
||
|
|
Then the implementation file:
|
||
|
|
|
||
|
|
```c
|
||
|
|
// main/wifi.c
|
||
|
|
|
||
|
|
// Add any header files or auxiliary functions that the main (wifi_connect)
|
||
|
|
// function will require here
|
||
|
|
|
||
|
|
void wifi_connect(void) {}
|
||
|
|
```
|
||
|
|
|
||
|
|
Then in your main entrypoint:
|
||
|
|
|
||
|
|
```c
|
||
|
|
// main/main.c
|
||
|
|
#include "wifi.h"
|
||
|
|
wifi_connect()
|
||
|
|
```
|