How I can add a cflag in cmake Android Studio?

2019-06-02 04:09发布

问题:

How I can add -D_FILE_OFFSET_BITS=64 in Cmake config file. I'm trying to add as cflag in build.gradle but it not work.

    externalNativeBuild {
        cmake {
            cppFlags ""
            cFlags "-D_FILE_OFFSET_BITS=64"
            arguments "-DANDROID_TOOLCHAIN=gcc"
        }
    }

回答1:

In the CMakeLists.txt file, I do something like:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__ANDROID__ -DANDROID -DCUSTOM_FLAG")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DGL_GLEXT_PROTOTYPES=1 -DIOAPI_NO_64 -DUSE_FILE32API ")

However, you can use gradle too, but externalNativeBuild needs to be under defaultConfig or release or debug (a build flavor) for cmake options to pass the flags.

For Example:

    android {
        compileSdkVersion 25
        buildToolsVersion '25.0.3'
        defaultConfig {
            applicationId "..."
 
            externalNativeBuild {
                cmake {
                    // here, arguments, cppFlags, cFlags, ..  all work
                    arguments '-DANDROID_PLATFORM=android-15',
                            '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=gnustl_static',
                            '-DANDROID_CPP_FEATURES=rtti exceptions'
                }
            }
        }
        externalNativeBuild {
            cmake {
                // only 'path' variable is valid here
                path '../../gameSource/CMakeLists.txt'
            }
        }
        buildTypes {
            release {
               // .. release flavor
            }
            debug {
                //... debug flavor
            }
        }
    }