5

I want to build small library which was written in C99 for Android, but compiler gave log as

note: use option -std=c99 or -std=gnu99 to compile your code

Where can I set it?

Rusfearuth
  • 3,261
  • 5
  • 28
  • 37

3 Answers3

12

In your Android.mk add

LOCAL_CFLAGS += -std=c99

For example:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_CFLAGS += -std=c99
LOCAL_SRC_FILES := com_example_ndktest_TestLib.c
LOCAL_MODULE := com_example_ndktest_TestLib
include $(BUILD_SHARED_LIBRARY)

Make sure you add 'LOCAL_CFLAGS' after adding 'include $(CLEAR_VARS)'

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
auselen
  • 27,577
  • 7
  • 73
  • 114
  • Note that Android studio auto generates the Android.mk file. To use a custom Android.mk check this answer http://stackoverflow.com/questions/27833530/how-to-use-my-own-android-mk-file-with-android-sudio – Lorne K Oct 02 '15 at 01:02
  • My Android.mk comment is out of date - please check my gradle answer below. – Lorne K Oct 02 '15 at 22:04
1

An addendum to auselen's answer:

According to the NDK docs (kandroid.org mirror), LOCAL_CFLAGS will only apply to each module - if you want this behavior across an entire project, set APP_CFLAGS in Application.mk. Also, CFLAGS will cover C and C++ sources, CPPFLAGS covers C++ only.

MandisaW
  • 971
  • 9
  • 21
  • Thanks @MandisaW for this complement but replacing LOCAL_CFLAGS by APP_CFLAGS in the statement LOCAL_CFLAGS += -std=c99, does not work for me. – Lisarien Mar 15 '14 at 08:54
  • Lisarien, you can't just substitute one for the other - APP_CFLAGS has to go in a different file, Application.mk, rather than Android.mk. – MandisaW Mar 16 '14 at 16:19
1

As people may be arriving here looking to "set standard c99 for compile android NDK project", I think this needs an update.

For Android Studio 1.4 with Gradle 2.5, the c99 can be set in build.gradle

NOTE THAT THE CASE SENSITIVE SYNTAX WITHIN BUILD.GRADLE HAS CHANGED FROM cFlags TO CFlags (many examples online use the old syntax).

here is a build.gradle modified from the sample hello-jni project with C99 support added.

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.0"

        defaultConfig.with {
            applicationId = "com.example.hellojni"
            minSdkVersion.apiLevel = 4
            targetSdkVersion.apiLevel = 23
        }
    }

    compileOptions.with {
        sourceCompatibility=JavaVersion.VERSION_1_7
        targetCompatibility=JavaVersion.VERSION_1_7
    }

    /*
     * native build settings
     */
    android.ndk {
        moduleName = "hello-jni"
            CFlags += "-std=c99"
    }
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles  += file('proguard-rules.txt')
        }
    }
    android.productFlavors {
        // for detailed abiFilter descriptions, refer to "Supported ABIs" @
        // https://developer.android.com/ndk/guides/abis.html#sa
        create("arm") {
            ndk.abiFilters += "armeabi"
        }
        create("arm7") {
            ndk.abiFilters += "armeabi-v7a"
        }
        create("arm8") {
            ndk.abiFilters += "arm64-v8a"
        }
        create("x86") {
            ndk.abiFilters += "x86"
        }
        create("x86-64") {
            ndk.abiFilters += "x86_64"
        }
        create("mips") {
            ndk.abiFilters += "mips"
        }
        create("mips-64") {
            ndk.abiFilters += "mips64"
        }
        // To include all cpu architectures, leaves abiFilters empty
        create("all")
    }
}
Lorne K
  • 109
  • 7