nanoMODBUS icon indicating copy to clipboard operation
nanoMODBUS copied to clipboard

Compile/Linker options required for using static library and FetchContent

Open jhird-fraser opened this issue 5 months ago • 2 comments

This might be my build system being badly setup but it is crucial to specify the link options for nanoMODBUS when importing it into the project using CMake's FetchContent functionality.

The current readme states

FetchContent_Declare(
        nanomodbus
        GIT_REPOSITORY https://github.com/debevv/nanoMODBUS
        GIT_TAG master # or the version you want
        GIT_SHALLOW TRUE
)

FetchContent_MakeAvailable(nanomodbus)

#...

add_executable(your_program source_codes)
target_link_libraries(your_program nanomodbus)

Where in reality the following is needed (adapted from my project using atsamd21J18A) so that your program doesn't crash when deployed to the target. Without it you can build the library but any calls to it will result in runtime errors.

FetchContent_Declare(
        nanomodbus
        GIT_REPOSITORY https://github.com/debevv/nanoMODBUS
        GIT_TAG master # or the version you want
        GIT_SHALLOW TRUE
)

FetchContent_MakeAvailable(nanomodbus)

target_compile_options(nanomodbus PRIVATE
		${CPU_PARAMETERS}  # -mcpu=cortex-m0plus
	)

target_link_options(nanomodbus PRIVATE
	-T${MCU_LINKER_SCRIPT} #linker script for atsamd21J18A
	-L${MCU_LINKER_LIBRARY_PATH} #linker lib for atsamd21J18A
	${CPU_PARAMETERS} # -mcpu=cortex-m0plus
       #etc.     
)

target_link_libraries(${APP_EXEC} nanomodbus)

Might be obvious for those more familiar with CMake but thought I'd make the issue to highlight it.

jhird-fraser avatar Jul 31 '25 13:07 jhird-fraser

Perhaps the readme example could be expanded to

FetchContent_Declare(
        nanomodbus
        GIT_REPOSITORY https://github.com/debevv/nanoMODBUS
        GIT_TAG master # or the version you want
        GIT_SHALLOW TRUE
)

FetchContent_MakeAvailable(nanomodbus)

#...

# Ensure library is compiled and linked for your target MCU

target_compile_options(nanomodbus PRIVATE 
                                      # MCU compile options
                                      )
target_link_options(nanomodbus PRIVATE 
                                # MCU linker options
                                )

add_executable(your_program source_codes)
target_link_libraries(your_program nanomodbus)

jhird-fraser avatar Aug 04 '25 13:08 jhird-fraser

Thank you for opening this issue, you saved me some CMake debugging.

NicGrimpe avatar Sep 04 '25 20:09 NicGrimpe