包括 Boost C++安卓中的库

发布于 2024-12-11 21:14:58 字数 2036 浏览 0 评论 0原文

很长一段时间以来,我一直试图在 Windows 上结合 Boost 和 Android,并尝试了很多方法,但仍然没有成功。我想在android中使用Boost库制作一个示例程序。我正在此处关注本教程。< /a>

正如本教程所建议的,我将 Boost 库保存在 ****(Android NDK)\sources\boost_1_44_0**** 中,并成功编译了它。

然后我在 sources/boost_1_44_0 中创建了一个 Android.mk 文件,并创建了我想要使用的每个库的条目。在这种情况下,lib.文件是 libboost_date_time-gcc-mt-s-1_44.a,位于 boost_1_44_0/android/lib/
这是Android.mk文件的内容。

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_date
LOCAL_SRC_FILES:= boost_1_44_0/android/lib/libboost_date_time-gcc-mt-s-1_44.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY) 

现在下一步是在我的项目目录中的 jni 文件夹中创建一个 Android.mk 文件。(这是为了创建一个共享库。)。以下是其内容。

LOCAL_PATH := $(call my-dir)
 include $(call all-subdir-makefiles)

include $(CLEAR_VARS)

# Here we give our module name and source file(s)
LOCAL_LDLIBS := -llog -ldl
LOCAL_MODULE    := ndkfoo
LOCAL_SRC_FILES := ndkfoo.cpp
LOCAL_STATIC_LIBRARIES := boost_date
include $(BUILD_SHARED_LIBRARY)
$(call import-module,boost_1_44_0) 

这是位于 jni 文件夹内同一位置的 Application.mk 文件。 Application.mk 文件的内容如下:

APP_STL      = gnustl_static #(or APP_STL = stlport_static as required)
APP_CPPFLAGS = -fexceptions  

最后是我的 ndkfoo.cpp 文件,

#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <boost/date_time.hpp>

using namespace boost::gregorian;

void Java_com_ndkfoo_NdkfooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
    date weekstart(2002,Feb,1);

}

该程序可能不正确,但问题是它无法识别任何 boost 标头或函数。我总是收到编译错误。

我是否遗漏或做错了什么?任何帮助将不胜感激。

编辑: 这个问题包含了在 android 中包含 Boost 库所需的一切。有关更多提示,请查看我下面的回答。希望这也适合您。

谢谢。

I have been trying to marry Boost and android on windows for long time and tried lot of approaches but still no luck. I want to make a sample program using Boost library in android. I am following this tutorial here.

As this tutorial suggested i have kept my Boost lib in ****(Android NDK)\sources\boost_1_44_0**** compiled it successfully.

Then i made an Android.mk file inside sources/boost_1_44_0 and made the entry of each library which i want to use. In this case lib. file is libboost_date_time-gcc-mt-s-1_44.a available in boost_1_44_0/android/lib/
Here is the content of Android.mk file.

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_date
LOCAL_SRC_FILES:= boost_1_44_0/android/lib/libboost_date_time-gcc-mt-s-1_44.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY) 

Now the next step is to make an Android.mk file in my project directory, inside jni folder.(this is to create a shared library.). Here are its contents.

LOCAL_PATH := $(call my-dir)
 include $(call all-subdir-makefiles)

include $(CLEAR_VARS)

# Here we give our module name and source file(s)
LOCAL_LDLIBS := -llog -ldl
LOCAL_MODULE    := ndkfoo
LOCAL_SRC_FILES := ndkfoo.cpp
LOCAL_STATIC_LIBRARIES := boost_date
include $(BUILD_SHARED_LIBRARY)
$(call import-module,boost_1_44_0) 

Here is the Application.mk file placed on the same location, inside jni folder. Contents of Application.mk file are as follows:

APP_STL      = gnustl_static #(or APP_STL = stlport_static as required)
APP_CPPFLAGS = -fexceptions  

And finally here is my ndkfoo.cpp file

#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <boost/date_time.hpp>

using namespace boost::gregorian;

void Java_com_ndkfoo_NdkfooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
    date weekstart(2002,Feb,1);

}

this program might be incorrect but the problem is that it does not recognize any boost headers or function. and i always get compilation error.

Is there something i am missing or doing incorrectly? Any help would be really appreciated.

EDIT: This question contains everything you would need to include Boost library in android. For more tips look at my answer below. Hopefully this would also work for you.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑咖 2024-12-18 21:14:58

我的问题包含在 Android 中包含 BOOST 库的几乎完整步骤。但在处理此问题时,您仍然应该记住一些重要的要点。

  • 每次编译本机代码之前,请删除自动生成的 objlib 文件夹。

  • 如果您要使用 C++ 编写本机代码,请将 LOCAL_CPP_EXTENSION := .cpp 添加到您的 Android.mk(jni/Android.mk) 文件中。

  • 如果您要使用 C++ 进行编码,请将所有 cpp 代码放入 extern "C" {} 中。

    extern C { /*cpp code*/ }

My question contained almost complete steps for including BOOST library in Android. But still there are some important points you should remember while working with this.

  • Delete auto generated obj and libs folder Each time before you compile your native code.

  • If you are going to write your native code in C++, add LOCAL_CPP_EXTENSION := .cpp to your Android.mk(jni/Android.mk) file.

  • if you are going to code in C++, put your all cpp code inside extern "C" {}.

    extern C { /*cpp code*/ }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文