jiandingzhe
jiandingzhe

Reputation: 2121

CMake Android cross-compiling get empty compiler flags for Debug and Release

To demonstrate this issue, I made a minimum CMakeLists.txt:

cmake_minimum_required(VERSION 3.12)
project(FlagsTest)

add_executable(myapp myapp.cpp)

I configure this cmake project with toolchain file inside Android NDK. Ninja generator is used, and cmake cached variable ANDROID_ABI is set to armeabi-v7a. The versions of software and toolchain are:

The whole stuff successfully configured and generated. However, cmake cache variables CMAKE_CXX_FLAGS_DEBUG, CMAKE_CXX_FLAGS_RELEASE, CMAKE_C_FLAGS_DEBUG and CMAKE_C_FLAGS_RELEASE are all blank. This caused in no debug symbol in debug build, and no optimization in release build, which bothered us for a lot.

At current, I made a workaround by manually specify these variables from cmake command line. But as these variables should be automatically set by cmake, I'm wondering whether it is a bug in cmake, or a bug in Android SDK.

Upvotes: 0

Views: 547

Answers (2)

jiandingzhe
jiandingzhe

Reputation: 2121

This is caused by incorrect configuration in the ndk toolchain file. It set empty initial value for CMAKE_CXX_FLAGS_DEBUG and CMAKE_CXX_FLAGS_RELEASE that prevents CMake using the default value:

set(CMAKE_C_FLAGS_DEBUG ""
  CACHE STRING "Flags used by the compiler during debug builds.")
set(CMAKE_CXX_FLAGS_DEBUG ""
  CACHE STRING "Flags used by the compiler during debug builds.")
set(CMAKE_C_FLAGS_RELEASE ""
  CACHE STRING "Flags used by the compiler during release builds.")
set(CMAKE_CXX_FLAGS_RELEASE ""
  CACHE STRING "Flags used by the compiler during release builds.")

As it's not good to modify this file directly, it seems the only way to solve it is to specify the value explicitly in command-line for every first generation.

Upvotes: 0

Alex Cohn
Alex Cohn

Reputation: 57203

CMake will take care of release and debug flags internally, you are expected to pass -DCMAKE_BUILD_TYPE=Release or -DCMAKE_BUILD_TYPE=Debug on command line.

Upvotes: -1

Related Questions