Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set standard c99 for compile android NDK project

Tags:

android-ndk

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?

like image 218
Rusfearuth Avatar asked Nov 21 '12 00:11

Rusfearuth


People also ask

What compiler does NDK use?

Using Android Studio 2.2 and higher, you can use the NDK to compile C and C++ code into a native library and package it into your APK using Gradle, the IDE's integrated build system. Your Java code can then call functions in your native library through the Java Native Interface (JNI) framework.

What is NDK Sysroot?

$NDK/sysroot is the unified headers and is what you should be using when compiling. When linking, you should still be using $NDK/platforms/android-$API/arch-$ARCH . We do have a doc explaining how to use unified headers in a custom build system: android.googlesource.com/platform/ndk/+/master/docs/….


2 Answers

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.

like image 31
MandisaW Avatar answered Oct 03 '22 19:10

MandisaW


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)'

like image 181
auselen Avatar answered Oct 03 '22 17:10

auselen