Skip to content
Search

CMake:Troubleshooting

lastmod 2025-02-14created 2016-04-1912 edits

Solutions to problems that can occur when using CMake.

Objective-C compile

See Objective-C#CMake compile.

CMake function target

To make a CMake script a target (Target), apply it as follows.

add_custom_target(target_name
    COMMAND ${CMAKE_COMMAND} -P path_to_script
)

list argument

If a list is not passed as a function parameter, check whether you applied quoting (Quoting).

test_function (${LIBRARY_NAMES_LIST}) # ERROR.
test_function ("${LIBRARY_NAMES_LIST}") # OK!

Not found stdio.h

At some point, odd behavior may suddenly occur, such as the make command being unable to find stdio.h. In this case, check for things like duplicated variables. The problem most likely arose from using include or similar.

list does not recognize sub-command FILTER

If the CMake version is lower than 3.6, the following error may occur.

list does not recognize sub-command FILTER

This is because the list command has no FILTER command. You can work around it by writing a function as follows.

function (__remove_windows_files __result)
    set (${__result})
    foreach (__source_cursor ${ARGN})
        if (NOT "${__source_cursor}" MATCHES "windows[^/]*$")
            list (APPEND ${__result} "${__source_cursor}")
        endif ()
    endforeach ()
    set (${__result} ${${__result}} PARENT_SCOPE)
endfunction ()

## ...
file(GLOB LIBFLAC_SOURCES src/libFLAC/*.c)

if (CMAKE_VERSION VERSION_LESS "3.6")
    __remove_windows_files (LIBFLAC_SOURCES_TEMP ${LIBFLAC_SOURCES})
    set (LIBFLAC_SOURCES ${LIBFLAC_SOURCES_TEMP})
    unset (LIBFLAC_SOURCES_TEMP)
else ()
    list(FILTER LIBFLAC_SOURCES EXCLUDE REGEX "windows[^/]*$")
endif ()

Environment variable to execute_process

To pass environment variables when running execute_process, do the following.

set(ENV{LD_LIBRARY_PATH} my_path)
execute_process(COMMAND my_process)

To print the list of all accessible variables, do the following.

get_cmake_property(_variableNames VARIABLES)
foreach (_variableName ${_variableNames})
    message(STATUS "${_variableName}=${${_variableName}}")
endforeach()

find_path variable not working

On Ubuntu 14.04, when functions such as find_library and find_path are used in succession, they may not work properly if the first argument (OUTPUT VARIABLE) is the same. Therefore, do not use the same argument name.

CMake: Could NOT find PythonLibs

When CMake cannot find Python using FindPythonLibs, the following message is printed.

Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS)

In this case, add PYTHON_LIBRARY and PYTHON_INCLUDE_DIR to the cmake options.

$ cmake -DPYTHON_LIBRARY=/home/username/local/lib/libpython2.7.a -DPYTHON_INCLUDE_DIR=/home/username/local/include/python2.7

install_name absolute path

If a library fails to link, checking the rpath and so on with something like otool -L liba.dylib may show that the library is not applied with an absolute path. This is because on MaxOSX, the cmake_install.cmake file is generated during the cmake step, and it uses install_name_tool to erase the path. 1

To write an absolute path as the install_name, do the following.

set (CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/lib")

See also

References


  1. Inside the cmake_install.cmake script, install_name_tool -id liba.3.0.dylib /Users/user/Downloads/liba.3.0.dylib is used to overwrite the install_name path. ↩

author Zer0 sitemap