3

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?

parsley72
  • 8,449
  • 8
  • 65
  • 98
AntoineC
  • 111
  • 1
  • 2
  • 10
  • Possible duplicate of [Define preprocessor macro through CMake?](https://stackoverflow.com/q/9017573/608639) – jww Jul 22 '19 at 17:13
  • 2
    @jww: The question you suggest as a duplicate is about "internal" adding the flags, within project's `CMakeLists.txt`. Current question is about "external" adding the flags, specifically in Yocto - quite a different thing. – Tsyvarev Jul 22 '19 at 22:44
  • @Tsyvarev - Yeah, I thought that too. Then I reviewed the accepted answer. – jww Jul 23 '19 at 01:03
  • The [accepted answer](https://stackoverflow.com/a/42578353/3440745) tells mostly about Yocto way ("external") of setting preprocessor macros. Only its last paragraph notes about `CMakeLists.txt` ("internal") way. But this note is crucial for the external way to work: If the project **rewrites** compiler flags (`CMAKE_CXX_FLAGS`), then all "external" settings will be lost. So the correct way for the project is **appending** flags instead. – Tsyvarev Jul 23 '19 at 08:28

1 Answers1

9

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.

Tomas Novotny
  • 1,771
  • 9
  • 11