[Solved] Adding a define in component.mk

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

[Solved] Adding a define in component.mk

Postby kolban » Mon Nov 07, 2016 5:34 am

I am using the ESP-IDF framework. I have cloned a copy of the ESP-IDF template app. When I compile the main.c program found in subdirectory main, I need to include an environmentally injected macro definition.

As such, I edited main/component.mk such that the final file reads:

Code: Select all

CFLAGS:=-DARDUINO=1
include $(IDF_PATH)/make/component_common.mk
My desire is to add a macro definition of "ARDUINO".

When I ran the compilation, it appeared that the setting was ignored so I set "export V=1" and re-ran the build and in my results, indeed I do NOT see the definition being added. Am I mis-understanding the docs found here:

http://esp-idf.readthedocs.io/en/latest ... ystem.html

On setting "CFLAGS" to include additional compilation values?
Last edited by kolban on Mon Nov 07, 2016 1:41 pm, edited 1 time in total.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: Adding a define in component.mk

Postby ESP_igrr » Mon Nov 07, 2016 6:45 am

I'm not able to reproduce the issue here... I have cloned esp-idf-template and added "CFLAGS := -DFOO=1" line above "include" line in main/component.mk.

Here's a part of build log:

Code: Select all

xtensa-esp32-elf-gcc -DFOO=1 -DESP_PLATFORM  -I /Volumes/ESPTools/esp-idf-template/main/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/bt/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/driver/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/esp32/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/expat/port/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/expat/include/expat -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/freertos/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/json/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/json/port/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/log/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/lwip/include/lwip -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/lwip/include/lwip/port -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/lwip/include/lwip/posix -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/mbedtls/port/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/mbedtls/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/newlib/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/newlib/platform_include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/nghttp/port/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/nghttp/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/nvs_flash/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/openssl/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/spi_flash/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/tcpip_adapter/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/vfs/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/xtensa-debug-module/include -I /Volumes/ESPTools/esp-idf-template/build/include  -I. -c /Volumes/ESPTools/esp-idf-template/main/./main.c -o main.o
Note that -DFOO=1 was added, but all other flags which came from the build system were removed. So it's better to add "CFLAGS += -DFOO=1" instead.

Code: Select all

xtensa-esp32-elf-gcc -std=gnu99 -Og -ggdb -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -mlongcalls -nostdlib -MMD -MP -Wall -Werror -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -DMBEDTLS_CONFIG_FILE='"mbedtls/esp_config.h"' -DHAVE_CONFIG_H -DFOO=1 -DESP_PLATFORM  -I /Volumes/ESPTools/esp-idf-template/main/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/bt/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/driver/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/esp32/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/expat/port/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/expat/include/expat -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/freertos/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/json/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/json/port/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/log/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/lwip/include/lwip -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/lwip/include/lwip/port -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/lwip/include/lwip/posix -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/mbedtls/port/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/mbedtls/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/newlib/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/newlib/platform_include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/nghttp/port/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/nghttp/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/nvs_flash/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/openssl/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/spi_flash/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/tcpip_adapter/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/vfs/include -I /Users/igrokhotkov/e/ESPTools/esp32-sdkng/components/xtensa-debug-module/include -I /Volumes/ESPTools/esp-idf-template/build/include  -I. -c /Volumes/ESPTools/esp-idf-template/main/./main.c -o main.o
Now "-DFOO=1" is present after all other defines which came from the build system.

Note that the build system may not detect the change to makefile to rebuild all .c files... So you may need to run "make main-clean main-build" to rebuild the component to see extra CFLAGS taking effect.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Adding a define in component.mk

Postby kolban » Mon Nov 07, 2016 1:41 pm

Thank you sir. With a fresh morning mind and examining in detail your response ... it dawned on me that the distinction between your solution and mine was that I was compiling a C++ program and not a C program (I think I may have said a C program in my post and apologies for that). Reading on Makefiles, I then found that the equivalent CFLAGS makefile variable for C++ is CXXFLAGS. I set my CXXFLAGS to contain the value I wanted and all started working exactly as I had hoped.

We might want to consider adding the C++ variables into the hints section here ... http://esp-idf.readthedocs.io/en/latest ... ystem.html ... as an aid for newbies like me who are mixing C++, ESP-IDF, Make systems and ESP32.

Sorry again for wasting your time but MANY thanks for testing it out.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Who is online

Users browsing this forum: No registered users and 391 guests