ccmakeraspberry-pipigpio

Unsure what to do with the CMake file from pigpio


I am new to CMake and was hoping to use a pretty simple Raspberry Pi application as a learning experience. I have a few files in a simple directory structure, plus I'm using pigpio. My code compiles fine by itself, but when I use CMake to generate a makefile, the makefile cannot find the references in pigpio. pigpio includes a file, Findpigpio.cmake, in the util directory, so I tried including it in CMakeLists.txt, but to no avail.

File structure:

RPapp
├──inc/
|   ├──spi.h
|   └──gpio.h
├──src/
|   ├──spi.c
|   ├──gpio.c
|   └──main.c
├──pigpio/
|   ├──util/
|   |   ├──Findpigpio.cmake
|   |   └──[whatever else is in here]
|   └──[remainder of code cloned from github]
└──CMakeLists.txt

Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.0)
project(RPapp C)
include(pigpio/util/Findpigpio.cmake)
include_directories(inc)
set(CMAKE_C_STANDARD 11)
add_executable(RPapp ${PROJECT_SOURCE_DIR}/src/main.c ${PROJECT_SOURCE_DIR}/src/gpio.c ${PROJECT_SOURCE_DIR}/src/spi.c)

I mkdir _build, cd _build, and cmake .., which gives very promising output:

-- The C compiler identification is GNU 8.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Found pigpio: /usr/local/include
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/RPapp/_build

but then when I run make, the output shows that it cannot reference the functions from pigpio:

Scanning dependencies of target RPapp
[ 25%] Building C object CMakeFiles/RPapp.dir/src/main.c.o
[ 50%] Building C object CMakeFiles/RPapp.dir/src/gpio.c.o
[ 75%] Building C object CMakeFiles/RPapp.dir/src/spi.c.o
[100%] Linking C executable RPapp
/usr/bin/ld: CMakeFiles/RPapp.dir/src/main.c.o: in function `main':
main.c:(.text+0x38): undefined reference to `gpioTerminate'
/usr/bin/ld: CMakeFiles/RPapp.dir/src/main.c.o: in function `buff_rdy_handler':
main.c:(.text+0x88): undefined reference to `spiRead'
...(more of the same)

Solution

  • I don't actually know about pigpio specifically. But from a pure cmake perspective you need to pull in the pigpio CMakeLists.txt and add that library as a link dependency. The include(pigpio/util/Findpigpio.cmake) is unnecessary.

    cmake_minimum_required(VERSION 3.0.0)
    project(RPapp C)
    # removed  following line
    #include(pigpio/util/Findpigpio.cmake)
    include_directories(inc)
    set(CMAKE_C_STANDARD 11)
    add_executable(RPapp ${PROJECT_SOURCE_DIR}/src/main.c ${PROJECT_SOURCE_DIR}/src/gpio.c ${PROJECT_SOURCE_DIR}/src/spi.c)
    
    # added these lines
    add_subdirectory(pigpio)
    target_link_libraries(RPapp pigpio)