I'm working on a existent project that use CMake to configure and generate. The target is an ARM device, so I build all the system with Yocto/OpenEmbedded.
I manage to build a recipe to build the cmake project. It looks like that:
DESCRIPTION = "FANN LIB"
LICENSE = "CLOSED"
inherit cmake
SRCREV = "${AUTOREV}"
PV = "1.0"
SRC_URI = "git://github.com/libfann/fann.git;branch=master;protocol=git"
S = "${WORKDIR}/git"
BBCLASSEXTEND = "native"
FILES_${PN} += "/usr/lib/cmake/"
In the code there are "#ifdef DEBUG" that I would like to activate. So I would like to add DEBUG to the C/C++ Flags.
I found that I could use
EXTRA_OECMAKE += "CXXFLAGS='-DDEBUG'"
EXTRA_OECMAKE = "set(CMAKE_CXX_FLAGS "-DDEBUG")"
But both replace all CFlags and that is not what I want (plus it broke compilation!)
I just would like that -DDEBUG is added when calling the compiler! :-)
How can I add a preprocessor definition in a CMake based project in a Yocto recipe?
I'm using this assignment for a target build in my recipe files (also based on cmake):
# This flag is also propagated to CXXFLAGS
TARGET_CFLAGS += "-DSOME_FLAG"
I'm not sure why CFLAGS are propagated to CXXFLAGS, so you can also try TARGET_CXXFLAGS
variable in OpenEmbeedded. See Yocto mega manual.
Please note that this will add flags only for a target build (i.e. the result will be used only on target). I see that you are also building native variant, so BUILD_CXXFLAGS
might be helpful. There is also BUILDSDK_CXXFLAGS
for nativesdk variant (this is not your case). These variables are clearly described in the manual in the CXXFLAGS variable description.
By the way, one thing which is related to the CMake: If you use set(CMAKE_CXX_FLAGS "-DDEBUG")
in your CMakeLists.txt, you need to repeat CMAKE_CXX_FLAGS on the right side, i.e. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG")
, otherwise your flags would be overwritten. Please see this blog for more information.