Page 1 of 1

Setting CPPFLAGS with CMake and

Posted: Sun Apr 28, 2019 11:45 am
I've included an external library (ArduinoWebSockets) as a component by including it as a git submodule.
The component define various macros depending upon the value of a compiler option that is normally set via CPPFLAGS.

This is building fine using make under MSYS32, but want to transition to CMake so I can use CLion as IDE.

Have read ... ompilation

But after trying many different combos, while I can convince the IDE that the compiler option has been set, building on the command line with " build" doesn't pass the option to the compiler.

The compiler option is "ESP32", I have tried all of the below in the CMakeLists.txt for both component and project:

Code: Select all

# Trying to configure CPPFLAGS with -DESP32 so that ArduinoWebSocket uses the correct macros.
set_source_files_properties(src/WebSockets.h PROPERTIES COMPILE_FLAGS -DESP32)

target_compile_definitions(arduinoWebsockets PRIVATE "-DESP32")

How do I set CPPFLAGS for a component with and CMake?

Re: Setting CPPFLAGS with CMake and

Posted: Mon Apr 29, 2019 12:00 am
by ESP_Angus
You should be able to set this in the component CMakeLists.txt file.

I recommend using target_compile_definitions(${COMPONENT_TARGET} PRIVATE "-DESP32") if definition only needs to be set when building the source files in the component, not when including the headers. Or use target_compile_definitions(${COMPONENT_TARGET} PUBLIC "-DESP32") if this macro also needs to be defined when including the public headers, while compiling source files in another component.

If it's not working in the component CMakeLists.txt, it's probably due to the order that the statements are being placed in the CMakeLists.txt file. Any target___() lines have to be placed after the register_component() line in the CMakeLists.txt file.

It's recommended to use ${COMPONENT_TARGET} variable rather than guessing the component library name, which may change in the future.

If this is still not working, please post the full component CMakeLists.txt contents and we can take a look.

Re: Setting CPPFLAGS with CMake and

Posted: Mon Aug 12, 2019 8:41 pm
Angus, I still can't seem to get this to work.

This is the CMakeLists.txt for the component:

Code: Select all


SET (COMPONENT_ADD_INCLUDEDIRS src src/libb64 src/libsha1)
#        "src/SocketIOclient.cpp"


# To pass in the ESP32 CPPFlag
target_compile_definitions(${COMPONENT_TARGET} PRIVATE "-DESP32")
NB this is the component

If I build with the above, then
#elif defined(ESP32)
on line 99 of WebSockets.h evaluates to false and it fails trying to include "Ethernet.h"

If I add
#define ESP32
to the top of WebSockets.h then it builds OK, but warns about ESP32 being redefined several times.

Code: Select all

[5/12] Building CXX object esp-idf/arduinoWebsockets/CMakeFiles/idf_component_arduinoWebsockets.dir/src/WebSocketsClient.cpp.obj
In file included from ../components/arduinoWebsockets/src/WebSocketsClient.cpp:25:0:
../components/arduinoWebsockets/src/WebSockets.h:30:0: warning: "ESP32" redefined
 #define ESP32
<command-line>:0:0: note: this is the location of the previous definition

Re: Setting CPPFLAGS with CMake and

Posted: Mon Aug 12, 2019 8:44 pm
Scratch that - clearly need some sleep.

As you indicated above - this works.

Code: Select all

# To pass in the ESP32 CPPFlag
target_compile_definitions(${COMPONENT_TARGET} PUBLIC "-DESP32")