{"id":16603143,"url":"https://github.com/laserpants/dotenv-cpp","last_synced_at":"2025-10-29T15:30:52.346Z","repository":{"id":42230320,"uuid":"147963865","full_name":"laserpants/dotenv-cpp","owner":"laserpants","description":":cow: A utility to load environment variables from a .env file","archived":false,"fork":false,"pushed_at":"2024-05-19T19:21:40.000Z","size":82,"stargazers_count":52,"open_issues_count":3,"forks_count":16,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-02T02:11:47.259Z","etag":null,"topics":["cpp","dotenv","dotenv-cpp","header-only"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/laserpants.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-08T19:32:04.000Z","updated_at":"2025-02-02T01:04:38.000Z","dependencies_parsed_at":"2022-09-03T11:42:09.837Z","dependency_job_id":null,"html_url":"https://github.com/laserpants/dotenv-cpp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laserpants%2Fdotenv-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laserpants%2Fdotenv-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laserpants%2Fdotenv-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laserpants%2Fdotenv-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/laserpants","download_url":"https://codeload.github.com/laserpants/dotenv-cpp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238840757,"owners_count":19539608,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","dotenv","dotenv-cpp","header-only"],"created_at":"2024-10-12T00:47:41.560Z","updated_at":"2025-10-29T15:30:47.054Z","avatar_url":"https://github.com/laserpants.png","language":"C++","readme":"# dotenv-cpp\n\n## Installation\n\nThe library is header-only, so there is nothing to build. You could simply merge the `include` directory into your project root, entering something like \n\n```bash\nmkdir -p include\ncp -rn \u003cpath-to-this-repo\u003e/dotenv-cpp/include/ .\n```\n\nand compile, adding `-I include/laserpants/dotenv` to your build command, which then allows you to `#include \"dotenv.h\"`.\n\nAlthough the above method is sufficient for many use cases; for proper CMake support, you should install the library using a sequence of command such as\n\n```bash\ncd \u003cpath-to-this-repo\u003e\nmkdir -p build\ncd build\ncmake ..\nmake\nsudo make install\n```\n\nBy default, the headers will then be installed to `include/laserpants/dotenv-\u003cVERSION\u003e/`, relative to the CMake install prefix (`/usr/local/` on Linux/Unix). To compile *without* CMake, you can then use a flag\n\n```\n-I /usr/local/include/laserpants/dotenv-\u003cVERSION\u003e\n```\n\nor the equivalent path on other platforms. \n\nFor CMake-based projects, your project's `CMakeLists.txt` file could look something like the following:\n\n```cmake\ncmake_minimum_required(VERSION 3.2)\nproject(test)\n\nfind_package(laserpants_dotenv)\n\nadd_executable(example example.cpp)\ntarget_link_libraries(example laserpants::dotenv)\n```\n\nThen, in `example.cpp`, just\n\n```cpp\n#include \u003cdotenv.h\u003e\n```\n\n## Usage\n\n### Example\n\nGiven a file `.env`\n\n```shell\nDATABASE_HOST=localhost\nDATABASE_USERNAME=user\nDATABASE_PASSWORD=\"antipasto\"\n```\n\nand a program `example.cpp`\n\n```cpp\n// example.cpp\n#include \u003ciostream\u003e\n#include \u003cdotenv.h\u003e\n\nint main()\n{\n    dotenv::init();\n\n    std::cout \u003c\u003c std::getenv(\"DATABASE_USERNAME\") \u003c\u003c std::endl;\n    std::cout \u003c\u003c std::getenv(\"DATABASE_PASSWORD\") \u003c\u003c std::endl;\n\n    return 0;\n}\n```\n\nthe output is:\n\n```\nuser\nantipasto\n```\n\n### Default values\n\n`dotenv::getenv()` is a wrapper for `std::getenv()` that also takes a default value, in case the variable is empty:\n\n```cpp\nstd::cout \u003c\u003c dotenv::getenv(\"DATABASE_USERNAME\", \"anonymous\") \u003c\u003c std::endl;\n```\n\n### Referencing other variables\n\nOther variables can be referenced using either `${VARIABLE}` or `$VARIABLE`.\n\n#### Example: \n\nGiven this program\n\n```cpp\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\n#include \"dotenv.h\"\n\nint main(int argc, char **argv)\n{\n   dotenv::init(\"resolve.env\");\n   std::cout \u003c\u003c std::getenv(\"MESSAGE\") \u003c\u003c std::endl;\n   return 0;\n}\n```\n\nand this `resolve.env`\n\n```\nTEMPERATURE = cold\nEXTENT      =    \"  long  \"\nSEASON      = '$EXTENT $TEMPERATURE winter'\nMORE        =    and $TEMPERATURE ice\nCONTAINS    = lots of ${TEMPERATURE} snow $MORE\nMESSAGE     =    I wish you a ${SEASON}, with $CONTAINS\n```\n\nthe output becomes\n\n```\nI wish you a   long   cold winter, with lots of cold snow and cold ice\n```\n\n### Options\n\nBy default, if a name is already present in the environment, `dotenv::init()` will replace it with the new value. To preserve existing variables, you must pass the `Preserve` flag.\n\n```cpp\ndotenv::init(dotenv::Preserve);\n\nstd::cout \u003c\u003c std::getenv(\"DATABASE_USERNAME\") \u003c\u003c std::endl;\nstd::cout \u003c\u003c std::getenv(\"DATABASE_PASSWORD\") \u003c\u003c std::endl;\n```\n\nUpdating the previous example, recompiling and running the program again, after setting the `DATABASE_USERNAME` variable, e.g. as\n\n```\nDATABASE_USERNAME=root ./example\n```\n\nthe output this time is:\n\n```\nroot\nantipasto\n```\n\n## Changelog\n\n### 0.9.3\n\n#### Added\n- Add cctype header needed by std::isspace\n\n### 0.9.2\n\n#### Added\n- Add support for referencing other variables in variable definitions\n\n### 0.9.1\n\n#### Added\n- Add wrapper for setenv to support MSVC\n\n### 0.9.0\n\n- Initial version\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaserpants%2Fdotenv-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaserpants%2Fdotenv-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaserpants%2Fdotenv-cpp/lists"}