1

I have been learning to write drivers for a Pi Pico and I implemented my own GPIO library.

But I am unable to get it to build as the linker is not able to find the library (-lcustom_gpio).

This is how my project directory looks.

.
└── LED_Blink/
    ├── build
    ├── gpio/
    │   ├── CMakeLists.txt
    │   ├── gpio.cpp
    │   └── gpio.hpp
    ├── CMakeLists.txt
    ├── main.cpp
    ├── main.hpp
    └── pico_sdk_import.cmake

CmakeLists.txt under gpio:

set(gpio_files gpio.cpp gpio.hpp)

add_library(custom_gpio ${gpio_files})

CMakeLists.txt under root:

cmake_minimum_required(VERSION 3.13)
include(pico_sdk_import.cmake)
project(test_project C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
pico_sdk_init()

set(SOURCE_FILES main.cpp)



add_executable(test
${SOURCE_FILES}
)
target_link_libraries(test custom_gpio)
target_include_directories(test ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/gpio)
pico_add_extra_outputs(test custom_gpio)

Output of 'cmake ..':

PICO_SDK_PATH is /home/arun/pico/pico-sdk
PICO platform is rp2040.
Build type is Release
PICO target board is pico.
Using board configuration from /home/arun/pico/pico-sdk/src/boards/include/boards/pico.h
TinyUSB available at /home/arun/pico/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040; enabling build support for USB.
BTstack available at /home/arun/pico/pico-sdk/lib/btstack
cyw43-driver available at /home/arun/pico/pico-sdk/lib/cyw43-driver
Pico W Bluetooth build support available.
lwIP available at /home/arun/pico/pico-sdk/lib/lwip
mbedtls available at /home/arun/pico/pico-sdk/lib/mbedtls
-- Configuring done
-- Generating done
-- Build files have been written to: /home/arun/pico/LED_Blink/build

output of 'make -j4':

Scanning dependencies of target bs2_default
[  5%] Performing build step for 'ELF2UF2Build'
[ 10%] Performing build step for 'PioasmBuild'
[ 20%] Built target bs2_default
[100%] Built target elf2uf2
[100%] Built target pioasm
[ 25%] No install step for 'ELF2UF2Build'
[ 30%] No install step for 'PioasmBuild'
[ 35%] Completed 'ELF2UF2Build'
[ 40%] Completed 'PioasmBuild'
[ 65%] Built target ELF2UF2Build
[ 90%] Built target PioasmBuild
Consolidate compiler generated dependencies of target test
[ 95%] Linking CXX executable test.elf
/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: cannot find -lcustom_gpio: No such file or directory
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/test.dir/build.make:97: test.elf] Error 1
make[1]: *** [CMakeFiles/Makefile2:1490: CMakeFiles/test.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

I have been referring to related StackOverflow threads and CMake documentation, but I am stuck getting this project to build.

What am I doing wrong?

Marc Dirven
  • 309
  • 2
  • 18
  • Root doesn't seem to include the gpio subfolder? – Alan Birtles Apr 21 '23 at 06:37
  • By Root I mean 'LED_Blink' – ArunVijay0718 Apr 21 '23 at 06:41
  • Yep, I understood that, I meant that your root cmake file isn't including the gpio subfolder – Alan Birtles Apr 21 '23 at 06:43
  • include_directories(${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/gpio) Does this line not include the gpio subfolder? – ArunVijay0718 Apr 21 '23 at 06:48
  • It adds it to the compiler include paths, it doesn't add the cmake files in that directory, see https://cmake.org/cmake/help/latest/command/add_subdirectory.html – Alan Birtles Apr 21 '23 at 06:56
  • Thank you for the information, I did not know that. However I face another linker error: undefined reference to function_name(). I had included this line in the root makefile: add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/gpio) – ArunVijay0718 Apr 21 '23 at 07:13
  • You also need [target_link_directories](https://cmake.org/cmake/help/latest/command/target_link_directories.html). – Ian Abbott Apr 21 '23 at 09:17
  • @IanAbbott not for linking to a target, cmake should do it all for you – Alan Birtles Apr 21 '23 at 09:44
  • BTW, `test` is a reserved target name in some versions of CMake. Even if it causes no problems now, it would be better to avoid calling a target `test`. And `target_include_directories` has a mandatory scope argument, e.g. `PUBLIC`. – Friedrich Apr 25 '23 at 12:59

0 Answers0