How to use NDK wth gcc on Android? - gcc

I need to compile OpenCV 2 to test some things and it can only be built with gcc. I tried downloading the oldest NDK in Android Studio which is version 16, however it still tries to use clang. How can I force it to use gcc?
Error on version 16:
ERROR: /home/user/AndroidStudioProjects/opencv2builder/app/src/main/cpp/CMakeLists.txt : C/C++ debug|x86 : CMake Error at /home/user/Android/Sdk/cmake/3.10.2.4988404/share/cmake-3.10/Modules/CMakeTestCCompiler.cmake:52 (message):
The C compiler
"/home/user/Android/Sdk/ndk/16.1.4479499/toolchains/llvm/prebuilt/linux-x86_64/bin/clang"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /home/user/AndroidStudioProjects/opencv2builder/app/.cxx/cmake/debug/x86/CMakeFiles/CMakeTmp
Run Build Command:"/home/user/Android/Sdk/cmake/3.10.2.4988404/bin/ninja" "cmTC_ed369"
[1/2] Building C object CMakeFiles/cmTC_ed369.dir/testCCompiler.c.o
FAILED: CMakeFiles/cmTC_ed369.dir/testCCompiler.c.o
/home/user/Android/Sdk/ndk/16.1.4479499/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=i686-none-linux-android --gcc-toolchain=/home/user/Android/Sdk/ndk/16.1.4479499/toolchains/x86-4.9/prebuilt/linux-x86_64 --sysroot=/home/user/Android/Sdk/ndk/16.1.4479499/sysroot -isystem /home/user/Android/Sdk/ndk/16.1.4479499/sysroot/usr/include/i686-linux-android -D__ANDROID_API__=16 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mstackrealign -Wa,--noexecstack -Wformat -Werror=format-security -fPIE -o CMakeFiles/cmTC_ed369.dir/testCCompiler.c.o -c testCCompiler.c
/home/user/Android/Sdk/ndk/16.1.4479499/toolchains/llvm/prebuilt/linux-x86_64/bin/clang: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt
Affected Modules: app

Related

Unable to link libraries when compiling native node modules to Android. Recompile with -fPIC issue when using -fPIC flag

This question is mostly related to node-gyp, GCC and the NDK toolchain, but I will add the full context because it may be necessary.
Context
I am building an App using React Native. Since I need to use node native libraries in this App, I am using this node module nodejs-mobile for running a Node.js process on Android and iOS.
When adding the composer-admin module, I am having compilation issues for Android.
Error
The compiler is failing when linking the grpc_node module which is required by a composer-admin dependency. The error is:
[LIB 1 PATH OMITTED] requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC
...
[LIB N PATH OMITTED] requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC
clang70++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [grpc_node.target.mk:189: Release/obj.target/grpc_node.node] Error 1
make: Leaving directory '/home/vanclief/Cacao_repos/react-app/android/build/nodejs-native-assets-temp-build/nodejs-native-assets-armeabi-v7a/nodejs-project/node_modules/fabric-client/node_modules/grpc/build'
The weird thing is the -fPIC flag is being used as specified in the common.gypi:
/home/vanclief/Cacao_repos/react-app/android/build/standalone-toolchains/arm-linux-androideabi/bin/arm-linux-androideabi-clang++ -shared -g -rdynamic -fPIC -Wl,-soname=grpc_node.node -o
This is the full compilation log
common.gypi is wrong. It has -fPIC in ldflags. It needs to be in cflags.

Force CMake in verbose mode via Gradle and the Android NDK

I'm using Gradle and CMake to compile an Android NDK project from the command line. Previously, I was using Ant and ndk-build but I'm trying to migrate the project completely to Gradle and CMake.
In my build.gradle I have the following lines to invoke CMake:
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}
Now how can I force CMake to print all compiler calls to the console before it makes them? Specifically, I want to see just how CMake runs the compiler and linker.
I've already tried the following, all to no avail:
1) In my CMakeLists.txt I have put the following line:
set(CMAKE_VERBOSE_MAKEFILE on)
Didn't have any effect.
2) I have started the build like this:
./gradlew build --info
Gradle printed some stuff, but no compiler calls.
3) And like this:
./gradlew build --debug
Gradle printed lots of stuff, but no compiler calls.
So none of those three attempts did what I wanted which makes me wonder how can I see how CMake runs clang on my individual source files?
Disclaimer: the following description applies to the version of Android Gradle Plugin (AGP) that was the latest at the time I updated this Answer (aug '21). If you are curious, have a look at the history of edits.
As #artyomd has noticed, for AGP 4.2.0 and higher, you can set android.native.buildOutput gradle property to verbose to force cmake logging.
In Android Studio, gradle creates directory .cxx under the module root, for each module that has NDK integration, via CMake or ndk-build.
For CMake, the gradle plugin is quite verbose. For each build variant it creates separate subdirectory, e.g. .cxx/cmake/debug/x86 or .cxx/cmake/release/armeabi-v7a, etc.
Each directory contains some useful files: cmake_build_command.txt describes the actual parameters passed to CMake; android_gradle_build.json shows what parameters the gradle plugin derived for your binaries; from build.ninja you can deduce the how these parameters were applied for each compilation or linkage step.
For ndk-build, the android_gradle_build.json file is also quite useful. ndkBuild_build_command.txt lists all parameters passed to ndk-build command, and ndkBuild_build_output.txt is the unabridged output of that command. You can easily add V=1 to the arguments, e.g.
externalNativeBuild {
ndkBuild {
cppFlags "-std=c++11"
arguments "APP_STL=c++_static", "APP_OPTIM=release", "NDK_DEBUG=0", "V=1"
abiFilters "armeabi-v7a"
}
}
For CMake, the relevant argument is "-DCMAKE_VERBOSE_MAKEFILE=ON" (see explanation
and alternatives):
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
arguments "-DCMAKE_VERBOSE_MAKEFILE=ON"
abiFilters "armeabi-v7a"
}
}
But, as #user7860670 has observed, the recent versions of AGP ignore this flag.
Without CMAKE_VERBOSE_MAKEFILE, the Gradle Console displays:
:app:externalNativeBuildDebug
Build native-lib armeabi-v7a
[1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
[2/2] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so
With "-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON", I used to get tons of output:
:app:externalNativeBuildDebug
Build native-lib armeabi-v7a
[1/2] /Users/alex/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=armv5te-none-linux-androideabi --gcc-toolchain=/Users/alex/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/alex/Library/Android/sdk/ndk-bundle/platforms/android-14/arch-arm -Dnative_lib_EXPORTS -isystem /Users/alex/Library/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include -isystem /Users/alex/Library/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include -isystem /Users/alex/Library/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include/backward -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -MF CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o.d -o CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -c /Users/alex/test/egl/app/src/main/cpp/native-lib.cpp
[2/2] : && /Users/alex/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=armv5te-none-linux-androideabi --gcc-toolchain=/Users/alex/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/alex/Library/Android/sdk/ndk-bundle/platforms/android-14/arch-arm -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -lEGL -lGLESv2 -lm "/Users/alex/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_static.a" "-latomic" && :
Not anymore. As far as I know, this information is filtered by Gradle Plugin and is completely lost. I can recover it only manually: run the command
/Users/alex/Library/Android/sdk/cmake/3.10.2.4988404/bin/cmake --build app/.cxx/cmake/debug/armeabi-v7a
You can use the Terminal window (Alt-F12) of Android Studio. This will invoke ninja with -v flag when -DCMAKE_VERBOSE_MAKEFILE=ON was used to sync C++ with Gradle.
Note that the expected file .cxx/cmake/debug/armeabi-v7a/cmake_build_output.txt does not contain interesting information (unless you have problems with CMake configuration per se).
P.S. with this 3.6.0 Gradle Plugin, if you have a compilation error, then the whole command line for compiler is shown in Build Output window, no matter whether you set CMAKE_VERBOSE_MAKEFILE or not. Actually, twice: once, in black on white (I don't use the dark theme), second time, in brown on white, after
FAILURE: Build failed with an exception.
* What went wrong:
As an ugly workaround I've replaced ninja with my own executable that passes all the commands to the real ninja executable appending "-v"
Try to update your gradle version.

Compile C++ code containing OpenMP with manually built gcc compiler

I've downloaded from github and built gcc. After that I've tried to compile a code with OpenMP:
../GCC/build/gcc/xgcc -B./../GCC/build/gcc/ -I./../GCC/build/x86_64-unknown-linux-gnu/libgomp -Wno-write-strings -O3 -Wall -fopenmp -lpng -o mandelbrot-omp mandelbrot-omp.cpp​
Then I got the following error message:
xgcc: error: libgomp.spec: No such file or directory​
I've checked and found out that libgomp directory contains the libgomp.spec file. The directory is included with -I option. What's wrong?

Clang boost header not found

I am trying to compile a Python PCL module which builds some C++ source. I am getting this error:
$ python setup.py install
running install
running build
running build_py
running build_ext
skipping 'pcl/_pcl.cpp' Cython extension (up-to-date)
building 'pcl._pcl' extension
/usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -g -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Library/Python/2.7/site-packages/numpy/core/include -I/usr/local/Cellar/pcl/HEAD/include/pcl-1.8 -I/usr/local/Cellar/eigen/3.2.3/include/eigen3 -I/usr/local/Cellar/pcl/HEAD/include/pcl-1.8 -I/usr/local/Cellar/flann/1.8.4/include -I/usr/local/Cellar/pcl/HEAD/include/pcl-1.8 -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c pcl/_pcl.cpp -o build/temp.macosx-10.6-intel-2.7/pcl/_pcl.o
pcl/_pcl.cpp:244:10: fatal error: 'boost/smart_ptr/shared_ptr.hpp' file not
found
#include "boost/smart_ptr/shared_ptr.hpp"
^
1 error generated.
error: command '/usr/bin/clang'
For whatever reason clang isn't looking in /usr/local/include where it most definitely would find the boost headers. As you can see it is linking all the other dependencies fine. What can I add that so clang will find boost?
On OSX 10.10, nothing fancy happening anywhere. Boost was probably installed by homebrew, but the files are all in /usr/local/include/boost as I'd expect.
Check the output of /usr/bin/clang++ -v some_test_file.cpp but chances are /usr/local/include isn't in the standard search path of the compiler.
You'll need to add -I/usr/local/include to CXXFLAGS or CPPFLAGS or whatever appropriate place in your build script or environment. It seems the python build script is failing to properly detect Boost.
If the above is not true (and /usr/local/include does show up in the output), make sure /usr/local/include/boost/smart_ptr/shared_ptr.hpp exists.

Build wxWidgets 2.8.12 GCC 4.6.2

I am getting this error when building wxWidgets in Cygwin:
../src/generic/dirctrlg.cpp:67:24: fatal error: direct.h: No such file or directory
compilation terminated.
make: *** [corelib_dirctrlg.o] Error 1
T_BITS=64 -D_LARGE_FILES -D__WXDEBUG__ -I/cygdrive/c/wxWidgets-2.8.12/build-debug/lib/wx/include/msw-ansi-debug-static-2.8 -I../include -Wall -Wundef -Wno-ctor-dtor-privacy -ggdb -O0 ../src/generic/dirctrlg.cpp
Makefile:28340: recipe for target `corelib_dirctrlg.o' failed
I followed the instructions included in wxWidgets for Windows using configure in install.txt(Compiling wxWidgets).
Which compiler do you use? See http://wxwidgets.blogspot.com/2011/06/choosing-gcc-for-building-wxwidgets.html for some of the choices you have. FWIW direct.h should be present in MinGW compiler headers.

Resources