6

我对 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
) 

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

4

2 回答 2

9

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

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

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

最后,让你的soapC目标依赖它:

add_dependencies(soapC vsphere_header)

确保在目标定义add_dependencies()之后进行调用。soapC

于 2012-07-09T06:59:29.510 回答
3

感谢 arrododger 和 TobyHijzen 对这个问题的说明。

我使用带有 main_dependency 功能的 add_custom_command 来解决这个问题。以下是我的 CMakeLists.txt,用于 gsoap 教程的著名 calc 示例。

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)
于 2015-04-14T02:17:16.043 回答