Set cmake env variable

I have a stackoverflow topic that summarizes everything

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 …

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

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.

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

I entered them in the command line

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.

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

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):

example_command --arg1 --arg2
another_command

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

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} ..

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
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} …

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

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

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.

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

Could very well be.

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

I’ll try that and see what happens.

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.

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)

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.

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} …

`

Answer:

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