如何让Android使用ZeroMQ libzmq?
我尝试将一个程序从Linux桌面移植到Android。在C++中使用<代码> LBZMQ > 我想在没有如何让Android使用ZeroMQ libzmq?,android,c++,cross-compiling,zeromq,Android,C++,Cross Compiling,Zeromq,我尝试将一个程序从Linux桌面移植到Android。在C++中使用 LBZMQ > 我想在没有jerozmq或jzmq的应用程序的本机代码中使用zmq.hpp(ZeroMQ的Cpp绑定) 我已经使用此脚本为4 arch构建了libzmq: export PATH=/home/hmi/Android/arm-26-toolchain-clang:$PATH rm -r /tmp/zeromq-android cd /tmp/ #git clone https://github.com/zer
jerozmq
或jzmq
的应用程序的本机代码中使用zmq.hpp
(ZeroMQ的Cpp绑定)
我已经使用此脚本为4 arch构建了libzmq
:
export PATH=/home/hmi/Android/arm-26-toolchain-clang:$PATH
rm -r /tmp/zeromq-android
cd /tmp/
#git clone https://github.com/zeromq/libzmq.git
cd libzmq/
############################################################################
# arm-linux-androideabi
export OUTPUT_DIR=/tmp/zeromq-android/arm-linux-androideabi
./autogen.sh
./configure --enable-static \
--disable-shared \
--host=arm-linux-androideabi \
--prefix=$OUTPUT_DIR \
LDFLAGS="-L$OUTPUT_DIR/lib" \
CPPFLAGS="-fPIC -I$OUTPUT_DIR/include" \
LIBS="-lgcc"
make -j4
make install
############################################################################
# armv7a-linux-androideabi
export OUTPUT_DIR=/tmp/zeromq-android/armeabi-v7a
./autogen.sh
./configure --enable-static \
--disable-shared \
--host=armv7a-linux-androideabi \
--prefix=$OUTPUT_DIR \
LDFLAGS="-L$OUTPUT_DIR/lib" \
CPPFLAGS="-fPIC -I$OUTPUT_DIR/include" \
LIBS="-lgcc"
make -j4
make install
############################################################################
# aarch64-linux-android
export OUTPUT_DIR=/tmp/zeromq-android/arm64-v8a
./autogen.sh
./configure --enable-static \
--disable-shared \
--host=aarch64-linux-android \
--prefix=$OUTPUT_DIR \
LDFLAGS="-L$OUTPUT_DIR/lib" \
CPPFLAGS="-fPIC -I$OUTPUT_DIR/include" \
LIBS="-lgcc"
make -j4
make install
############################################################################
# i686-linux-android
export OUTPUT_DIR=/tmp/zeromq-android/x86
./autogen.sh
./configure --enable-static \
--disable-shared \
--host=i686-linux-android \
--prefix=$OUTPUT_DIR \
LDFLAGS="-L$OUTPUT_DIR/lib" \
CPPFLAGS="-fPIC -I$OUTPUT_DIR/include" \
LIBS="-lgcc"
make -j4
make install
############################################################################
# x86_64-linux-android
export OUTPUT_DIR=/tmp/zeromq-android/x86_64
./autogen.sh
./configure --enable-static \
--disable-shared \
--host=x86_64-linux-android \
--prefix=$OUTPUT_DIR \
LDFLAGS="-L$OUTPUT_DIR/lib" \
CPPFLAGS="-fPIC -I$OUTPUT_DIR/include" \
LIBS="-lgcc"
make -j4
make install
cd /tmp/zeromq-android/
mkdir all
cd all
for i in \
"armeabi-v7a" \
"arm64-v8a" \
"x86" \
"x86_64"
do
mkdir ${i}
cp -r /tmp/zeromq-android/$i/lib/* ./$i/
done
############################################################################
## jzmq
#export OUTPUT_DIR=/tmp/zeromq-android-arm-linux-androideabi
#
#cd /tmp/
#git clone https://github.com/zeromq/jzmq.git
#cd jzmq/jzmq-jni/
#./autogen.sh
#./configure --host=arm-linux-androideabi \
# --prefix=$OUTPUT_DIR \
# --with-zeromq=$OUTPUT_DIR \
# CPPFLAGS="-fPIC -I$OUTPUT_DIR/include" \
# LDFLAGS="-L$OUTPUT_DIR/lib" \
# --disable-version \
# LIBS="-lpthread -lrt"
#make
#make install}
可能吗
链接native-lib.cpp static和libzmq static时,构建是正常的,但它会阻塞:
Runtime.error.loadingLib(本机库)
在本机lib shared和libzmq static中链接时,构建将在以下位置进行:
/home/hmi/barepo/AndroidCppZmq/app/src/main/cpp/include/zmq.hpp:766:错误:未定义对“zmq\u ctx\u destroy”的引用。
鉴于报告的错误是:
错误:未定义对“zmq\u ctx\u destroy”的引用。
这让我相信,你可能混淆了不同的版本
<> > Git URL导致“C++中的ZrOMQ核心引擎,实现ZMTP/3.1”,其中版本3.1在ZrOMQ本地API中确实没有<代码> ZMQJCTXXDebug()/<代码>函数。
您可以决定使用或破解
zmq\u ctx\u term()
-调用,以实际调用v3.1 API当前替换int-zmq\u term(void*context)
,但是适当的Android端口应该与干净的API实现共存,不是吗?鉴于报告的错误是:
错误:未定义对“zmq\u ctx\u destroy”的引用。
这让我相信,你可能混淆了不同的版本
<> > Git URL导致“C++中的ZrOMQ核心引擎,实现ZMTP/3.1”,其中版本3.1在ZrOMQ本地API中确实没有<代码> ZMQJCTXXDebug()/<代码>函数。
您可以决定是向前跳转,使用,还是破解
zmq\u ctx\u term()
-调用来实际调用v3.1 API当前的替换int-zmq\u term(void*context)
,但正确的Android端口应该与干净的API实现共存,不是吗?谢谢您的回复。所以现在我想用Android虚拟设备(像素C API 28)测试,并在Android Studio.c/p中创建了一个带有C++代码的新项目。
为了摆脱cppzmq,我现在只使用libzmq
我使用以下脚本构建了libzmqlibzmq
:
#!/bin/bash
OLDPATH=$PATH
rm -r /tmp/zeromq-android
cd /tmp/
git clone https://github.com/zeromq/libzmq.git
cd libzmq/
export NDK=$HOME/initEnvV0.0/AndroidSdk/ndk/21.0.6113669
export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
# Only choose one of these, depending on your device...
#export TARGET=aarch64-linux-android
#export TARGET=armv7a-linux-androideabi
#export TARGET=i686-linux-android
export TARGET=x86_64-linux-android
# Set this to your minSdkVersion.
export API=28
# Configure and build.
export AR=$TOOLCHAIN/bin/$TARGET-ar
export AS=$TOOLCHAIN/bin/$TARGET-as
export CC=$TOOLCHAIN/bin/$TARGET$API-clang
export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
export LD=$TOOLCHAIN/bin/$TARGET-ld
export RANLIB=$TOOLCHAIN/bin/$TARGET-ranlib
export STRIP=$TOOLCHAIN/bin/$TARGET-strip
export OUTPUT_DIR=/tmp/zeromq-android/$TARGET
./autogen.sh
#./configure --host $TARGET
./configure --enable-static \
--disable-shared \
--host=$TARGET \
--prefix=$OUTPUT_DIR LDFLAGS="-L$OUTPUT_DIR/lib" \
CPPFLAGS="-fPIC -I$OUTPUT_DIR/include" \
LIBS="-lgcc" # ^ was missing
make
make install
将内置库复制到jniFolder
后,我的目录如下所示:
src/main/jniLibs/
└── x86_64
├── bin
│ └── curve_keygen
├── include
│ ├── zmq.h
│ └── zmq_utils.h
└── lib
├── libzmq.a
├── libzmq.la
├── libzmq.so
└── pkgconfig
└── libzmq.pc
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
include_directories(
/home/hmi/barepo/AndroidCppZmq/app/src/main/jniLibs/x86_64/include
)
add_library( zmq STATIC IMPORTED )
set_target_properties( zmq PROPERTIES IMPORTED_LOCATION /home/hmi/barepo/AndroidCppZmq/app/src/main/jniLibs/x86_64/lib/libzmq.a )
set_target_properties( zmq PROPERTIES INCLUDE_DIRECTORIES /home/hmi/barepo/AndroidCppZmq/app/src/main/jniLibs/x86_64/include )
add_library( native-lib # Sets the name of the library.
SHARED # Sets the library as a shared library.
native-lib.cpp # Provides a relative path to your source file(s).
)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( log-lib # Sets the name of the path variable.
log # Specifies the name of the NDK library that
) # you want CMake to locate.
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( native-lib # Specifies the target library.
zmq # Links the target library to the log library
${log-lib} # included in the NDK.
)
我已将此添加到本机lib.cpp中:
// libzmq
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
int testlibzmq (void)
{
// Socket to talk to clients
void *context = zmq_ctx_new ();
void *responder = zmq_socket (context, ZMQ_REP);
int rc = zmq_bind (responder, "tcp://*:5555");
assert (rc == 0);
char buffer [10];
zmq_recv (responder, buffer, 10, 0);
printf ("Received Hello\n");
sleep (1); // Do some 'work'
zmq_send (responder, "World", 5, 0);
return 0;
}
我是否必须在我的jniLibs文件夹旁边生成一个Android.mk
和Application.mk
文件?是否有人试图在本机代码中使用libzmq
,而没有jzmq
或Jeromq
构建链接共享本机库的stuck
:
Entering directory `/home/hmi/barepo/AndroidCppZmq/app/.cxx/cmake/debug/x86_64'
[1/1] Linking CXX shared library /home/hmi/barepo/AndroidCppZmq/app/build/intermediates/cmake/debug/obj/x86_64/libnative-lib.so
FAILED: /home/hmi/barepo/AndroidCppZmq/app/build/intermediates/cmake/debug/obj/x86_64/libnative-lib.so
谢谢你的回复。所以现在我想用Android虚拟设备(像素C API 28)测试,并在Android Studio.c/p中创建了一个带有C++代码的新项目。
为了摆脱cppzmq,我现在只使用libzmq
我使用以下脚本构建了libzmqlibzmq
:
#!/bin/bash
OLDPATH=$PATH
rm -r /tmp/zeromq-android
cd /tmp/
git clone https://github.com/zeromq/libzmq.git
cd libzmq/
export NDK=$HOME/initEnvV0.0/AndroidSdk/ndk/21.0.6113669
export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
# Only choose one of these, depending on your device...
#export TARGET=aarch64-linux-android
#export TARGET=armv7a-linux-androideabi
#export TARGET=i686-linux-android
export TARGET=x86_64-linux-android
# Set this to your minSdkVersion.
export API=28
# Configure and build.
export AR=$TOOLCHAIN/bin/$TARGET-ar
export AS=$TOOLCHAIN/bin/$TARGET-as
export CC=$TOOLCHAIN/bin/$TARGET$API-clang
export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
export LD=$TOOLCHAIN/bin/$TARGET-ld
export RANLIB=$TOOLCHAIN/bin/$TARGET-ranlib
export STRIP=$TOOLCHAIN/bin/$TARGET-strip
export OUTPUT_DIR=/tmp/zeromq-android/$TARGET
./autogen.sh
#./configure --host $TARGET
./configure --enable-static \
--disable-shared \
--host=$TARGET \
--prefix=$OUTPUT_DIR LDFLAGS="-L$OUTPUT_DIR/lib" \
CPPFLAGS="-fPIC -I$OUTPUT_DIR/include" \
LIBS="-lgcc" # ^ was missing
make
make install
将内置库复制到jniFolder
后,我的目录如下所示:
src/main/jniLibs/
└── x86_64
├── bin
│ └── curve_keygen
├── include
│ ├── zmq.h
│ └── zmq_utils.h
└── lib
├── libzmq.a
├── libzmq.la
├── libzmq.so
└── pkgconfig
└── libzmq.pc
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
include_directories(
/home/hmi/barepo/AndroidCppZmq/app/src/main/jniLibs/x86_64/include
)
add_library( zmq STATIC IMPORTED )
set_target_properties( zmq PROPERTIES IMPORTED_LOCATION /home/hmi/barepo/AndroidCppZmq/app/src/main/jniLibs/x86_64/lib/libzmq.a )
set_target_properties( zmq PROPERTIES INCLUDE_DIRECTORIES /home/hmi/barepo/AndroidCppZmq/app/src/main/jniLibs/x86_64/include )
add_library( native-lib # Sets the name of the library.
SHARED # Sets the library as a shared library.
native-lib.cpp # Provides a relative path to your source file(s).
)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( log-lib # Sets the name of the path variable.
log # Specifies the name of the NDK library that
) # you want CMake to locate.
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( native-lib # Specifies the target library.
zmq # Links the target library to the log library
${log-lib} # included in the NDK.
)
我已将此添加到本机lib.cpp中:
// libzmq
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
int testlibzmq (void)
{
// Socket to talk to clients
void *context = zmq_ctx_new ();
void *responder = zmq_socket (context, ZMQ_REP);
int rc = zmq_bind (responder, "tcp://*:5555");
assert (rc == 0);
char buffer [10];
zmq_recv (responder, buffer, 10, 0);
printf ("Received Hello\n");
sleep (1); // Do some 'work'
zmq_send (responder, "World", 5, 0);
return 0;
}
我是否必须在我的jniLibs文件夹旁边生成一个Android.mk
和Application.mk
文件?是否有人试图在本机代码中使用libzmq
,而没有jzmq
或Jeromq
构建链接共享本机库的stuck
:
Entering directory `/home/hmi/barepo/AndroidCppZmq/app/.cxx/cmake/debug/x86_64'
[1/1] Linking CXX shared library /home/hmi/barepo/AndroidCppZmq/app/build/intermediates/cmake/debug/obj/x86_64/libnative-lib.so
FAILED: /home/hmi/barepo/AndroidCppZmq/app/build/intermediates/cmake/debug/obj/x86_64/libnative-lib.so