美文网首页
Cygwin 在 win 中编译 cpp 源码软件

Cygwin 在 win 中编译 cpp 源码软件

作者: 小潤澤 | 来源:发表于2023-12-24 15:42 被阅读0次

    这个软件的作用是将只能在linux上编译的软件用于在window上编译

    下载 Cygwin

    从官网上下载:https://www.cygwin.com/

    每一次安装编译新软件都需要点击 setup.exe,如下图所示



    这一页选择从互联网下载动态库


    选择下载的文件夹,一路点下一步


    这里选择下载源,我选的是阿里云的


    这个页面选择要下载的动态库,通常去待安装的源码种查看依赖库,并从这个页面下载,如果需要再次下载,则点击 Cygwin 的setup.exe来到这个页面,再次选择要下载的动态库
    选择下载完毕后,点击下一步进行安装即可安装库完毕以后,重启一下Cygwin

    以编译fastANI为例

    https://github.com/ParBLiSS/FastANI下载源码,并且放入 Cygwin 的目录下(与bin同级),解压缩到当前目录,然后查看 INSTALL.txt 的安装说明

    mkdir build 
    cd build
    cmake .. -DCMAKE_BUILD_TYPE=Release
    cmake --build .
    

    如果在cmake的时候有缺少库报错,缺少哪一个则安装哪一个就可以,这里对CMakeLists.txt文件做了些许改动,

    cmake_minimum_required(VERSION 3.20)
    project(FastANI
        VERSION 1.34
        DESCRIPTION "Fast computation of whole-genome Average Nucleotide Identity (ANI)."
        LANGUAGES CXX)
    #
    # Main executable
    add_executable(fastANI src/cgi/core_genome_identity.cpp src/cgi/main.cpp)
    target_include_directories(fastANI PUBLIC "src/")
    #
    # by default, static linking
    option(GSL_SHARED "Build using shared libraries" ON)
    option(BUILD_TESTING "Build tests" ON) # build tests
    #
    # zlib dependency
    find_package(ZLIB 1.1 REQUIRED)
    if(${ZLIB_FOUND})
        message("Using zlib library v${ZLIB}")
    endif(${ZLIB_FOUND})
    target_link_libraries(fastANI PUBLIC ZLIB::ZLIB)
    #
    # openmp dependency
    find_package(OpenMP)
    if(${OpenMP_CXX_FOUND})
        message("Using OpenMP_CXX v${OpenMP_CXX_LIBRARIES}")
        message("Using OpenMP_CXX v${OpenMP_CXX_VERSION}")
        target_link_libraries(fastANI PUBLIC ${OpenMP_CXX_LIBRARIES})
        target_include_directories(fastANI PUBLIC ${OpenMP_CXX_INCLUDE_DIRS})
        target_compile_options(fastANI PUBLIC ${OpenMP_CXX_FLAGS})
    else()
        message("NOTE: OpenMP not found; Compiling as a single threaded app...")
    endif(${OpenMP_CXX_FOUND})
    #
    # GSL or Boost math
    find_package(GSL 1.6)
    if(${GSL_FOUND})
        message("GSL v${GSL_VERSION} Found: ${GSL_LIBDIR}")
        if(${GSL_SHARED})
            target_link_libraries(fastANI PUBLIC ${GSL_LIBRARIES})
        else()
            # GSL_LIBDIR is not documented
            target_link_libraries(fastANI PUBLIC
                    ${GSL_LIBDIR}/libgsl.a
                    ${GSL_LIBDIR}/libgslcblas.a)
        endif(${GSL_SHARED})
    
        target_include_directories(fastANI PUBLIC ${GSL_INCLUDE_DIRS})
    else()
        set(Boost_USE_STATIC_LIBS        ON)  # only find static libs
        set(Boost_USE_DEBUG_LIBS        OFF)  # ignore debug libs and
        set(Boost_USE_RELEASE_LIBS       ON)  # only find release libs
        set(Boost_USE_MULTITHREADED      ON)
        find_package(Boost 1.45 REQUIRED COMPONENTS math_c99)
        target_link_libraries(fastANI PUBLIC ${Boost_MATCH_C99_LIBRARY})
        target_include_directories(fastANI PUBLIC ${Boost_INCLUDE_DIRS})
        target_compile_definitions(fastANI PUBLIC USE_BOOST=1)
    endif(${GSL_FOUND})
    #
    #
    if(${BUILD_TESTING})
        add_subdirectory(ext/Catch2)
        include(CTest)
        include(Catch)
        add_executable(fastANITest tests/fastani_tests.cpp src/cgi/core_genome_identity.cpp)
        target_include_directories(fastANITest PUBLIC "src/"
                                ${OpenMP_CXX_INCLUDE_DIRS})
        target_compile_options(fastANITest PUBLIC
                --coverage -g -O0 -fprofile-arcs -ftest-coverage ${OpenMP_CXX_FLAGS})
        target_link_libraries(fastANITest PRIVATE
                              Catch2::Catch2 Catch2::Catch2WithMain
                              GSL::gsl GSL::gslcblas
                              ${OpenMP_CXX_LIBRARIES}
                              gcov)
    
        # 改动部分
        target_link_libraries(fastANI PUBLIC ZLIB::ZLIB)
        catch_discover_tests(fastANITest)
        file(COPY tests/data DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
        add_custom_target(lcov lcov -c -d .. -o fastANITest.out
                            COMMAND genhtml -o ../lcov/ fastANITest.out
                            DEPENDS fastANITest)
        add_custom_target(lcov2 lcov -c -d ./ -o fastANITest.info
                            COMMAND lcov -e fastANITest.info "*/FastANI/src/*/*.cpp" "*/FastANI/src/*/*.hpp"  > coverage.info
                            COMMAND lcov --list coverage.info |tee coverage_list.txt
                            COMMAND lcov --summary coverage.info |tee coverage_summary.txt
                            DEPENDS fastANITest)
    endif(${BUILD_TESTING})
    
    install(TARGETS fastANI DESTINATION bin)
    
    set(CPACK_GENERATOR "TGZ")
    set(CPACK_SOURCE_GENERATOR "TGZ")
    set(CPACK_PACKAGE_DIRECTORY ${PROJECT_BINARY_DIR}/package)
    
    set(CPACK_SOURCE_IGNORE_FILES .git/ .github/ 
        .vscode/ .cache/ .mypy_cache/
        .idea/ complie_comands.json
        lcov/ lcov0/ lcov2/ data/ cmake-build-debug/
        _CPack_Packages/ build.sh/ build/  build.clang/
        ${CMAKE_BINARY_DIR}/ ${PROJECT_BINARY_DIR}/)
    
    include(CPack)
    

    打开 cygwin 终端,然后按照INSTALL.txt进行编译和安装:

    mkdir build 
    cd build
    cmake .. -DCMAKE_BUILD_TYPE=Release [--install-prefix ...  -DGSL_SHARED=... -DGSL_ROOT_DIR=... -DBOOST_ROOT=...]
    cmake --build .
    

    就可以在 build 文件夹下生成 fastANI.exe文件了



    这里会有报错:



    缺少哪一个模块,则去 Cygwin/bin 中将缺少的文件复制到 fastANI中的build文件夹下即可,移动时模块与fastANI.exe要一起移动

    检查下效果:



    OK了

    相关文章

      网友评论

          本文标题:Cygwin 在 win 中编译 cpp 源码软件

          本文链接:https://www.haomeiwen.com/subject/nvhhndtx.html