Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

260
Visualizações
Trouble setting environment variables for CTest tests

I'm tasked with building python bindings for a c++-based project (using swig). The project uses cmake to build and ctest to test and the build and test of the bindings are supposed to be integrated into this.

I've gotten the build to work and the tests work when run manually, but I have to set a couple of environment variables in order for them to work and I'm having trouble setting those for the automated process.

I need to set LD_LIBRARY_PATH and PYTHONPATH. PYTHONPATH I can get around by manipulating sys.path within the testing script, but that's harder to do with LD_LIBRARY_PATH. So far I have the following added to the CMakelists.txt in the testing directory:

#Python wrapper testing

find_package(PythonInterp 3.5 REQUIRED)

if (NOT PYTHONINTERP_FOUND)
  message(STATUS "Python interpreter NOT found")
else(NOT PYTHONINTERP_FOUND)
  message(STATUS "Python interpreter found")
  ADD_TEST(NAME testPyMyproj
       COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_scripts/test_pyMyproj.py
       )
  set_property(TEST testPyMyproj PROPERTY ENVIRONMENT LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/lib/)
endif (NOT PYTHONINTERP_FOUND)

The error I am getting is

ImportError: libMyproj.so: cannot open shared object file: No such file or directory

Which is the library my bindings are linked to and is in the directory specified by ${CMAKE_BINARY_DIR}/lib/.

I take this to mean that $LD_LIBRARY_PATH is not set correctly, but I don't know what I'm doing wrong.

Is there a way to check within the test what the state of the variable is? Can anyone spot what I am doing wrong?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can set environment variables as part of invoking the test by using the cmake -E env command. You can modify the add_test() call to something like the following:

ADD_TEST(NAME testPyMyproj
    COMMAND ${CMAKE_COMMAND} -E env
        LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/lib:$ENV{LD_LIBRARY_PATH}
        ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_scripts/test_pyMyproj.py
)

The above assumes a Unix-based host environment, but you could generalise this to support all platforms/generator types with a bit of work if you needed to.

Another alternative is to use the ENVIRONMENT test property which should achieve essentially the same thing:

set_tests_properties(testPyMyproj PROPERTIES
    ENVIRONMENT LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/lib:$ENV{LD_LIBRARY_PATH})
over 4 years ago · Santiago Trujillo Relatório

0

Craig Scott already provided a great solution via ENVIRONMENT test properties. I just want to add how this works on Windows with the Visual Studio generator and generator expressions:

set_tests_properties(
    name_of_test_one
    name_of_test_two
PROPERTIES
    # Make sure DLL is found by adding its directory to PATH
    ENVIRONMENT "PATH=$<TARGET_FILE_DIR:library_target_name>\;$ENV{PATH}"
)

Note 1: Instead of a colon, use an escaped semi-colon Note 2: I'm using the more flexible $ generator expression

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda