代码之家  ›  专栏  ›  技术社区  ›  Superlokkus

添加依赖项时,Unix make file与cmakejs一起失败

  •  0
  • Superlokkus  · 技术社区  · 5 年前

    我用napi和cmakejs成功地创建了一个本机节点插件。但是当添加一个简单的库时,由cmake js生成的unix make文件会爆炸

    [50%]链接CXX静态库lib\u name.a [50%]生成的目标库名称 CMAKE文件/spielwiese.dir/标志.品牌:10: *缺少分隔符。停下。 制造商[1]:* [CMakeFiles/Makefile2:72:CMakeFiles文件/spielwiese.dir/全部]错误2 品牌:**[生成文件:130:all]错误2 呃!OMG进程终止:2

    最小化示例项目: https://github.com/Superlokkus/spielwiese/tree/napi

    根CMakeLists应该接近或打算接近cmakejs示例样板版本,只需使用一个附加函数 PARSE_CMAKEJS_PROPERTIES 也可以通过cmakecli命令来构建它,以便更好地使用CLion之类的ide进行开发。但是,当移除 解析\u CMAKEJS \u属性 功能。

    我给图书馆加了一个 add_subdirectory ,如果删除 https://github.com/Superlokkus/spielwiese/blob/napi/CMakeLists.txt#L47 又称作 add_subdirectory(src/lib_name) https://github.com/Superlokkus/spielwiese/blob/napi/CMakeLists.txt#L63 又称作 target_link_libraries(${PROJECT_NAME} PUBLIC ${CMAKE_JS_LIB} lib_name) target_link_libraries(${PROJECT_NAME} PUBLIC ${CMAKE_JS_LIB}) 同时移除 https://github.com/Superlokkus/spielwiese/blob/napi/src/spielwiese.cpp#L3 又称作 #include <lib_name/lib_name.hpp>

    该项目再次建立包括摩卡测试。但是,如果添加示例库,则会再次出现错误。

    根目录列表:

    cmake_minimum_required(VERSION 3.2)
    
    # The following function is just for nice CLion IDE support with cmake-js
    function(PARSE_CMAKEJS_PROPERTIES)
        function(GET_VARIABLE INPUT_STRING VARIABLE_TO_SELECT OUTPUT_VARIABLE)
            set(SEARCH_STRING "${VARIABLE_TO_SELECT}=\"")
            string(LENGTH "${SEARCH_STRING}" SEARCH_STRING_LENGTH)
            string(LENGTH "${INPUT_STRING}" INPUT_STRING_LENGTH)
    
            string(FIND "${INPUT_STRING}" "${VARIABLE_TO_SELECT}=\"" SEARCH_STRING_INDEX)
    
            math(EXPR SEARCH_STRING_INDEX "${SEARCH_STRING_INDEX}+${SEARCH_STRING_LENGTH}")
    
            string(SUBSTRING "${INPUT_STRING}" ${SEARCH_STRING_INDEX} ${INPUT_STRING_LENGTH} AFTER_SEARCH_STRING)
            string(FIND "${AFTER_SEARCH_STRING}" "\"" QUOTE_INDEX)
            string(SUBSTRING "${AFTER_SEARCH_STRING}" "0" "${QUOTE_INDEX}" RESULT_STRING)
            set("${OUTPUT_VARIABLE}" "${RESULT_STRING}" PARENT_SCOPE)
        endfunction(GET_VARIABLE)
    
        string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
        if (CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
            exec_program(./node_modules/.bin/cmake-js ${CMAKE_CURRENT_SOURCE_DIR}
                    ARGS print-configure --debug
                    OUTPUT_VARIABLE CMAKE_JS_OUTPUT
                    )
        else ()
            exec_program(./node_modules/.bin/cmake-js ${CMAKE_CURRENT_SOURCE_DIR}
                    ARGS print-configure
                    OUTPUT_VARIABLE CMAKE_JS_OUTPUT
                    )
        endif ()
    
        get_variable("${CMAKE_JS_OUTPUT}" "CMAKE_JS_INC" CMAKE_JS_INC)
        set(CMAKE_JS_INC "${CMAKE_JS_INC}" PARENT_SCOPE)
    
        get_variable("${CMAKE_JS_OUTPUT}" "CMAKE_LIBRARY_OUTPUT_DIRECTORY" CMAKE_LIBRARY_OUTPUT_DIRECTORY)
        set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}" PARENT_SCOPE)
    
    endfunction(PARSE_CMAKEJS_PROPERTIES)
    
    # Name of the project (will be the name of the plugin)
    project(spielwiese VERSION 1.0)
    if (NOT CMAKE_JS_INC)
        parse_cmakejs_properties()
    endif ()
    
    add_subdirectory(src/lib_name)
    
    # Essential include files to build a node addon,
    # you should add this line in every CMake.js based project.
    include_directories(${CMAKE_JS_INC})
    
    # Declare the location of the source files
    file(GLOB SOURCE_FILES "src/*.cpp")
    
    add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
    
    # This line will give our library file a .node extension without any "lib" prefix
    set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
    
    # Essential library files to link to a node addon,
    # you should add this line in every CMake.js based project.
    target_link_libraries(${PROJECT_NAME} PUBLIC ${CMAKE_JS_LIB} lib_name)
    
    # Include N-API wrappers
    execute_process(COMMAND node -p "require('node-addon-api').include"
            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
            OUTPUT_VARIABLE NODE_ADDON_API_DIR
            )
    string(REPLACE "\"" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
    target_include_directories(${PROJECT_NAME} PRIVATE ${NODE_ADDON_API_DIR})
    

    Libnames和CMakeLists

    cmake_minimum_required(VERSION 3.0)
    project(lib_name VERSION 1.0)
    
    string(TOUPPER ${PROJECT_NAME} PROJECT_NAME_UPPER_CASE)
    configure_file(
            ${PROJECT_NAME}_version.hpp.in
            ${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}/${PROJECT_NAME}_version.hpp
    )
    
    set(${PROJECT_NAME}_implementation_files
            src/lib.cpp
            )
    
    
    add_library(${PROJECT_NAME} ${${PROJECT_NAME}_implementation_files})
    set_property(TARGET ${PROJECT_NAME} PROPERTY LINKER_LANGUAGE CXX)
    set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17)
    
    target_include_directories(${PROJECT_NAME}
            PUBLIC
            $<INSTALL_INTERFACE:include>
            $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
            PRIVATE
            ${CMAKE_CURRENT_SOURCE_DIR}/src
            )
    
    install(TARGETS ${PROJECT_NAME}
            ARCHIVE
            DESTINATION lib)
    install(TARGETS ${PROJECT_NAME}
            PUBLIC_HEADER
            DESTINATION include)
    

    更新 我做了一些实验

    target_include_directories(${PROJECT_NAME}
            PUBLIC
            $<INSTALL_INTERFACE:include>
            $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
            PRIVATE
            ${CMAKE_CURRENT_SOURCE_DIR}/src
            )
    

    正在处理问题,所以看起来cmakejs对该命令有问题?!

    0 回复  |  直到 5 年前
        1
  •  0
  •   Superlokkus    5 年前

    原来是从cmake js官方文档中提取的片段

    execute_process(COMMAND node -p "require('node-addon-api').include"
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        OUTPUT_VARIABLE NODE_ADDON_API_DIR
        )
    

    # Include N-API wrappers
    execute_process(COMMAND node -p "require('node-addon-api').include"
            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
            OUTPUT_VARIABLE NODE_ADDON_API_DIR
            )
    string(REPLACE "\n" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
    string(REPLACE "\"" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
    target_include_directories(${PROJECT_NAME} PRIVATE ${NODE_ADDON_API_DIR})
    

    所以增加了 string(REPLACE "\n" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR}) 为了摆脱新线修正了这个问题。

    谢谢@fred帮我找到问题

    已经创建了一个pull请求来修复cmake js文档: https://github.com/cmake-js/cmake-js/issues/175

    推荐文章