Openssl Build Issue with Android NDK r8 - c++

I am trying to build Openssl inside my NDK app . I am constantly getting linking error even after following all necessary steps expected by Android ndk build. I am using ndk-build command with ndk supported library. i could see libcrypto.So bieng compiled and linked successfully
Compile thumb : crypto <= sha512-armv4.S
SharedLibrary : libcrypto.so
Install : libcrypto.so => /Users/<me>/Downloads/paddybyers-openssl-android-2b40b8b/libs/armeabi/libcrypto.so
but openssl which starts after this is failing for some reason .I tried on all possible ways including building on eclipse as well as on command line.I even tried to build the openssl seperately as a stand alone project. But it always stops at the same level.
My configuration :
Using mac OS x
NDK r8
Openssl source from : https://github.com/eighthave/openssl-android.git (I tried building on guardproject https://github.com/guardianproject/openssl-android.git )
it is throwing an error before it starts compiling openssl after creating libcrypto.So
****Compile thumb : ssl <= ssl_algs.c
Compile thumb : ssl <= bio_ssl.c
Compile thumb : ssl <= ssl_err.c
Compile thumb : ssl <= kssl.c
SharedLibrary : libssl.so
Executable : openssl
/Users/me/Documents/android/android-ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6.x-google/../../../../arm-linux-androideabi/bin/ld: warning: libz.so, needed by ./obj/local/armeabi/libcrypto.so, not found (try using -rpath or -rpath-link)
./obj/local/armeabi/libcrypto.so: undefined reference to `zError'
./obj/local/armeabi/libcrypto.so: undefined reference to `inflateEnd'
./obj/local/armeabi/libcrypto.so: undefined reference to `deflate'
./obj/local/armeabi/libcrypto.so: undefined reference to `deflateInit_'
./obj/local/armeabi/libcrypto.so: undefined reference to `inflate'
./obj/local/armeabi/libcrypto.so: undefined reference to `deflateEnd'
./obj/local/armeabi/libcrypto.so: undefined reference to `inflateInit_'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/openssl] Error 1**** }
I would really appreciate if some one help me out on this ?

I ran into the same issue and fixed it by modifying OpenSSL1.0.1cForAndroid/crypto/Android.mk, adding libzib to the export list:
crypto/Android.mk
LOCAL_EXPORT_LDLIBS := -lz
Crypto/Android.mk does have "LOCAL_LDLIBS += -lz" and so it successfully links it when building libcrypto but apparently it's not enough to propagate it to when linking in ssl (?).
I'm using ndk r8b, gcc 4.6

Try adding the following line in your OpenSSL1.0.1cForAndroid\jni\Application.mk
NDK_TOOLCHAIN_VERSION=4.4.3
I just fixed the same problem by doing this.

Most likely you have a linking related issue, try to read the answer to this question
How to link any library in ndk application

Looks like you need to link against zlib as well (or disable zlib support in OpenSSL). You can do this with -lz (It's part of Android)

I was also facing same problem with android ndk r8e. Then I have downloaded openssl-static-android form git. That worked like charm. And the version of openssl you are using is successfully build in android ndk r7e. And one more thing, if you want to link this .so files to another library file or to make .apk, I will suggest you to use .a files instead of .so. Because .so files sometimes causes linker problem. If you are intended to use .a files you can use openssl-static-android from git. It works fine with all the versions of ndk.
N.B: If you are using android ndk r8e, I suggest you should check the binary files to check whether it is cleaned or not. Because there is a bug in android ndk r8e.

Related

MinGW in Code::Blocks not linking static OpenSSL Library

I'm trying to take advantage of some of OpenSSL's EC cryptography and ECDSA functionality to make a rudimentary blockchain simulation, but I've run into a major roadblock. I'm using the Code::Blocks IDE, and I've installed OpenSSL using the 64-bit binary found here: https://slproweb.com/products/Win32OpenSSL.html.
In Linker Settings, I added OpenSSL-Win64\lib\openssl.lib
In Search Directories > Compiler, I added OpenSSL-Win64\include
In Search Directories > Linker, I added OpenSSL-Win64\lib
However, when I compile, I get the "undefined reference to (function name)" error for every OpenSSL function I try to use. However, the compiler seems to be successfully including the header files, since it recognizes the new data types like EC_KEY. I suspect a linking error, but I'm not sure what could be wrong, since I have the link paths and static library linked as above.
Here is my build log:
mingw32-g++.exe -LD:\OpenSSL-Win64\lib -LD:\OpenSSL-Win64\include -LD:\OpenSSL-Win64 -o bin\Debug\BlockchainSim.exe obj\Debug\main.o obj\Debug\src\Block.o obj\Debug\src\BlockchainNode.o obj\Debug\src\Transaction.o obj\Debug\src\Valuable.o D:\OpenSSL-Win64\lib\openssl.lib
obj\Debug\src\BlockchainNode.o: In function `ZN14BlockchainNodeC2Ei':
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:18: undefined reference to `EC_KEY_new_by_curve_name'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:21: undefined reference to `BN_new'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:22: undefined reference to `EC_KEY_set_private_key'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:25: undefined reference to `BN_CTX_new'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:26: undefined reference to `BN_CTX_start'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:28: undefined reference to `EC_KEY_get0_group'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:29: undefined reference to `EC_POINT_new'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:30: undefined reference to `EC_POINT_mul'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:31: undefined reference to `EC_KEY_set_public_key'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:34: undefined reference to `EC_POINT_free'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:35: undefined reference to `BN_CTX_end'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:36: undefined reference to `BN_CTX_free'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:37: undefined reference to `BN_clear_free'
obj\Debug\src\BlockchainNode.o: In function `ZN14BlockchainNodeD2Ev':
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:42: undefined reference to `EC_KEY_free'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
15 error(s), 0 warning(s) (0 minute(s), 0 second(s))
I'm hoping I'm just missing something simple here since I'm new to linking static libraries. A lot of similar issues seem to be solved by adding options to compile commands, but since I'm using Code::Blocks and linking OpenSSL as a static library, I'm not sure if those apply here. Any help is greatly appreciated.
Figured it out. The issue was that I had no C:/MinGW folder (as Code::Blocks installed MinGW within its own directory). I'm guessing the binary I used tries to detect existing compilers and compiles the library differently depending on what it finds.
I fixed it by installing default MinGW (with MSYS, in case that matters) with the default path (C:/MinGW) and then reinstalling the 32-bit binary OpenSSL linked in the original question, also in the default path (C:/OpenSSL-Win32). This made it so that within the C:/OpenSSL-Win32/lib folder, there was a new MinGW folder which I then included in the linker search directories.

RcppGSL Windows installation trouble

I'm trying to install RcppGSL as presented in
Linking GSL library to RcppGSL on Windows machine
after copping the library as presented in the link above to C:/local323
and moving all the files in x64 up one directory into into LIB_GSL/lib as presented in the link. I used the following code.
library(Rcpp)
Sys.setenv("LIB_GSL" = "C:/local323") # this is where the library is located
Sys.setenv("PKG_LIBS"="-L(LIB_GSL)/lib -lgsl -lgslcblas")
install.packages("RcppGSL")
I added the line
Sys.setenv("PKG_LIBS"="-L(LIB_GSL)/lib -lgsl -lgslcblas")
after getting the following error(and I still got the same error) after doing some research and thought there might be a linking problem(just guessing).
The error I received was
RcppExports.o:RcppExports.cpp:(.text+0x916): undefined reference to `gsl_matrix_alloc'
RcppExports.o:RcppExports.cpp:(.text+0x945): undefined reference to `gsl_matrix_set'
RcppExports.o:RcppExports.cpp:(.text+0x993): undefined reference to `gsl_vector_calloc'
fastLm.o:fastLm.cpp:(.text+0x122): undefined reference to `gsl_vector_calloc'
fastLm.o:fastLm.cpp:(.text+0x131): undefined reference to `gsl_matrix_alloc'
fastLm.o:fastLm.cpp:(.text+0x142): undefined reference to `gsl_multifit_linear_alloc'
fastLm.o:fastLm.cpp:(.text+0x16d): undefined reference to `gsl_multifit_linear'
fastLm.o:fastLm.cpp:(.text+0x175): undefined reference to `gsl_multifit_linear_free'
fastLm.o:fastLm.cpp:(.text+0x24f): undefined reference to `gsl_matrix_diagonal'
setErrorHandler.o:setErrorHandler.cpp:(.text+0x104): undefined reference to `gsl_set_error_handler_off'
setErrorHandler.o:setErrorHandler.cpp:(.text+0x133): undefined reference to `gsl_set_error_handler'
any help is greatly appreciated!
Very best,
Steve
I would try this on the command-line, ie in cmd.exe -- not from R.
That way you should see the compilation and linking steps which may help when something go south.
"It builds at CRAN ..." so with the right files in the right location, and the proper env var set it should for you too.
Well I do not know why the following worked but I'm able to use RcppGSL
I followed the procedure from the link I posted exactly. Then instead of using
install.packages("RcppGSL")
I downloaded the zip file from CRAN and installed the package via the R GUI with the option to install from a zip. These should be identical I know, but using install.packages("RcppGSL").
Also
Sys.setenv("LIB_GSL" = "C:/local323")
is required anytime I try to compile an Rcpp file that uses the library.
I'm not sure why, but I can use the library at least.
Dirk thank you for your help. I will go over the tutorial on R-extensions to see if I can figure out what is going on.

vtk autoinit linker error

I have a very confusing (at least for me :) ) problem with linking to vtk-libraries. On one desktop pc, my code compiles without any error. I have an identical installation on another pc, but there, I get linker errors.
On both pc's are installed: itk 4.8, vtk 6.3 and cmake 3.3. Both are debian-systems (8.1 - jessie).
[ 96%] Linking CXX executable project
CMakeFiles/project.dir/main.cpp.o: In function vtkRenderingVolume_AutoInit::~vtkRenderingVolume_AutoInit()':
/usr/local/include/vtk-6.3/vtkRenderingVolumeModule.h:44: undefined reference tovtkRenderingVolumeOpenGL_AutoInit_Destruct()'
CMakeFiles/project.dir/main.cpp.o: In function vtkRenderingVolumeOpenGL_ModuleInit::~vtkRenderingVolumeOpenGL_ModuleInit()':
project/main.cpp:4: undefined reference tovtkRenderingVolumeOpenGL_AutoInit_Destruct()'
CMakeFiles/project.dir/main.cpp.o: In function vtkRenderingVolumeOpenGL_ModuleInit':
project/main.cpp:4: undefined reference tovtkRenderingVolumeOpenGL_AutoInit_Construct()'
CMakeFiles/project.dir/main.cpp.o: In function vtkRenderingVolume_AutoInit':
/usr/local/include/vtk-6.3/vtkRenderingVolumeModule.h:44: undefined reference tovtkRenderingVolumeOpenGL_AutoInit_Construct()'
libcommon.a(datareader.cpp.o): In function vtkRenderingVolume_AutoInit':
/usr/local/include/vtk-6.3/vtkRenderingVolumeModule.h:44: undefined reference tovtkRenderingVolumeOpenGL_AutoInit_Construct()'
collect2: error: ld returned 1 exit status
CMakeFiles/project.dir/build.make:326: recipe for target 'project' failed
I tried to fix those errors with adding
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL);
to the file where I am using the vtk libraries, but it didn't help.
Does anyone know what I'm doing wrong? I am wondering, why it works on one of those pcs but not on the other one.
thank you very much!
Update: I fixed my problem by switching the position of
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
in the CMakeLists to the end. However, does anyone know, why it makes such a huge difference for different pcs?
As long as the VTK_USE_FILE is found, your code should compile correctly. In your other PC, VTK_USE_FILE was already found from a previous configuration and sits in a CMake cache entry, so that is why it compiles fine.
If your are using Nightly,
Your should put these in front of your including any vtk headers.
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL);
and link all the library you made from vtk source

Error of linking a static lib embedded with Lua on Linux

Update
after using -ldl and link liblua.a in Project 2(console) the compile is okay, but when it runs it crashed with error Segment fault (core dumped) as soon as it runs a Lua function.
Problem Background:
OS: Linux Ubuntu
IDE: CodeBlock
Launguage: C++
2 project:
Project 1: static lib using Lua;
Project 2: console application, using the lib generated by Project 1
Problem description
Project 1 (static lib) is built successfully.
The problem is that when building project 2, it says that those Lua functions in the lib from project 1 are undefined, here is part of the error messages:
g++ -o bin/Release/BattleConsoleCB obj/Release/main.o -s
../BattleConsole/libBattleCore.a
../BattleConsole/libBattleCore.a(DataLoader.o):
In function `boolDataLoader::GetNumber<double>(char const*, double&) [clone .isra.5]':
DataLoader.cpp:(.text+0x13): undefined reference to `lua_settop'
DataLoader.cpp:(.text+0x1e): undefined reference to `lua_getglobal'
DataLoader.cpp:(.text+0x2b): undefined reference to `lua_isnumber'
DataLoader.cpp:(.text+0x3e): undefined reference to `lua_tonumberx'
DataLoader.cpp:(.text+0x51): undefined reference to `lua_settop'
Note that "DataLoader.cpp" is from project 1, which should have been built in the static lib "libBattleCore.a" which should have been embedded with Lua.
How to solve the problem? Thanks for your help.
Additional information:
Project 2 should include: "libBattleCore.h", "main.cpp", libBattleCore.a
Project 1 : CodeBlockbuilding options have included "Lua/install/include" in Compile search directory and "Lua/install/lib" in Link search directory
The same code is successfully built and run on Win with VS2012
If anything else is needed, please inform, I will add it.
I am a green hand on linux and g++. :-(
Thank you
I can't comment without 50 rep. There is no support for anonymous replies. So posting here is all I can do.
It's been a few years, but with g++, I thought it was .o files that you listed on the command line and with libraries you used -L to specify the directory to search and then -lname to include the library. (Where name lacked the leading lib and trailing .a.) E.g. -L../BattleConsole -lBattleCore
Oh, and the ordering of -lfoo -lbar vs -lbar -lfoo is critical, determining which library can use code from the other.
It's not clear that a static library will necessary include code from other libraries. You may need to link (in the right order) against the libraries from project1.
Try "nm". As in nm --demangle libBattleCore.a | grep ... This will let you see what precisely is included in each library. man nm or http://linux.die.net/man/1/nm can help you figure out what the letters stand for. For example, U is Undefined (used by not provided). T is defined in the text (code) segment. W and V are weakly-defined. Etc.

Libusb undefined reference to

I'm trying to set up libusb API on my OS. I downloaded libusb api on libusb.org. I followed the standard installation procedure:
cd into directory
./configure
make
make check //without errors
make install
Then I launched Eclipse C/C++ and copied some code from the tutorial found on the internet. But when trying to build it I got following output:
main.cpp:(.text+0x19): undefined reference to `libusb_init'
main.cpp:(.text+0x76): undefined reference to `libusb_set_debug'
main.cpp:(.text+0x8a): undefined reference to `libusb_get_device_list'
main.cpp:(.text+0x136): undefined reference to `libusb_free_device_list'
main.cpp:(.text+0x142): undefined reference to `libusb_exit'
/tmp/ccOWJGwe.o: In function `printdev(libusb_device*)':
main.cpp:(.text+0x162): undefined reference to `libusb_get_device_descriptor'
main.cpp:(.text+0x28a): undefined reference to `libusb_get_config_descriptor'
main.cpp:(.text+0x4d4): undefined reference to `libusb_free_config_descriptor'
collect2: ld returned 1 exit status
I have libusb.so in /lib and also I have usb.h in /usr/local/include and the link for the .so and libusb.a in /usr/local/lib.
Also the #include inside the code is correct.
I know that problem is in linker but I, kind of, cannot make it work :)
I'm using Fedora 15 operating system and gcc 4.6.0 20110603 (Red Hat 4.6.0-10) version compiler.
So what could I do to resolve these undefined references? Thanks very much for help :)
I did face the same problem. But I was able to solve it by adding '-lusb-1.0' to the linker.
e.g : g++ myfile.cpp -lusb-1.0
you have to set the library linker flag for compilation in the linker,
you can get a full list in the console by executing
pkg-config --list-all
These are the libraries which you have installed on your system and you have to link against the ones you want to use.
so in your example it is libusb so you do
pkg-config --libs libusb
there should be the output
-lusb
or
-lusb-1.0
This gives you the flag you have to pass to the linker. e.g.
g++ myfile.cpp -lusb[-1.0]
Then you edit the configuration of the project and search for the linkerflags, there should be a textfield for that somewhere in the buildoptions. i'm not quite shure where to find it but googling for it suggested:
Project -> Properties -> C/C++
Build -> Miscellaneous -> flags
After you found it, just add the linker flag in the textfield and you should be fine.
EDIT
since my answer is the accepted one, I also added the other flag that seems to work for a lot of people.
What is your linker command line? You need to have -lusb in the linking command; just having the header included won't work.
I don't use Eclipse C/C++ but I am pretty sure the reason is the same that I faced some while ago when setting up a C project in Netbeans.
It's not enough to have the #include in your code and the library at the right location - you also have to tell Eclipse where to look for them and how to use them. This turorial shows you how to set it up in Eclipse.