# How to export environment variables globally?

**URL:** https://discourse.cmake.org/t/how-to-export-environment-variables-globally/13892
**Category:** Code
**Created:** [April 7, 2025, 1:20pm UTC](https://discourse.cmake.org/t/how-to-export-environment-variables-globally/13892 "2025-04-07T13:20:07Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![CsatiZoltan](https://discourse.cmake.org/user_avatar/discourse.cmake.org/csatizoltan/32/5488_2.png) [@CsatiZoltan](https://discourse.cmake.org/u/CsatiZoltan)
#### Post date: [April 7, 2025, 1:20pm UTC](https://discourse.cmake.org/t/how-to-export-environment-variables-globally/13892/1 "2025-04-07T13:20:07Z")

</div>

Hi,

I have a C++ project with Doxygen documentation. I set variables in the CMake script, such as

```auto
set(P4LBM_HOME ${CMAKE_CURRENT_SOURCE_DIR})
set(P4LBM_DOC ${P4LBM_HOME}/docs)
add_custom_target(docs
        COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
        COMMENT "Generating API documentation with Doxygen"
        VERBATIM)

```

In _Doxyfile.in_, I read the values of these variables using e.g.

```auto
HTML_FOOTER = @P4LBM_DOC@/doxygen_footer.html

```

The documentation is built successfully with `make docs`.

* * *

Now I want to be able to build the documentation in GitLab CI without building the main code and without CMake. So I replace the CMake `set` commands with environment variables in the _.gitlab-ci.yml_ file:

```auto
pages:
  stage: deploy
  script:
    - mkdir build
    - export ROOT=$PWD
    - export P4LBM_DOC=$ROOT/docs
    - doxygen docs/Doxyfile.in

```

Environment variables in Doxygen are read with the `$(...)` syntax, not with `@...@`. In the example above:

```auto
HTML_FOOTER = $(P4LBM_DOC)/doxygen_footer.html

```

Once I perform these changes in _Doxyfile.in_, the documentation builds properly on the CI.

* * *

Now I cannot build the documentation locally with CMake using `make docs` because _Doxyfile.in_ expects environment variables, not CMake variables. And adding the `ENV` keywords, e.g.

```auto
set(ENV{P4LBM_DOC} ${P4LBM_HOME}/docs)

```

does not make a difference, probably because [CMake cannot inject environment variables into the parent process](https://gitlab.kitware.com/cmake/cmake/-/issues/22624#note_1015296). Is there a way to let Doxygen know about these environment variables from within CMake? Alternatively, I thought about writing the documentation creation commands

```auto
export ROOT=$PWD
export P4LBM_DOC=$ROOT/docs
doxygen docs/Doxyfile.in

```

into a Bash script and call this script from CMake. The drawback is that it would only work on OSs which can interpret Bash scripts.

---

<div class="post-metadata">

### Author: ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)
#### Post date: [April 7, 2025, 7:24pm UTC](https://discourse.cmake.org/t/how-to-export-environment-variables-globally/13892/2 "2025-04-07T19:24:15Z")

</div>

You can replace your doxygen command in your cmake with

```cmake
add_custom_target(docs
        COMMAND ${CMAKE_COMMAND} -E env VAR=val
           ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
        COMMENT "Generating API documentation with Doxygen"
        VERBATIM)

```

---

<div class="post-metadata">

### Author: ![CsatiZoltan](https://discourse.cmake.org/user_avatar/discourse.cmake.org/csatizoltan/32/5488_2.png) [@CsatiZoltan](https://discourse.cmake.org/u/CsatiZoltan)
#### Post date: [April 8, 2025, 7:59am UTC](https://discourse.cmake.org/t/how-to-export-environment-variables-globally/13892/3 "2025-04-08T07:59:36Z")

</div>

```auto
add_custom_target(docs
		COMMAND ${CMAKE_COMMAND} -E env P4LBM_DOC=${P4LBM_HOME}/docs LBMGRID_DOC ${P4LBM_HOME}/visualization DOXYGEN_IN=${P4LBM_HOME}/docs/Doxyfile.in DOXYGEN_OUT=${CMAKE_CURRENT_BINARY_DIR}/Doxyfile.out ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
        COMMENT "Generating API documentation with Doxygen"
        VERBATIM)

```

gives me the following error:

```auto
Generating API documentation with Doxygen
No such file or directory
make[3]: *** [CMakeFiles/docs.dir/build.make:71: CMakeFiles/docs] Error 1
make[2]: *** [CMakeFiles/Makefile2:555: CMakeFiles/docs.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:562: CMakeFiles/docs.dir/rule] Error 2
make: *** [Makefile:202: docs] Error 2

```

Where can I check the paths generated by CMake and a verbose error message?

---

<div class="post-metadata">

### Author: ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)
#### Post date: [April 8, 2025, 8:19am UTC](https://discourse.cmake.org/t/how-to-export-environment-variables-globally/13892/4 "2025-04-08T08:19:31Z")

</div>

> [@CsatiZoltan](#):
>
> `LBMGRID_DOC`

looks like your command is missing an = after LBMGRID\_DOC

---

<div class="post-metadata">

### Author: ![CsatiZoltan](https://discourse.cmake.org/user_avatar/discourse.cmake.org/csatizoltan/32/5488_2.png) [@CsatiZoltan](https://discourse.cmake.org/u/CsatiZoltan)
#### Post date: [April 8, 2025, 8:53am UTC](https://discourse.cmake.org/t/how-to-export-environment-variables-globally/13892/5 "2025-04-08T08:53:17Z")

</div>

Rookie mistake. Thanks a lot, it works perfectly!
