Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8057

SDK • Re: [RESOLVED] project can't find my library source files in subdirectory (cmake issues?)

$
0
0
Recently I had a new problem with this where the object files were compiled, but the linker could not link them to create the elf when I ran "make."

I also have updated my CMake to version 3.24.1, which may or may not have caused the issue. What I found is when adding an interface library, target_sources needs to be used instead of listing the sources inside add_library as shown in the CMake tutorial here: https://cmake.org/cmake/help/latest/gui ... -a-library This absolutely does not work for an INTERFACE library. Again, a big thanks to Davey Thacher for the RP2040_SKELETON, I couldn't have done it without this. https://github.com/daveythacher/RP2040_SKELETON

Here are my working CMakeLists.txt files, and directory listing. I put the library source and headers into ./src to KISS and YAGNI:

Code:

breaker@ace:~/pico/tmp117$ ls -R.:CMakeLists.txt  cm.sh*  examples/  pico_sdk_import.cmake  src/  tmp117_examples.txt./examples:1_temp_result/  2_alerts/  3_set_offset/  4_set_conversion_mode/  CMakeLists.txt./examples/1_temp_result:CMakeLists.txt  temp_result.c./examples/2_alerts:CMakeLists.txt  alerts.c./examples/3_set_offset:CMakeLists.txt  set_offset.c./examples/4_set_conversion_mode:CMakeLists.txt  set_conversion_mode.c./src:CMakeLists.txt  tmp117.c  tmp117.h  tmp117_registers.h
./CMakeLists.txt (top level)

Code:

# Top level CMakeLists.txt in $HOME/pico/tmp117# Minimum required CMake versioncmake_minimum_required(VERSION 3.13)# Pull in SDK (must be before project)include(pico_sdk_import.cmake)project(tmp117_examples C CXX ASM)set(CMAKE_C_STANDARD 11)set(CMAKE_CXX_STANDARD 17)if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")endif()# Initialize the SDKpico_sdk_init()add_compile_options(        -Wall                # Enable most warning messages        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int        -Wno-unused-function # we have some for the docs that aren't called        )if (CMAKE_C_COMPILER_ID STREQUAL "GNU")   add_compile_options(-Wno-maybe-uninitialized)endif()# Add the library source and headers directory to the buildadd_subdirectory(src)# Add the examples directory to the buildadd_subdirectory(examples)cmake_policy(GET CMP0079 policy_value)if(policy_value STREQUAL "NEW")    message(STATUS "CMake policy CMP0079 is set to NEW.")else()    message(STATUS "CMake policy CMP0079 is not set to NEW.")endif()message(STATUS "END tmp117/CMakeLists.txt")
./src CMakeLists.txt where the INTERFACE library is added.
A relative path is necessary, I used the variable ${CMAKE_CURRENT_SOURCE_DIR} as part of that path.

Code:

# CMakeLists.txt in the tmp117/src directory# Define an INTERFACE library named 'tmp117_functions'add_library(tmp117_functions INTERFACE)# Add the source file tmp117.c to the tmp117_functions interfacetarget_sources(tmp117_functions INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/tmp117.c)# Add the current source directory to the include path for tmp117_functionstarget_include_directories(tmp117_functions INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})# Link necessary libraries to the tmp117_functions interfacetarget_link_libraries(tmp117_functions INTERFACE    pico_stdlib    hardware_i2c    pico_printf)message(STATUS "END tmp117/src/CMakeLists.txt")
I now have the examples directories organized under an examples subdirectory, but this isn't necessary:

Code:

# Add example subdirectoriesadd_subdirectory(1_temp_result)add_subdirectory(2_alerts)add_subdirectory(3_set_offset)add_subdirectory(4_set_conversion_mode)message(STATUS "END tmp117/examples/CMakeLists.txt")
Finally here is a CMakeLists.txt for my first example program.
${PROJECT_SOURCE_DIR}/src was used for the path for the target_include_directories otherwise, it can't be located.

Code:

# Example 1 (tmp117/examples/1_temp_result/CMakeLists.txt)add_executable(temp_result temp_result.c)# pull in common dependencies and additional i2c hardware supporttarget_link_libraries(temp_result PRIVATE tmp117_functions pico_stdlib hardware_i2c pico_printf)# add include pathstarget_include_directories(temp_result PRIVATE ${PROJECT_SOURCE_DIR}/src)# enable usb output, disable uart outputpico_enable_stdio_usb(temp_result 1)pico_enable_stdio_uart(temp_result 0)# create map/bin/hex file etc.pico_add_extra_outputs(temp_result)# add url via pico_set_program_url# example_auto_set_url(temp_result)message(STATUS "END tmp117/examples/1_temp_result/CMakeLists.txt")
I guess I'm finally starting to wrap my mind around CMake!

Statistics: Posted by breaker — Fri Mar 29, 2024 5:42 pm



Viewing all articles
Browse latest Browse all 8057

Trending Articles