Skip to main content
connoisseur_de_mimi
Associate III
September 8, 2025
Solved

STM32CubeIDE: Linker Settings by Build spec

  • September 8, 2025
  • 4 replies
  • 461 views

I'm currently tinkering with the BLE_Ota example for the P-NUCLEO-WB55.Nucleo and created a debug build spec for it. The default linker script is 

/* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 28K 
RAM1 (xrw) : ORIGIN = 0x20000008, LENGTH = 0x2FFF8
RAM_SHARED (xrw) : ORIGIN = 0x20030000, LENGTH = 10K
}

but the image built with the debug build spec is larger, so the length of the flash area to use must be adjusted:

 

/* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 32K 
RAM1 (xrw) : ORIGIN = 0x20000008, LENGTH = 0x2FFF8
RAM_SHARED (xrw) : ORIGIN = 0x20030000, LENGTH = 10K
}

 

that made me wonder, is it possible to define constants in the build specs such that conditional statements (e.g using the DEFINED statement) inside the .ld file can be used? something like 

FLASH (rx) : ORIGIN = 0x08000000, LENGTH = DEFINED(DEBUG) ? 32K : 28K 
Best answer by connoisseur_de_mimi

this pointed me in the right direction. 

adding

-Wl,--defsym=DEBUG=1

 to the build spec under MCU GCC Linker -> MISC -> Other flags along with a check in the linker script 

FLASH (rx) : ORIGIN = 0x08000000, LENGTH = DEFINED(DEBUG) ? 32K : 28K

produces the expected result (-Wl is necessary to forward the --defsym statement to the linker)

4 replies

mƎALLEm
Technical Moderator
September 8, 2025

Hello,

Try this:

#if defined (DEBUG) 
 FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 32K dede
#else 
 FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 28K
#endif

 

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."
connoisseur_de_mimi
Associate III
September 8, 2025

this causes a warning by the linker: 

STM32WB55RGVX_FLASH.ld:45: warning: redeclaration of memory region `FLASH'

the linker seems to ignore lines starting with `#`, only the last FLASH (rx) statement in the file is actually used. 

Tesla DeLorean
Guru
September 9, 2025

Yes, I think they are comments and the Linker script doesn't support a pre-processor pass.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
TDK
Super User
September 8, 2025

The linker script is not interpreted by the C preprocessor so this can't work.

You can run it manually if you want, as a pre-build step. However, it isn't part of a compilation unit so any #defines within files may not be active. Project-level defines will work.

c - Can I use Preprocessor Directives in .ld file - Stack Overflow

"If you feel a post has answered your question, please click ""Accept as Solution""."
connoisseur_de_mimi
Associate III
September 8, 2025

I was thinking about passing a parameter to the linker (via the build spec settings in eclypse) to define a symbol that is then used in a DEFINED() statement. apparently flags can be passed to unset symbols but I have not found any to set symbols. 

connoisseur_de_mimi_0-1757346167935.png

can you elaborate on your proposed solution? have you tried it yourself?

mƎALLEm
Technical Moderator
September 8, 2025

May be another idea:

Try to add two linker files one for debug and another for release to your project and for each config : debug or release exclude from build the unused linker file. 

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."
mƎALLEm
Technical Moderator
September 8, 2025

@mƎALLEm wrote:

May be another idea:

Try to add two linker files one for debug and another for release to your project and for each config : debug or release exclude from build the unused linker file. 


Ok it seems that exclude from build could not be used for linker files!

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."
Pavel A.
Super User
September 9, 2025

Try the --defsym symbol=expression   command line option.

Also, you can put definitions of symbols (symbol=value ....) in a file and pass it to linker via -T option, before the main link script. The link command can have multiple -T options.

connoisseur_de_mimi
connoisseur_de_mimiAuthorBest answer
Associate III
September 9, 2025

this pointed me in the right direction. 

adding

-Wl,--defsym=DEBUG=1

 to the build spec under MCU GCC Linker -> MISC -> Other flags along with a check in the linker script 

FLASH (rx) : ORIGIN = 0x08000000, LENGTH = DEFINED(DEBUG) ? 32K : 28K

produces the expected result (-Wl is necessary to forward the --defsym statement to the linker)