<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How to pass variables to west</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/108933/how-to-pass-variables-to-west</link><description>I want to pass a variable to my program so I&amp;#39;m using the command: west build -b nrf52840dk_nrf52840 -p --build-dir build -- -Dmy_variable=40 Then in my program I want to print it: But the result is &amp;quot;my_variable is not defined&amp;quot;. What am I doing wrong?</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 02 Apr 2026 00:42:55 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/108933/how-to-pass-variables-to-west" /><item><title>RE: How to pass variables to west</title><link>https://devzone.nordicsemi.com/thread/564403?ContentTypeID=1</link><pubDate>Thu, 02 Apr 2026 00:42:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6383cd26-90aa-4e05-a151-25a527e63df4</guid><dc:creator>Monkeytronics</dc:creator><description>&lt;p&gt;So what exactly does:&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;west build -b nrf52840dk_nrf52840 -p --build-dir build -- -Dmy_variable=40&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;do? I thought it allowed you to use {&lt;span&gt;y_variable} in CMakeLists.txt, but that does not seem to be the case. I want to configure how .conf files are added based on a custom variable &amp;quot;MONKEY_BUILD&amp;quot;, but it is completely ignored in cmake. I&amp;#39;ve tried adding it as additional cmake in build config. Then tried this to simplify it down:&lt;br /&gt;&lt;br /&gt;west build -p auto -- -DMONKEY_BUILD=dev&lt;br /&gt;&lt;br /&gt;however, this outputs an empty string:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;message&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;STATUS&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;&amp;quot;MONKEY_BUILD = &lt;/span&gt;&lt;span&gt;${&lt;/span&gt;&lt;span&gt;MONKEY_BUILD&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;Update&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;----------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;span&gt;I thought I&amp;#39;d waste some more time on this and try out random stuff to try and learn more. Turns out I can only use existing CMAKE defined variables. For example:&amp;nbsp;-DCMAKE_BUILD_TYPE=prod. And interestingly, I can set these to custom values. For example this is fine -&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;if&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;CMAKE_BUILD_TYPE&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;STREQUAL&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;&amp;quot;dev&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span&gt;list&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;APPEND&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;OVERLAY_CONFIG&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;${&lt;/span&gt;&lt;span&gt;CMAKE_SOURCE_DIR&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;/../0-monkey/config/build/dev.conf&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;endif&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;- which allows me to cleanly create a configuration geared towards unit testing. Since it takes nearly 10 minutes to do a pristine build with stage 1 &amp;amp; 2 bootloaders and all the other stuff in my main app, this is essential.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;span&gt;&lt;br /&gt;I may be misreading the documentation on this, but it looks very much like it is wrong. You can&amp;#39;t just use a custom my_varaible and expect to use it to control the execution of your cmake code. I can crack on with my work now, but this has cost me time and left me feeling irritated.&amp;nbsp;There are so many issues like this that cost us developers time and cause frustration. It would be great if you could fix / document them better.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to pass variables to west</title><link>https://devzone.nordicsemi.com/thread/472280?ContentTypeID=1</link><pubDate>Tue, 05 Mar 2024 15:26:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6fb98fef-efe5-4ba3-935a-618f68a5f797</guid><dc:creator>ephimee</dc:creator><description>&lt;p&gt;I solved it by adding CONFIG_ in front of my variable in main.c&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to pass variables to west</title><link>https://devzone.nordicsemi.com/thread/472246?ContentTypeID=1</link><pubDate>Tue, 05 Mar 2024 14:00:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:21021904-6a65-4d74-8d10-4df9f7b9fe1b</guid><dc:creator>Sigurd Hellesvik</dc:creator><description>&lt;p&gt;Have a look at our samples. They use a file named simply &amp;quot;Kconfig&amp;quot;. Does that work for you?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to pass variables to west</title><link>https://devzone.nordicsemi.com/thread/472207?ContentTypeID=1</link><pubDate>Tue, 05 Mar 2024 10:27:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:53d5721c-6579-4368-b3dd-e74fb9a14bb4</guid><dc:creator>ephimee</dc:creator><description>&lt;p&gt;I&amp;#39;m not sure if Kconfig.application is the right naming for Kconfig files.&lt;br /&gt;So I renamed it to Kconfig.defconfig.&lt;br /&gt;Then I also added in CMakeLists.txt the following:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;set(ZEPHYR_EXTRA_KCONFIG_FILES ${CMAKE_CURRENT_SOURCE_DIR}/Kconfig.defconfig)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;When I print the variable in CMakeLists.txt, it gets the correct value:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;message(&amp;quot;MY VARIABLE is: ${MY_VARIABLE}&amp;quot;)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;But in main.c MY_VARIABLE is still undefined.&lt;/p&gt;
&lt;p&gt;This is my Kconfig.defconfig:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;config MY_VARIABLE
string &amp;quot;my variable explain bla bla&amp;quot;
default &amp;quot;default my variable&amp;quot;
help
This is an example for how to set a custom variable for your application&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to pass variables to west</title><link>https://devzone.nordicsemi.com/thread/472195?ContentTypeID=1</link><pubDate>Tue, 05 Mar 2024 09:28:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3fdef82a-7dc6-4644-b5b5-69212803d9f6</guid><dc:creator>ephimee</dc:creator><description>&lt;p&gt;I tried it, but I could not get it to work.&lt;/p&gt;
&lt;p&gt;I created the file Kconfig.application in my project folder. When I compile I still get the cmake warning:&lt;/p&gt;
&lt;p&gt;CMake Warning:&lt;br /&gt; Manually-specified variables were not used by the project:&lt;/p&gt;
&lt;p&gt;MY_VARIABLE&lt;/p&gt;
&lt;p&gt;How does west find the Kconfig file? Do I need to add something to CMakeLists.txt? Do you have any references for documentation how to do this?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to pass variables to west</title><link>https://devzone.nordicsemi.com/thread/472187?ContentTypeID=1</link><pubDate>Tue, 05 Mar 2024 09:02:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a64a10e0-e898-4dcb-be30-dd351d1374b3</guid><dc:creator>Sigurd Hellesvik</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The slightly more complex but more in the &amp;quot;Zephyr spirit&amp;quot;, method would be to add a Kconfig file to your project defining it:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;config MY_VARIABLE
int &amp;quot;My variable explain explain explain&amp;quot;
range 10 100
help
    This is an example for how to set a custom variable for your application

menu &amp;quot;Zephyr Kernel&amp;quot;
source &amp;quot;Kconfig.zephyr&amp;quot;
endmenu
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And then use -DCONFIG_MY_VARIABLE.&lt;/p&gt;
&lt;p&gt;This will let you define it in prj.conf if you want as well.&lt;br /&gt;And you can take advantages of features of Kconfig, such as the range attribue I show in my example. (That can also be removed, it is optional).&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Sigurd Hellesvik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>