# Set cmake env variable

**URL:** <https://discourse.cmake.org/t/set-cmake-env-variable/5771>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [June 1, 2022, 2:58pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771 "2022-06-01T14:58:45Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![whatheway](https://discourse.cmake.org/user_avatar/discourse.cmake.org/whatheway/32/2471_2.png) [@whatheway](https://discourse.cmake.org/u/whatheway)\
**Post date:** [June 1, 2022, 2:58pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/1 "2022-06-01T14:58:45Z")

</div>

I have a stackoverflow topic that summarizes everything

> <https://stackoverflow.com/questions/21047399/cmake-set-environment-variables-from-a-script>

I think I need to set the env variable but it isn’t working with any way I try.

I’ve tried:

$ENV={JASPER\_LIBRARIES} $ENV={JASPER\_INCLUDE\_DIR} $ENV={JPEG\_INCLUDE\_DIR} $ENV={JPEG\_LIBRARY} $ENV={FCFLAGS} $ENV={FFLAGS} $ENV={CXX} $ENV={FC} $ENV={CC}

-E env JASPER\_LIBRARIES=$DIR/grib2/lib cmake -E env JASPER\_INCLUDE\_DIR=$DIR/grib2/include cmake -E env JPEG\_INCLUDE\_DIR=$DIR/grib2/include cmake -E env JPEG\_LIBRARY=$DIR/grib2/lib cmake -E env FCFLAGS=-fallow-argument-mismatch cmake -E env FFLAGS=-fallow-argument-mismatch cmake -E env CXX=g++ cmake -E env FC=gfortran cmake -E env CC=gcc

cmake set(ENV{JPEG\_LIBRARY} $DIR/grib2/lib) cmake …

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [June 1, 2022, 3:20pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/2 "2022-06-01T15:20:02Z")

</div>

> [@whatheway](#):
>
> $ENV={JASPER\_LIBRARIES} $ENV={JASPER\_INCLUDE\_DIR} $ENV={JPEG\_INCLUDE\_DIR} $ENV={JPEG\_LIBRARY} $ENV={FCFLAGS} $ENV={FFLAGS} $ENV={CXX} $ENV={FC} $ENV={CC}

> [@whatheway](#):
>
> -E env JASPER\_LIBRARIES=$DIR/grib2/lib cmake -E env JASPER\_INCLUDE\_DIR=$DIR/grib2/include cmake -E env JPEG\_INCLUDE\_DIR=$DIR/grib2/include cmake -E env JPEG\_LIBRARY=$DIR/grib2/lib cmake -E env FCFLAGS=-fallow-argument-mismatch cmake -E env FFLAGS=-fallow-argument-mismatch cmake -E env CXX=g++ cmake -E env FC=gfortran cmake -E env CC=gcc

In what contexts did you try these? On the command line, as `add_custom_command`s, CMake code?

> [@whatheway](#):
>
> cmake set(ENV{JPEG\_LIBRARY} $DIR/grib2/lib) cmake …

I’m unsure what this is trying to depict. The middle bit is CMake code for setting the `JPEG_LIBRARY` envvar to `$DIR/grib2/lib` (the literal string; there is no CMake variable expansion going on here), but the bracketing `cmake` words are confusing.

---

<div class="post-metadata">

**Author:** ![whatheway](https://discourse.cmake.org/user_avatar/discourse.cmake.org/whatheway/32/2471_2.png) [@whatheway](https://discourse.cmake.org/u/whatheway)\
**Post date:** [June 1, 2022, 3:23pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/3 "2022-06-01T15:23:06Z")

</div>

So what I am trying to do is pass along manual entered values to the cmake command which makes other cmake commands.

---

<div class="post-metadata">

**Author:** ![whatheway](https://discourse.cmake.org/user_avatar/discourse.cmake.org/whatheway/32/2471_2.png) [@whatheway](https://discourse.cmake.org/u/whatheway)\
**Post date:** [June 1, 2022, 3:23pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/4 "2022-06-01T15:23:40Z")

</div>

I entered them in the command line

---

<div class="post-metadata">

**Author:** ![whatheway](https://discourse.cmake.org/user_avatar/discourse.cmake.org/whatheway/32/2471_2.png) [@whatheway](https://discourse.cmake.org/u/whatheway)\
**Post date:** [June 1, 2022, 3:24pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/5 "2022-06-01T15:24:51Z")

</div>

Also tried this just now: and it runs but the variables are not going to the other cmake files

cmake -DJPEG\_LIBRARY=${JPEG\_LIBRARY} -DJPEG\_INCLUDE\_DIR=${JPEG\_INCLUDE\_DIR} -DJASPER\_LIBRARIES=${JASPER\_LIBRARIES} -DJASPER\_INCLUDE\_DIR=${JASPER\_INCLUDE\_DIR} -DCMAKE\_INSTALL\_PREFIX=${INSTALL\_PREFIX} …

Sorry I am a new user and not a computer programmer so I am stumbling around.

---

<div class="post-metadata">

**Author:** ![whatheway](https://discourse.cmake.org/user_avatar/discourse.cmake.org/whatheway/32/2471_2.png) [@whatheway](https://discourse.cmake.org/u/whatheway)\
**Post date:** [June 1, 2022, 3:25pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/6 "2022-06-01T15:25:52Z")

</div>

This is the error that shows up in my terminal.

> BlockCMake Error at /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):  
> Could NOT find Jasper (missing: JASPER\_LIBRARIES JASPER\_INCLUDE\_DIR  
> JPEG\_LIBRARIES)  
> Call Stack (most recent call first):  
> /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (\_FPHSA\_FAILURE\_MESSAGE)  
> /usr/share/cmake-3.22/Modules/FindJasper.cmake:62 (find\_package\_handle\_standard\_args)  
> CMakeLists.txt:24 (find\_package)  
> quote

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [June 1, 2022, 3:28pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/7 "2022-06-01T15:28:58Z")

</div>

> [@whatheway](#):
>
> I entered them in the command line

Only the `-E env` bit is remotely valid shell syntax. Can you please show exactly what you are trying using something like this (using the triple-` markdown syntax):

```sh
example_command --arg1 --arg2
another_command

```

> [@whatheway](#):
>
> So what I am trying to do is pass along manual entered values to the cmake command which makes other cmake commands.

How does it make other CMake commands? Can you show the CMake code you’re using?

---

<div class="post-metadata">

**Author:** ![whatheway](https://discourse.cmake.org/user_avatar/discourse.cmake.org/whatheway/32/2471_2.png) [@whatheway](https://discourse.cmake.org/u/whatheway)\
**Post date:** [June 1, 2022, 3:32pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/8 "2022-06-01T15:32:10Z")

</div>

I think this is what you are asking for…

`cmake -DJPEG_LIBRARY=${JPEG_LIBRARY} -DJPEG_INCLUDE_DIR=${JPEG_INCLUDE_DIR} -DJASPER_LIBRARIES=${JASPER_LIBRARIES} -DJASPER_INCLUDE_DIR=${JASPER_INCLUDE_DIR} -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} ..`

---

<div class="post-metadata">

**Author:** ![whatheway](https://discourse.cmake.org/user_avatar/discourse.cmake.org/whatheway/32/2471_2.png) [@whatheway](https://discourse.cmake.org/u/whatheway)\
**Post date:** [June 1, 2022, 3:34pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/9 "2022-06-01T15:34:08Z")

</div>

For show you the code I am git cloning this file

`git clone https://github.com/NOAA-EMC/NCEPLIBS`

then running the following commands.

> cd $HOME/WRF/Downloads  
> git clone [GitHub - NOAA-EMC/NCEPLIBS: Top level repo containing submodules for NCEPLIBS and associated dependencies for superproject builds](https://github.com/NOAA-EMC/NCEPLIBS)  
> cd NCEPLIBS  
> mkdir -p build && cd build  
> export CC=gcc  
> export FC=gfortran  
> export CXX=g++  
> export FFLAGS=-std=legacy  
> export FCFLAGS=-std=legacy  
> export JPEG\_LIBRARY=$DIR/grib2/lib  
> export JPEG\_INCLUDE\_DIR=$DIR/grib2/include  
> export JASPER\_LIBRARIES=$DIR/grib2/lib  
> export JASPER\_INCLUDE\_DIR=$DIR/grib2/include  
> export INSTALL\_PREFIX=$HOME/WRF/Libs/nceplibs

cmake -DJPEG\_LIBRARY=${JPEG\_LIBRARY} -DJPEG\_INCLUDE\_DIR=${JPEG\_INCLUDE\_DIR} -DJASPER\_LIBRARIES=${JASPER\_LIBRARIES} -DJASPER\_INCLUDE\_DIR=${JASPER\_INCLUDE\_DIR} -DCMAKE\_INSTALL\_PREFIX=${INSTALL\_PREFIX} …

---

<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:** [June 1, 2022, 4:06pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/10 "2022-06-01T16:06:34Z")

</div>

I think you should use a toolchain file, instead of setting all these variables from the command line or the environment.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [June 1, 2022, 5:01pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/11 "2022-06-01T17:01:44Z")

</div>

This mostly seems fine. So the problem is that with this, the find modules aren’t working?

> [@whatheway](#):
>
> export JPEG\_LIBRARY=$DIR/grib2/lib  
> export JASPER\_LIBRARIES=$DIR/grib2/lib

These are certainly wrong though. `JPEG_LIBRARY` is the path to the library, not its directory. `JASPER_LIBRARIES` might be a list, but there should be singular library cache variables to set instead.

---

<div class="post-metadata">

**Author:** ![whatheway](https://discourse.cmake.org/user_avatar/discourse.cmake.org/whatheway/32/2471_2.png) [@whatheway](https://discourse.cmake.org/u/whatheway)\
**Post date:** [June 1, 2022, 5:07pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/12 "2022-06-01T17:07:34Z")

</div>

I’m not sure. Again first time using cmake command.

There used to be a github you clone that had everything install for you. Then you just point to it and it and it would install nceplibs.

My guess is there is something in there it needs.

[https://github.com/NOAA-EMC/NCEPLIBS-external](https://github.com/NOAA-EMC/NCEPLIBS-external)

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [June 1, 2022, 5:11pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/13 "2022-06-01T17:11:20Z")

</div>

> [@whatheway](#):
>
> My guess is there is something in there it needs.

Could very well be.

The `--debug-find` option to CMake can help to diagnose problems behind `find_package`-reported errors.

---

<div class="post-metadata">

**Author:** ![whatheway](https://discourse.cmake.org/user_avatar/discourse.cmake.org/whatheway/32/2471_2.png) [@whatheway](https://discourse.cmake.org/u/whatheway)\
**Post date:** [June 1, 2022, 5:13pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/14 "2022-06-01T17:13:13Z")

</div>

I’ll try that and see what happens.

Cmake output to terminal can be logged using &\> output.txt?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [June 1, 2022, 5:16pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/15 "2022-06-01T17:16:20Z")

</div>

> [@whatheway](#):
>
> Cmake output to terminal can be logged using &\> output.txt?

Yep, just like any other command. Different parts go to stderr and stdout, but that will grab both.

---

<div class="post-metadata">

**Author:** ![whatheway](https://discourse.cmake.org/user_avatar/discourse.cmake.org/whatheway/32/2471_2.png) [@whatheway](https://discourse.cmake.org/u/whatheway)\
**Post date:** [June 5, 2022, 12:03am UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/21 "2022-06-05T00:03:27Z")

</div>

So I am trying to my nceblibs from NOAA-EMC ([GitHub - NOAA-EMC/NCEPLIBS: Top level repo containing submodules for NCEPLIBS and associated dependencies for superproject builds](https://github.com/NOAA-EMC/NCEPLIBS))

These are the libraries I have installed on ubuntu 22.04.

- zlib 1.2.12
- libpng 1.6.37
- Jasper 1.900.1
- mpich 4.0.2
- hdf5 1.12.2
- netcdf-c 4.8.1
- netcdf-fortran 4.5.4
- libjpeg 9.1
- gcc/gfortran/g++ 11.2.0

I am exporting them to the bash terminal via these commands:

```
export CC=gcc
export FC=gfortran
export CXX=g++
export FFLAGS=-std=legacy
export FCFLAGS=-std=legacy
export JPEG_LIBRARY=$DIR/grib2/lib
export JPEG_INCLUDE_DIR=$DIR/grib2/include
export JASPER_LIBRARIES=$DIR/grib2/lib
export JASPER_INCLUDE_DIR=$DIR/grib2/include
export INSTALL_PREFIX=$HOME/WRF/Libs/nceplibs

```

I run cmake to build the make files using:

```
cmake -DJPEG_LIBRARIES=${JPEG_LIBRARY} -DJASPER_LIBRARIES=${JASPER_LIBRARIES} -DJASPER_INCLUDE_DIR=${JASPER_INCLUDE_DIR} -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} ..

```

The output is from cmake is:

```
-- The C compiler identification is GNU 11.2.0
-- The Fortran compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Check for working Fortran compiler: /usr/bin/gfortran - skipped
-- Setting build type to 'Release' as none was specified.
-- Installing NCEPLIBS from /home/will/WRF/Downloads/NCEPLIBS/COMPONENTS
-- Installing NCEPLIBS at /home/will/WRF/Libs/nceplibs
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    JASPER_INCLUDE_DIR
    JASPER_LIBRARIES
    JPEG_LIBRARIES

```

Now I need JASPER\_INCLUDE\_DIR, JASPER\_LIBRARIES, JPEG\_LIBRARIES to execute make.

When I execute make without them in the cmakecache.txt file this happens.

```
[43%] Performing configure step for 'g2'
-- The C compiler identification is GNU 11.2.0
-- The Fortran compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Check for working Fortran compiler: /usr/bin/gfortran - skipped
-- Setting build type to 'Release' as none was specified.
-- Could NOT find JPEG (missing: JPEG_LIBRARY JPEG_INCLUDE_DIR) 
CMake Error at /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Jasper (missing: JASPER_LIBRARIES JASPER_INCLUDE_DIR
  JPEG_LIBRARIES)
Call Stack (most recent call first):
  /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.22/Modules/FindJasper.cmake:62 (find_package_handle_standard_args)
  CMakeLists.txt:24 (find_package)

-- Configuring incomplete, errors occurred!
See also "/home/will/WRF/Downloads/NCEPLIBS/build/g2/src/g2-build/CMakeFiles/CMakeOutput.log".
make[2]: *** [CMakeFiles/g2.dir/build.make:92: g2/src/g2-stamp/g2-configure] Error 1
make[1]: *** [CMakeFiles/Makefile2:348:

```

Now I have heard that you can use CMAKE\_LIBRARY\_PATH or CMAKE\_PREFIX\_PATH to get these libraries into cache file. But I am not sure how to do this. Why isn’t the exports working for the calls?

I have tried to manually put them into cmake.cache.txt using this:

```
JASPER_INCLUDE_DIR:UNINITIALIZED=/home/will/WRF/Libs/grib2/include

JASPER_LIBRARIES:UNINITIALIZED=/home/will/WRF/Libs/grib2/lib

JPEG_LIBRARIES:UNINITIALIZED=/home/will/WRF/Libs/grib2/lib

```

But I am not sure if I am doing the format correctly.

Any ideas would be helpful.

---

<div class="post-metadata">

**Author:** ![whatheway](https://discourse.cmake.org/user_avatar/discourse.cmake.org/whatheway/32/2471_2.png) [@whatheway](https://discourse.cmake.org/u/whatheway)\
**Post date:** [June 5, 2022, 12:31am UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/22 "2022-06-05T00:31:28Z")

</div>

Tried this and it gave the same output error in make

> cmake -DJPEG\_LIBRARIES:INTERNAL=${JPEG\_LIBRARY} -DJASPER\_LIBRARIES:INTERNAL=${JASPER\_LIBRARIES} -DJASPER\_INCLUDE\_DIR:INTERNAL=${JASPER\_INCLUDE\_DIR} -DJPEG\_INCLUDE\_DIR:INTERNAL=${JPEG\_INCLUDE\_DIR} -DCMAKE\_INSTALL\_PREFIX=${INSTALL\_PREFIX} …

`

---

<div class="post-metadata">

**Author:** ![whatheway](https://discourse.cmake.org/user_avatar/discourse.cmake.org/whatheway/32/2471_2.png) [@whatheway](https://discourse.cmake.org/u/whatheway)\
**Post date:** [June 5, 2022, 4:52pm UTC](https://discourse.cmake.org/t/set-cmake-env-variable/5771/23 "2022-06-05T16:52:53Z")

</div>

Answer:

` cmake -DCMAKE_PREFIX_PATH:PATH=$DIR/grib2 -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} ..`
