Issue
I am trying to compile an executable binary to be packaged with my Java android application. The executable, VMD, has a main file vmdmain.C with the following
#if defined(ANDROID)
int VMDmain(int argc, char **argv) {
# else
int main(int argc, char **argv) {
#endif
It seems that the android ndk with cmake implicitly sets the ANDROID flag via -DANDROID
. This causes the main function above to be defined as VMDmain instead. Of course, with no main function, the executable fails to link, giving the error:
/home/username/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/i686-linux-android/24/crtbegin_dynamic.o:crtbegin.c:function _start_main: error: undefined reference to 'main'
I therefore want to, for the file vmdmain.C only, unset the ANDROID flag, so that the main function will be named main instead of VMDmain. I have tried in CMakeLists.txt
set_source_files_properties(src/main/vmd/vmd-1.9.3/src/vmdmain.C PROPERTIES COMPILE_FLAGS -DANDROID=0)
where the add_executable
directive in the same CMakeLists.txt file looks like
add_executable(
vmd
src/main/vmd/vmd-1.9.3/src/vmdmain.C
# other files...
)
which results in the following entry in the "command" key for vmdmain.c in compile_commands.json (linebreaks added, some unrelated compile flags and includes, libs excluded for clarity):
/home/username/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ \
--target=i686-none-linux-android24 \
--gcc-toolchain=/home/ning/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 \
-DARCH_ANDROIDARMV7A \
-DTCL_LIBRARY=\\\"/home/username/application/app/.externalNativeBuild/cmake/debug/x86\\\" \
-DTCL_PACKAGE_PATH=\\\"/home/username/application/app/.externalNativeBuild/cmake/debug/x86\\\" \
-DVMDMSMS \
-DVMDNANOSHAPER \
-DVMDPLUGIN_STATIC \
-DVMDSURF \
--sysroot /home/ning/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot \
-g \
-DANDROID \
-fdata-sections \
-ffunction-sections \
-funwind-tables \
-fstack-protector-strong \
-no-canonical-prefixes \
-fno-addrsig \
-Wa,--noexecstack \
-Wformat \
-Werror=format-security \
-stdlib=libc++ \
-O0 \
-fno-limit-debug-info \
-fPIE \
-DANDROID=0 \
-o CMakeFiles/vmd.dir/src/main/vmd/vmd-1.9.3/src/vmdmain.C.o \
-c /home/ning/github/palantir/app/src/main/vmd/vmd-1.9.3/src/vmdmain.C
However, this doesn't seem to work. I still get the same undefined reference to main
error.
Solution
To disable a definition for C preprocessor, use
-UANDROID
It's true that
#if (ANDROID)
will work the same for -DANDROID=0
and -UANDROID
, but the following (equivalent) statements behave differently:
#if defined(ANDROID)
or
#ifdef ANDROID
Answered By - Alex Cohn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.