简体   繁体   English

在Cmake中运行自定义命令

[英]Running a custom command in Cmake

I am very new to Cmake and need to generate some files at compile time. 我是Cmake的新手,需要在编译时生成一些文件。 once generated i need to compile and link the files. 一旦生成我需要编译和链接文件。 I ve created the cmake makefile to compile the already generated files like 我创建了cmake makefile来编译已生成的文件,如

cmake_minimum_required(VERSION 2.6)
project(demo)
set(CMAKE_CXX_FLAGS "-DWITH_COOKIES")
add_library(soapC soapC.cpp soapVimBindingProxy.cpp)
add_library(stdsoap2  /home/abdullah/installs/gsoap-shah_edits/gsoap/stdsoap2.cpp)
add_executable(demo test_file.cc test_app.cc)
target_link_libraries(demo soapC stdsoap2 gsoap++)

This successfully compiles the project. 这成功编译了该项目。 However the files soapC.cpp soapVimBindingProxy.cpp needs to be generated first. 但是, soapC.cpp soapVimBindingProxy.cpp需要生成文件soapC.cpp soapVimBindingProxy.cpp And I want to generate these files at runtime using the gsoap tool. 我想在运行时使用gsoap工具生成这些文件。

following is the command that needs to be run to generate the header file 以下是需要运行以生成头文件的命令

wsdl2h -o outfile.h infile.wsdl

This takes an input wsdl file and creates a corresponding outfile.h. 这将获取输入wsdl文件并创建相应的outfile.h。 Now I tried doing this in cmake like this 现在我尝试在这样的cmake中这样做

cmake_minimum_required(VERSION 2.6)
add_custom_command(
OUTPUT vsphere.h
COMMAND wsdl2h -o vsphere.h vim25/vim.wsdl
) 

But something goes wrong here. 但这里出了点问题。 No error pops up but no file is created either. 弹出没有错误,但也没有创建文件。 Am I missing something ? 我错过了什么吗? All help much appreciated. 所有人都非常感谢。 Thanks. 谢谢。

You've just created a command for producing your header file, so CMake knows just where to get vsphere.h from. 您刚刚创建了一个用于生成头文件的命令,因此CMake知道从哪里获取vsphere.h I'd recommend using OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/vsphere.h in the add_custom_command() call. 我建议在add_custom_command()调用中使用OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/vsphere.h

Now you need to create a target: 现在您需要创建一个目标:

add_custom_target(vsphere_header ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/vsphere.h)

Finally, make your soapC target depend on it: 最后,让你的soapC目标依赖于它:

add_dependencies(soapC vsphere_header)

Be sure to place add_dependencies() call after soapC target definition. 确保在soapC目标定义之后放置add_dependencies()调用。

Thanks arrododger and TobyHijzen for your lighting on this issue. 感谢arrododger和TobyHijzen在这个问题上的照明。

I use add_custom_command with main_dependency feature for solution for this issue. 我使用带有main_dependency功能的add_custom_command来解决此问题。 Following is my CMakeLists.txt for famous calc example of gsoap tutorial. 以下是我的CMakeLists.txt,用于gsoap教程的着名计算示例。

cmake_minimum_required(VERSION 2.8)

# Proejct name
PROJECT(Calculator)

# Make verbose level on/off
SET(CMAKE_VERBOSE_MAKEFILE ON)

# Varialbes used in cmake
SET(TARGET calc_client)
SET(GSOAP_STATIC_LIB gsoap)

SET(CLIENT_SRC calc_client.c)

SET(WSDL2H_EXEC wsdl2h)
SET(WSDL2H_IN http://www.genivia.com/calc.wsdl)
#SET(WSDL2H_IN calc.wsdl)
SET(WSDL2H_OUT calc.h)
SET(WSDL2H_OPT -c -o)

# command for generating stub and xml serializer code
ADD_CUSTOM_COMMAND(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${WSDL2H_OUT}
    COMMAND ${WSDL2H_EXEC} -c -ttypemap.dat -o ${WSDL2H_OUT} ${WSDL2H_IN}
    COMMENT "=================== Generating ${WSDL2H_OUT} gSOAP Header file ..."
)

SET(SOAPCPP2_EXEC soapcpp2)
SET(STUB soapClient.c soapC.c)

SET(SOAPCPP2_OUT
    ${STUB}
    calc.add.req.xml
    calc.add.res.xml
    calc.sub.req.xml
    calc.sub.res.xml
    calc.mul.req.xml
    calc.mul.res.xml
    calc.div.res.xml
    calc.div.req.xml
    calc.pow.res.xml
    calc.pow.req.xml
    calc.nsmap
    soapH.c
    soapH.h
    soapStub.h
    soapClientLib.c
)
# command for generating stub and xml serializer code
ADD_CUSTOM_COMMAND(
    OUTPUT ${STUB}
    COMMAND ${SOAPCPP2_EXEC} -c -C ${WSDL2H_OUT}
    MAIN_DEPENDENCY ${WSDL2H_OUT}
    COMMENT "=================== Generating ${STUB} outputs ..."
)


# Exutable files and dependents
ADD_EXECUTABLE(${TARGET} ${CLIENT_SRC} ${STUB})

# libraries for taget : for gcc -l option
TARGET_LINK_LIBRARIES(${TARGET} ${GSOAP_STATIC_LIB})

# Compiler options
ADD_DEFINITIONS(-Wall -O2 -s)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM