Problems with Android NDK #define

When I add #define to either main.cpp or to one of my headers called from main.cpp, it doesn't seem to be defined in other files.

For example, in main.cpp, I can do something like:

#define TEST_FOO 1 

Then, in one of my other files, for example secondfile.cpp, TEST_FOO is ignored, as if it had never been defined:

 #if TEST_FOO // do something <- this never gets reached #endif 

Even if in the Android.mk file I put the secondfile.cpp file after main.cpp:

 LOCAL_SRC_FILES := main.cpp \ secondfile.cpp 

Is there a way #define values ​​in Android NDK inside the actual code?

+8
android c-preprocessor android-ndk
source share
2 answers

It is right. The compiler only knows about one source file at a time. When you compile secondfile.cpp , it completely forgot about everything that you might have defined in main.cpp .

If you want #define be visible in all source files, you need to put it in a header that will be included with all your files. Or pass it on the command line; you can do this by adding something like this to your Android.mk :

 LOCAL_CPPFLAGS := -DTEST_FOO=1 
+16
source share

Put it in the header file and include the header file in any .c file where you want it to be defined.

0
source share

All Articles