Invoke MIDL compiler from CMAKE

拟墨画扇 提交于 2020-11-28 01:54:27

问题


I am going to use MIDL compiler with CMAKE but unable to invoke MIDL compiler from CmakeList

That is command which I use to achieve my goal

add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/IFace.h ${CMAKE_CURRENT_BINARY_DIR}/GUIDS.c ${CMAKE_CURRENT_BINARY_DIR}/Proxy.c ${CMAKE_CURRENT_BINARY_DIR}/ProxyDll.c
COMMAND midl /h IFace.h /iid GUIDS.c /proxy Proxy.c /dlldata ProxyDll.c ${CMAKE_CURRENT_LIST_DIR}/include/Server.idl
)

When I build my project there are no files produced by MIDL compiler in ${CMAKE_CURRENT_BINARY_DIR}

But with this signature files generates well

add_custom_command(
PRE_BUILD
TARGET ${PROJECT_NAME}
COMMAND midl /h IFace.h /iid GUIDS.c /proxy Proxy.c /dlldata ProxyDll.c ${CMAKE_CURRENT_LIST_DIR}/include/Server.idl
)

What am I doing wrong?


回答1:


That is how it works:

set(MIDL_OUTPUT
    ${CMAKE_CURRENT_BINARY_DIR}/IFace.h
    ${CMAKE_CURRENT_BINARY_DIR}/GUIDS.c
    ${CMAKE_CURRENT_BINARY_DIR}/Proxy.c
    ${CMAKE_CURRENT_BINARY_DIR}/ProxyDll.c
    )
set(MIDL_FILE
    ${CMAKE_CURRENT_LIST_DIR}/include/Server.idl
    )
add_custom_command(
    OUTPUT ${MIDL_OUTPUT}
    COMMAND midl /h IFace.h /iid GUIDS.c /proxy Proxy.c /dlldata ProxyDll.c ${MIDL_FILE}
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    DEPENDS ${MIDL_FILE}
    VERBATIM
    )
add_custom_target(midl-cmplr
    DEPENDS ${MIDL_OUTPUT}
    )
add_dependencies(${PROJECT_NAME}
    midl-cmplr
    )
set_source_files_properties(
    ${MIDL_OUTPUT}
        PROPERTIES
            GENERATED TRUE
            )



回答2:


to improve upon @definename's answer and @nathan-moinvaziri's comment, it's not necessary to do add_custom_target as long as you add the output to your source files list, so the revised answer is:

set(MIDL_OUTPUT
    ${CMAKE_CURRENT_BINARY_DIR}/IFace.h
    ${CMAKE_CURRENT_BINARY_DIR}/GUIDS.c
    ${CMAKE_CURRENT_BINARY_DIR}/Proxy.c
    ${CMAKE_CURRENT_BINARY_DIR}/ProxyDll.c
)
set(MIDL_FILE
    ${CMAKE_CURRENT_LIST_DIR}/BookSku.idl
)
add_custom_command(
    OUTPUT ${MIDL_OUTPUT}
    COMMAND midl /h IFace.h /iid GUIDS.c /proxy Proxy.c /dlldata ProxyDll.c ${MIDL_FILE}
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    DEPENDS ${MIDL_FILE}
    MAIN_DEPENDENCY ${MIDL_FILE}
    VERBATIM
)
# add_custom_target(midl-cmplr
#     DEPENDS ${MIDL_OUTPUT}
# )
# add_dependencies(${PROJECT_NAME}
#     midl-cmplr
# )
# set_source_files_properties(${MIDL_OUTPUT} PROPERTIES
#   GENERATED TRUE
# )
target_sources(${PROJECT_NAME} PRIVATE ${MIDL_FILE} ${MIDL_OUTPUT})


来源:https://stackoverflow.com/questions/46878906/invoke-midl-compiler-from-cmake

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!