Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Chester-Gillon/a7227a5a71e050d1c9da2ebe3c941a2e to your computer and use it in GitHub Desktop.
Save Chester-Gillon/a7227a5a71e050d1c9da2ebe3c941a2e to your computer and use it in GitHub Desktop.
Debug CMake CHECK_SYMBOL_EXISTS not finding function

With CMake 3.26.5 attempted to add the following to check for the pci_get_string_property function being supprted in the pciutils-devel package:

CHECK_SYMBOL_EXISTS ("pci_get_string_property" pci/pci.h HAVE_PCI_GET_STRING_PROPERTY)

With pciutils-devel-3.7.0-3.el8.x86_64 installed on AlmaLinux 8.9 the function exists, but CMake reported:

-- Looking for pci_get_string_property
-- Looking for pci_get_string_property - not found

The stack overflow CMake check_symbol_exists doesn't work because of missing -pthread explains CMake tries to compile a program to check the symbol exists, which can fail due to an unresolved symbol from the linker for a test program.

That stack overflow question suggested the log from the test program is in CMakeFiles/CMakeError.log, but that didn't exist.

A recursive search for pci_get_string_property found CMakeFiles/CMakeConfigureLog.yaml which had the error:

    kind: "try_compile-v1"
    backtrace:
      - "/usr/share/cmake/Modules/CheckSymbolExists.cmake:145 (try_compile)"
      - "/usr/share/cmake/Modules/CheckSymbolExists.cmake:71 (__CHECK_SYMBOL_EXISTS_IMPL)"
      - "CMakeLists.txt:85 (CHECK_SYMBOL_EXISTS)"
    checks:
      - "Looking for pci_get_string_property"
    directories:
      source: "/home/mr_halfword/fpga_sio/software_tests/eclipse_project/bin/coverage/CMakeFiles/CMakeScratch/TryCompile-R6AZRj"
      binary: "/home/mr_halfword/fpga_sio/software_tests/eclipse_project/bin/coverage/CMakeFiles/CMakeScratch/TryCompile-R6AZRj"
    cmakeVariables:
      CMAKE_C_FLAGS: "-g --coverage -fprofile-update=atomic --save-temps -Wall -Wconversion -fmessage-length=0"
    buildResult:
      variable: "HAVE_PCI_GET_STRING_PROPERTY"
      cached: true
      stdout: |
        Change Dir: /home/mr_halfword/fpga_sio/software_tests/eclipse_project/bin/coverage/CMakeFiles/CMakeScratch/TryCompile-R6AZRj
        
        Run Build Command(s):/usr/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_40459/fast && /usr/bin/gmake  -f CMakeFiles/cmTC_40459.dir/build.make CMakeFiles/cmTC_40459.dir/build
        gmake[1]: Entering directory '/home/mr_halfword/fpga_sio/software_tests/eclipse_project/bin/coverage/CMakeFiles/CMakeScratch/TryCompile-R6AZRj'
        Building C object CMakeFiles/cmTC_40459.dir/CheckSymbolExists.c.o
        /opt/GNAT/2021/bin/gcc  -I/home/mr_halfword/libpciaccess/include -I/home/mr_halfword/pciutils_install/usr/local/include -g --coverage -fprofile-update=atomic --save-temps -Wall -Wconversion -fmessage-length=0  -o CMakeFiles/cmTC_40459.dir/CheckSymbolExists.c.o -c /home/mr_halfword/fpga_sio/software_tests/eclipse_project/bin/coverage/CMakeFiles/CMakeScratch/TryCompile-R6AZRj/CheckSymbolExists.c
        Linking C executable cmTC_40459
        /usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_40459.dir/link.txt --verbose=1
        /opt/GNAT/2021/bin/gcc -g --coverage -fprofile-update=atomic --save-temps -Wall -Wconversion -fmessage-length=0  -rdynamic CMakeFiles/cmTC_40459.dir/CheckSymbolExists.c.o -o cmTC_40459 
        /opt/GNAT/2021/bin/../libexec/gcc/x86_64-pc-linux-gnu/10.3.1/ld: CMakeFiles/cmTC_40459.dir/CheckSymbolExists.c.o: in function `main':
        /home/mr_halfword/fpga_sio/software_tests/eclipse_project/bin/coverage/CMakeFiles/CMakeScratch/TryCompile-R6AZRj/CheckSymbolExists.c:8: undefined reference to `pci_get_string_property'
        collect2: error: ld returned 1 exit status
        gmake[1]: *** [CMakeFiles/cmTC_40459.dir/build.make:99: cmTC_40459] Error 1
        gmake[1]: Leaving directory '/home/mr_halfword/fpga_sio/software_tests/eclipse_project/bin/coverage/CMakeFiles/CMakeScratch/TryCompile-R6AZRj'
        gmake: *** [Makefile:127: cmTC_40459/fast] Error 2
        
      exitCode: 2

And adding CMAKE_REQUIRED_LIBRARIES to give the required library:

set (CMAKE_REQUIRED_LIBRARIES pci)
CHECK_SYMBOL_EXISTS ("pci_get_string_property" pci/pci.h HAVE_PCI_GET_STRING_PROPERTY)
if (${HAVE_PCI_GET_STRING_PROPERTY})
    add_definitions(-DHAVE_PCI_GET_STRING_PROPERTY)
endif()

Then allowed pci_get_string_property to be found:

-- Looking for pci_get_string_property
-- Looking for pci_get_string_property - found

The add_definitions is required to allow the code to conditionally compile calls to pci_get_string_property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment