A C program/library to read environment variables from a .env file, similar to other packages/libraries available in various programming languages.
- To use dotenv library in your project simply add the
dotenv.canddotenv.hfiles in your project along with your.envfile as shown below or create a shared library as per your need:
├── .env
├── dotenv.c
├── dotenv.h
└── main.c
- Once
dotenv.his included in your file, invoke theload_envfunction which takes filepath of the.envfile as an argument:
// filename: main.c
// other include statements
#include <stdlib.h>
#include "dotenv.h"
int main()
{
load_env(".env"); // if the .env file is in the current directory.
// use the getenv method in the <stdlib.h> to get the value of your environment variable.
char myVar[] = getenv("myVar");
// other logic...
}- Your
.envfile can look something like this:
myVar="myVarValue"
myvar = "myvarvalue"
#mycommentedvar = "mycommentedvarvalue"- Then compile you're program using
gccor similar compiler:
gcc main.c dotenv.c -o main
- Or if you created a shared library:
gcc main.c -ldotenv -o main # or whatever you named your library