尝试为Android构建ZXing(本机)时CMake脚本引发错误

时间:2018-09-07 03:24:40

标签: android c++ cmake android-ndk zxing

我正在尝试在Android Studio中从其C++ source code构建ZXing。但是CMake会引发很多错误,而我对NDK的开发经验不足,而且错误的描述性不足以理解,无法弄清这里出了什么问题。现在,我面临以下问题;

CMake Error at CMakeLists.txt:90 (add_executable):
CMake Error at CMakeLists.txt:91 (add_executable):
CMake Error at CMakeLists.txt:92 (target_link_libraries):

我试图通读CMake docs来理解命令的语法,它们在CMakeLists.txt(下面列出)中看起来还不错。

请问有人对这些原生语言有经验吗?

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)

project(zxing)

option(BUILD_TESTING "Enable generation of test targets" OFF)

set(CMAKE_LIBRARY_PATH /opt/local/lib ${CMAKE_LIBRARY_PATH})
set(OpenCV_DIR C:/opencv-3.4.3-android-sdk/OpenCV-android-sdk/sdk/native/jni)
# set(Iconv_DIR C:/Users/bcs/Downloads/libiconv-1.9.2-1-bin)


# Check for polluted source tree.
if(EXISTS ${CMAKE_SOURCE_DIR}/CMakeCache.txt OR
    EXISTS ${CMAKE_SOURCE_DIR}/CMakeFiles)
    message(FATAL_ERROR
        "Source directory is polluted:"
        "\n  * remove CMakeCache.txt"
        "\n  * remove CMakeFiles directory")
endif()

# Suppress in-source builds.
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
    message(FATAL_ERROR
        "CMake generation is not allowed within the source directory:"
        "\n  * mkdir build"
        "\n  * cd build"
        "\n  * Unix-like: cmake -G \"Unix Makefiles\" .."
        "\n  * Windows: cmake -G \"Visual Studio 10\" ..")
endif()

# Adjust CMake's module path.
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/FindModules)

# Suppress MSVC CRT warnings.
if(MSVC)
    add_definitions(-D_CRT_SECURE_NO_WARNINGS)
    add_definitions(/Za)
    add_definitions(/MP) # multi-core builds
endif()

include(source_files.cmake)

if(WIN32)
    include_directories(core/lib/win32)
    set(CMAKE_DEBUG_POSTFIX -debug)
endif()

# OpenCV classes
find_package(OpenCV)
if(OpenCV_FOUND)
    list(APPEND LIBZXING_FILES
        opencv/src/zxing/MatSource.cpp
        opencv/src/zxing/MatSource.h
    )

    include_directories(${OpenCV_INCLUDE_DIRS})
    include_directories(opencv/src)
endif()

include_directories(core/src)
add_library(libzxing ${LIBZXING_FILES})
set_target_properties(libzxing PROPERTIES PREFIX "")

find_package(Iconv)
if(ICONV_FOUND)
    include_directories(${ICONV_INCLUDE_DIR})
    target_link_libraries(libzxing ${ICONV_LIBRARIES})
 else()
   add_definitions(-DNO_ICONV=1)
 endif()
    add_definitions(-DNO_ICONV=1)

# Add OpenCV cli executable
if(OpenCV_FOUND)
    file(GLOB_RECURSE OPENCV_ZXING_FILES
        "./opencv-cli/src/*.cpp"
        "./opencv-cli/src/*.h"
    )
    # add_executable(zxing-cv ${OPENCV_ZXING_FILES})
    add_executable(zxing-cv "./opencv-cli/src/*.cpp")
    target_link_libraries(zxing-cv libzxing ${OpenCV_LIBRARIES})
endif()

# Add cli executable.
file(GLOB_RECURSE ZXING_FILES
    "./cli/src/*.cpp"
    "./cli/src/*.h"
)

 add_executable(zxing ${ZXING_FILES})
 add_executable(zxing-cv "./opencv-cli/src/*.cpp")
 target_link_libraries(zxing libzxing)

install(TARGETS zxing libzxing EXPORT zxing-targets
    LIBRARY DESTINATION lib
    RUNTIME DESTINATION bin
    ARCHIVE DESTINATION lib
    INCLUDES DESTINATION include
)

install(EXPORT zxing-targets DESTINATION lib/zxing/cmake NAMESPACE zxing::)

install(
    DIRECTORY core/src/zxing/
    DESTINATION include/zxing
    FILES_MATCHING PATTERN "*.h"
)

if(OpenCV_FOUND)
    install(
        DIRECTORY opencv/src/zxing/
        DESTINATION include/zxing
        FILES_MATCHING PATTERN "*.h"
    )
endif()

configure_file(cmake/zxing-config.cmake.in zxing-config.cmake @ONLY)
install(FILES ${CMAKE_BINARY_DIR}/zxing-config.cmake DESTINATION lib/zxing/cmake)

if(BUILD_TESTING)
    # Add testrunner executable.
    find_package(CPPUNIT)
    if(CPPUNIT_FOUND)
        file(GLOB_RECURSE TESTRUNNER_FILES
            "./core/tests/src/*.cpp"
            "./core/tests/src/*.h"
        )
        add_executable(testrunner ${TESTRUNNER_FILES})
        include_directories(${CPPUNIT_INCLUDE_DIR})
        target_link_libraries(testrunner libzxing  ${CPPUNIT_LIBRARIES})
    endif()
endif()

1 个答案:

答案 0 :(得分:1)

您可以切换到nu-book / zxing-cpp

ZXing到C ++的各个端口之间存在相当多的混淆。从最近开始,nu-book/zxing-cpp被认为类似于this discussion之后的“官方” ZXing C ++端口。

根据我自己的估计,这是当前端口中最活跃,最先进的端口。它与您尝试使用的端口glassechidna/zxing-cpp不同,它是完全在C ++中重新实现的,而不是从原始ZXing Java API到C ++的直接1:1端口。那应该在C ++环境中将名称等命名为“ nicer”。

建立它

我在构建nu-book / zxing-cpp并将其在应用程序中使用时没有重大问题(尽管我尚未在Android上尝试过)。我没有使用Android Studio来构建它,而是使用了控制台,如下所示:

cd /path/to/zxing-cpp/sources && mkdir build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/path/to/zxing-cpp/sources/export
make
make install

此后,您可以在配置了cmake .. -DCMAKE_PREFIX_PATH=/path/to/zxing-cpp/sources/export/lib/cmake/ZXing并将其包含在CMakeLists.txt中的基于CMake的项目中使用它:

find_package(ZXing REQUIRED)
add_executable( ... )
target_link_libraries(minimal-zxing-demo ZXing::ZXing)