[FIXED] Android C++/Gradle/NDK: what flags should I pass to cppFlags, cFlags or arguments?

Issue

I see here a typical example of Gradle file for a NDK project.

Nevertheless, I wonder what are the rules to know where to put the different flags:

    // Passes optional arguments to CMake.
       arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang"
    
    // Sets a flag to enable format macro constants for the C compiler.
       cFlags "-D__STDC_FORMAT_MACROS"
    
   // Sets optional flags for the C++ compiler.
      cppFlags "-fexceptions", "-frtti"

For example, why "-fexceptions", "-frtti" are in ‘cppFlags’ and not in ‘arguments’ ? What are the rules to decide and the influence on compilation/app runtime/performance?

Thanks.

Solution

The snippet you linked to clearly indicates the audience for each setting:

  1. When Gradle needs to build the native component of your application, it calls CMake for every ABI in scope. Thus, arguments are for the CMake invocation.
  2. CMake itself does not actually build your native component, it just sets up a set of files for your build system. Typically this tool is Ninja or Make.
  3. The cFlags and cppFlags are embedded in the build rules for the respective files. Files with a .c extension are built with the C compiler so they get the cFlags, and likewise cppFlags for .cpp or .cc. The exact mechanism how these flags get to the respective compilers is not very interesting so I will skip that.

To answer your last question: you need to inspect your CMakeLists.txt to see which properties it looks for and interprets. There is also a whole slew of CMake built-in variables that can be set from the commandline.

You can consult the Clang user’s guide and the Clang commandline reference. Often you can figure out from the description whether it is a C or a C++ flag.

Answered By – Botje

Answer Checked By – Timothy Miller (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published