在Cmake中运行自定义命令

时间:2012-07-09 06:49:30

标签: makefile cmake

我是Cmake的新手,需要在编译时生成一些文件。一旦生成我需要编译和链接文件。我创建了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++)

这成功编译了该项目。但是,需要首先生成文件soapC.cpp soapVimBindingProxy.cpp。我想在运行时使用gsoap工具生成这些文件。

以下是需要运行以生成头文件的命令

wsdl2h -o outfile.h infile.wsdl

这将获取输入wsdl文件并创建相应的outfile.h。 现在我尝试在这样的cmake中这样做

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

但这里出了点问题。弹出没有错误,但也没有创建文件。我错过了什么吗?所有人都非常感谢。 感谢。

2 个答案:

答案 0 :(得分:7)

您刚刚创建了一个用于生成头文件的命令,因此CMake知道从哪里获取vsphere.h。我建议在OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/vsphere.h来电中使用add_custom_command()

现在你需要创建一个目标:

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

最后,让你的soapC目标取决于它:

add_dependencies(soapC vsphere_header)

请务必在add_dependencies()目标定义后发出soapC来电。

答案 1 :(得分:1)

感谢arrododger和TobyHijzen为您解决这个问题。

我使用带有main_dependency功能的add_custom_command来解决此问题。以下是我的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)