I’m new to Cmake, and am a bit beaten by it.
In my main project I’m trying to fetchcontent a git Repo and add it to my main project as a lib.
The problem I have is that everything builds fine. however when I call the function int GetVersion(){return 2;} from the LIB, it simply returns 0.
I build a .elf for an STM32 Microcontroller using STM32CUBEIDE.
I already made a project without IDE and made .exe and… it works as expected it prints 3…
There are no errors or warning other that a = unused. But the optimizer is turned of and i can step through the code.
Since everything builds fine, I have a feeling it is not linking well.
Am I missing anything, what can I do to debug this further?
a = GetVersion(); // after this call a = 0
<code>//Main.c
#include <stdint.h>
#include "ringbuff.h"
int main(void)
{
static int a = 1;
a = GetVersion(); // after this call a = 0
for(;;);
}
</code>
//Main.c
#include <stdint.h>
#include "ringbuff.h"
int main(void)
{
static int a = 1;
a = GetVersion(); // after this call a = 0
for(;;);
}
<code>//ringbuff.c from the lib. The header contains: 'int GetVersion2();'
<code>//ringbuff.c from the lib. The header contains: 'int GetVersion2();'
#include "ringbuff.h"
int GetVersion()
{
return 3;
}
</code>
//ringbuff.c from the lib. The header contains: 'int GetVersion2();'
#include "ringbuff.h"
int GetVersion()
{
return 3;
}
cMakelists of Main project. Everything is standard cmaketemplate from the IDE, I’ve added the part inbetween the Import Lib comments
<code>cmake_minimum_required(VERSION 3.20)
Startup/startup_stm32h755zitx.s
Sources/system_stm32h7xx_dualcore_boot_cm4_cm7.c
# LIST COMPILER DEFINITIONS HERE
# LIST INCLUDE DIRECTORIES HERE
Drivers/STM32H7xx_HAL_Driver/Inc
Drivers/STM32H7xx_HAL_Driver/Inc/Legacy
Drivers/CMSIS/Device/ST/STM32H7xx/Include
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${CMAKE_PROJECT_NAME}>)
################# Import module #####################
Set(FETCHCONTENT_QUIET FALSE)
GIT_REPOSITORY https://github.com/TESTMODULE.git
FetchContent_MakeAvailable(ringbuff)
target_include_directories(${PROJECT_NAME}
PUBLIC ${ringbuff_SOURCE_DIR}
target_link_libraries(${PROJECT_NAME}
#####################################################
target_compile_definitions(${PROJECT_NAME}
PRIVATE ${PROJECT_DEFINES}
target_include_directories(${PROJECT_NAME}
PUBLIC ${PROJECT_INCLUDES}
set (CMAKE_EXECUTABLE_SUFFIX ".elf")
set (CMAKE_STATIC_LIBRARY_SUFFIX ".a")
set (CMAKE_C_FLAGS "-mcpu=Cortex-M7 -O0 -std=gnu11 -mfpu=fpv5-d16 --specs=nano.specs -mthumb -Wall")
set (CMAKE_EXE_LINKER_FLAGS "-T../STM32H755ZITX_FLASH.ld --specs=nosys.specs -Wl,-Map=test.map -Wl,--gc-sections -static -Wl,--start-group -lc -lm -Wl,--end-group")
set (CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp")
<code>cmake_minimum_required(VERSION 3.20)
set (PROJECT_NAME "CM7")
# LIST SOURCE FILES HERE
set (PROJECT_SOURCES
Startup/startup_stm32h755zitx.s
Sources/system_stm32h7xx_dualcore_boot_cm4_cm7.c
Sources/main.c
Sources/syscalls.c
Sources/sysmem.c
)
# LIST COMPILER DEFINITIONS HERE
set (PROJECT_DEFINES
CORE_CM7
STM32H755xx
)
# LIST INCLUDE DIRECTORIES HERE
set (PROJECT_INCLUDES
Sources
#STM32 HAL & CMSIS
Drivers/STM32H7xx_HAL_Driver/Inc
Drivers/STM32H7xx_HAL_Driver/Inc/Legacy
Drivers/CMSIS/Device/ST/STM32H7xx/Include
Drivers/CMSIS/Include
)
project(${PROJECT_NAME})
enable_language(ASM)
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${CMAKE_PROJECT_NAME}>)
################# Import module #####################
include(FetchContent)
Set(FETCHCONTENT_QUIET FALSE)
FetchContent_Declare(
ringbuff
GIT_REPOSITORY https://github.com/TESTMODULE.git
GIT_TAG V0.7
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(ringbuff)
target_include_directories(${PROJECT_NAME}
PUBLIC ${ringbuff_SOURCE_DIR}
)
target_link_libraries(${PROJECT_NAME}
PRIVATE ringbuff
)
#####################################################
target_compile_definitions(${PROJECT_NAME}
PRIVATE ${PROJECT_DEFINES}
)
target_include_directories(${PROJECT_NAME}
PUBLIC ${PROJECT_INCLUDES}
)
set (CMAKE_EXECUTABLE_SUFFIX ".elf")
set (CMAKE_STATIC_LIBRARY_SUFFIX ".a")
set (CMAKE_C_FLAGS "-mcpu=Cortex-M7 -O0 -std=gnu11 -mfpu=fpv5-d16 --specs=nano.specs -mthumb -Wall")
set (CMAKE_EXE_LINKER_FLAGS "-T../STM32H755ZITX_FLASH.ld --specs=nosys.specs -Wl,-Map=test.map -Wl,--gc-sections -static -Wl,--start-group -lc -lm -Wl,--end-group")
set (CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp")
</code>
cmake_minimum_required(VERSION 3.20)
set (PROJECT_NAME "CM7")
# LIST SOURCE FILES HERE
set (PROJECT_SOURCES
Startup/startup_stm32h755zitx.s
Sources/system_stm32h7xx_dualcore_boot_cm4_cm7.c
Sources/main.c
Sources/syscalls.c
Sources/sysmem.c
)
# LIST COMPILER DEFINITIONS HERE
set (PROJECT_DEFINES
CORE_CM7
STM32H755xx
)
# LIST INCLUDE DIRECTORIES HERE
set (PROJECT_INCLUDES
Sources
#STM32 HAL & CMSIS
Drivers/STM32H7xx_HAL_Driver/Inc
Drivers/STM32H7xx_HAL_Driver/Inc/Legacy
Drivers/CMSIS/Device/ST/STM32H7xx/Include
Drivers/CMSIS/Include
)
project(${PROJECT_NAME})
enable_language(ASM)
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${CMAKE_PROJECT_NAME}>)
################# Import module #####################
include(FetchContent)
Set(FETCHCONTENT_QUIET FALSE)
FetchContent_Declare(
ringbuff
GIT_REPOSITORY https://github.com/TESTMODULE.git
GIT_TAG V0.7
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(ringbuff)
target_include_directories(${PROJECT_NAME}
PUBLIC ${ringbuff_SOURCE_DIR}
)
target_link_libraries(${PROJECT_NAME}
PRIVATE ringbuff
)
#####################################################
target_compile_definitions(${PROJECT_NAME}
PRIVATE ${PROJECT_DEFINES}
)
target_include_directories(${PROJECT_NAME}
PUBLIC ${PROJECT_INCLUDES}
)
set (CMAKE_EXECUTABLE_SUFFIX ".elf")
set (CMAKE_STATIC_LIBRARY_SUFFIX ".a")
set (CMAKE_C_FLAGS "-mcpu=Cortex-M7 -O0 -std=gnu11 -mfpu=fpv5-d16 --specs=nano.specs -mthumb -Wall")
set (CMAKE_EXE_LINKER_FLAGS "-T../STM32H755ZITX_FLASH.ld --specs=nosys.specs -Wl,-Map=test.map -Wl,--gc-sections -static -Wl,--start-group -lc -lm -Wl,--end-group")
set (CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp")
Cmakelist of the ringbuff lib
<code># Minimum version of CMake required
cmake_minimum_required(VERSION 3.20)
add_library(ringbuff STATIC ringbuff.c)
<code># Minimum version of CMake required
cmake_minimum_required(VERSION 3.20)
# Project name
project(RingBuffer C)
# Add the source files
add_library(ringbuff STATIC ringbuff.c)
</code>
# Minimum version of CMake required
cmake_minimum_required(VERSION 3.20)
# Project name
project(RingBuffer C)
# Add the source files
add_library(ringbuff STATIC ringbuff.c)