首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复CERES_USE_OPENMP、CERES_USE_CXX11_THREADS或CERES_NO_THREADS中必须在Ceres Solver Android中定义的错误

如何修复CERES_USE_OPENMP、CERES_USE_CXX11_THREADS或CERES_NO_THREADS中必须在Ceres Solver Android中定义的错误
EN

Stack Overflow用户
提问于 2019-10-09 19:09:27
回答 1查看 622关注 0票数 3

我正在我的Android应用程序中集成Ceres Solver Library。我已经使用Android Studio中的CMakeLists.txt为所有架构创建了预先构建的共享库(.so文件)。现在我想用OpenCV java包装器在Java/Kotlin中实现包调整。

要在安卓应用程序中使用Ceres Solver,我们必须使用ceres solver及其依赖库的.so文件和头文件在C++中编写Ceres求解器逻辑。然后,我们必须将包装器写到我们自己的方法中,以便在Java / Kotlin中使用。

为此,我使用以下CMakeLists.txt文件

代码语言:javascript
复制
# 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)

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             native-lib.cpp )


find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )


include_directories( C:/eigen-eigen-323c052e1731 )
include_directories( C:/My_Data/Ceres-Solver-Builder/ceres-solver/include )
include_directories( C:/My_Data/Ceres-Solver-Builder/ceres-solver/config )
include_directories( C:/My_Data/Ceres-Solver-Builder/ceres-solver/internal/ceres/miniglog )
include_directories( C:/My_Data/Ceres-Solver-Builder/ceres-solver/internal )

add_library( ceres-lib SHARED IMPORTED)
set_target_properties( # Specifies the target library.
        ceres-lib

        # Specifies the parameter you want to define.
        PROPERTIES IMPORTED_LOCATION

        # Provides the path to the library you want to import.
        C:/My_Data/AS_Workspace/JNIApplication/app/src/main/jniLibs/${ANDROID_ABI}/libceres.so )
target_link_libraries( native-lib ceres-lib ${log-lib})

使用Ceres Solver类和方法的示例C++文件

代码语言:javascript
复制
#include <jni.h>
#include <string>
#include <bitset>

#include "ceres/ceres.h"
#include "glog/logging.h"

using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::Problem;
using ceres::Solver;
using ceres::Solve;
int test();

extern "C" JNIEXPORT jstring JNICALL
Java_com_trimble_jniapplication_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";

    test();

    return env->NewStringUTF(hello.c_str());
}



// A templated cost functor that implements the residual r = 10 -
// x. The method operator() is templated so that we can then use an
// automatic differentiation wrapper around it to generate its
// derivatives.
struct CostFunctor {
    template<typename T>
    bool operator()(const T *const x, T *residual) const {
        residual[0] = 10.0 - x[0];
        return true;
    }
};

int test() {
    //google::InitGoogleLogging(argv[0]);

    // The variable to solve for with its initial value. It will be
    // mutated in place by the solver.
    double x = 0.5;
    const double initial_x = x;

    // Build the problem.
    Problem problem;

    // Set up the only cost function (also known as residual). This uses
    // auto-differentiation to obtain the derivative (jacobian).
    CostFunction *cost_function = new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
    problem.AddResidualBlock(cost_function, NULL, &x);

    // Run the solver!
    Solver::Options options;
    options.minimizer_progress_to_stdout = true;
    Solver::Summary summary;
    Solve(options, &problem, &summary);

    std::cout << summary.BriefReport() << "\n";
    std::cout << "x : " << initial_x
              << " -> " << x << "\n";
    return 0;
}

当我构建应用程序时,我得到了以下错误。

代码语言:javascript
复制
error One of CERES_USE_OPENMP, CERES_USE_CXX11_THREADS or CERES_NO_THREADS must be defined.

我刚接触NDK、Cmake和Ceres Solver,所以我不知道如何解决这个问题。请有人建议我如何解决这个问题,或者指出我哪里做错了。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2019-10-09 19:43:12

如上所述,在使用Ceres时,您至少需要定义这些预处理器定义中的一个(请参阅逻辑here)。您可以使用target_compile_definitions()通过CMake将定义添加到编译中

代码语言:javascript
复制
add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             native-lib.cpp )

target_compile_definitions(native-lib PRIVATE CERES_USE_CXX11_THREADS=1)

...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58302815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档