本文介绍了如何包括生成的文件编译与cmake?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我在子目录xsd中有一组.xsd文件。我使用外部命令生成一组.hxx和cxx文件用于XML绑定。 add_custom_command( PRE_BUILD OUTPUT $ {PROJECT_SOURCE_DIR} /xsd/file1.hxx $ {PROJECT_SOURCE_DIR} /xsd/file1.cxx COMMAND $ {XSD_EXECUTABLE} cxx-tree --file-list-prologue'set (XSD_GENERATED_FILES' --file-list $ {PROJECT_SOURCE_DIR} /cmake_xsd_filelist.txt --file-list-epilogue')' --output-dir $ {PROJECT_SOURCE_DIR} / xsd $ {XSD_SOURCE_FILES} DEPENDS $ {XSD_SOURCE_FILES} ) 该工具创建生成的文件的列表。我有以下形式: set(XSD_GENERATED_FILES file1.cpp file1.hpp file2.cpp file2.hpp ) 这里。 现在的部分,我不知道如何继续。我想添加生成的文件列表并将其添加到可执行文件。 include($ {PROJECT_SOURCE_DIR} /cmake_xsd_filelist.txt) add_executable(Prog $ {SOURCE_FILES} $ {XSD_GENERATED_FILES}) 我想要的是。实际上,它没有什么,因为最初 cmake_xsd_filelist.txt 是空的。 我的目标是: 当'xsd'文件夹中的文件更改时,执行XSD_EXECUTABLE以生成.cpp和hpp文件。 只有在生成的.cpp文件发生更改时才会进行编译 将生成的源文件添加到可执行文件。 这是甚至可能与cmake?如果是,如何?解决方案 包括来源 include($ {PROJECT_SOURCE_DIR} /cmake_xsd_filelist.txt) add_executable(Prog $ {SOURCE_FILES} $ {XSD_GENERATED_FILES}) 这意味着您需要在配置步骤中有 cmake_xsd_filelist.txt 文件,即xsd必须由 execute_process 调用。我认为最好是生成文件到二进制目录(按照 out-of-source 概念): #如果需要,创建目录文件(MAKE_DIRECTORY$ {CMAKE_BINARY_DIR} / generated ) execute_process( COMMAND $ {XSD_EXECUTABLE} cxx-tree --file-list-prologue'set(XSD_GENERATED_FILES' --file -list$ {CMAKE_BINARY_DIR} /generated/cmake_xsd_filelist.txt --file-list-epilogue)' --output-dir$ {CMAKE_BINARY_DIR} / generated / xsd $ {XSD_SOURCE_FILES} RESULT_VARIABLE结果) 不要忘记检查successfull退出状态: if(NOT result EQUAL 0) message(FATAL_ERRORXSD failed) endif() 现在您可以包括来源: include($ {CMAKE_BINARY_DIR} /generated/cmake_xsd_filelist.txt) add_executable(Prog $ {SOURCE_FILES} $ {XSD_GENERATED_FILES}) XSD源更新 文件在xsd文件夹中更改,执行XSD_EXECUTABLE 生成.cpp和hpp文件。 如果 xsd 文件之一更改,CMake必须运行重新配置。这是帮助的诀窍: foreach(x $ {XSD_SOURCE_FILES}) configure_file($ {x} $ {x} COPYONLY) endforeach() 现在如果 XSD_SOURCE_FILES 更改CMake将重新配置项目,有效地重新运行 XSD_EXECUTABLE 并重新包含 cmake_xsd_filelist.txt I have a set of .xsd files in a subdirectory 'xsd'. I generate a set of .hxx and cxx files for XML-binding with an external command.add_custom_command(PRE_BUILDOUTPUT ${PROJECT_SOURCE_DIR}/xsd/file1.hxx ${PROJECT_SOURCE_DIR}/xsd/file1.cxxCOMMAND ${XSD_EXECUTABLE} cxx-tree --file-list-prologue 'set(XSD_GENERATED_FILES ' --file-list ${PROJECT_SOURCE_DIR}/cmake_xsd_filelist.txt --file-list-epilogue ')' --output-dir ${PROJECT_SOURCE_DIR}/xsd ${XSD_SOURCE_FILES}DEPENDS ${XSD_SOURCE_FILES})The tool creates a list of generated files. I have it in the following form:set(XSD_GENERATED_FILESfile1.cppfile1.hppfile2.cppfile2.hpp)Fine up to here.Now the part where I don't know how to continue. I want to include the generated file list and add them to the executable.include(${PROJECT_SOURCE_DIR}/cmake_xsd_filelist.txt)add_executable(Prog ${SOURCE_FILES} ${XSD_GENERATED_FILES})But this does not do what I want. Actually, it does nothing because initially cmake_xsd_filelist.txt is empty.My goal is the following:when a file in the 'xsd' folder changes, execute XSD_EXECUTABLE for generating the .cpp and hpp files.only compile once the generated .cpp files when they have changedadd the generated source files to the executable.Is this even possible with cmake? If yes, how? 解决方案 Include sourcesinclude(${PROJECT_SOURCE_DIR}/cmake_xsd_filelist.txt)add_executable(Prog ${SOURCE_FILES} ${XSD_GENERATED_FILES})This means that you need to have a cmake_xsd_filelist.txt file on configure step, i.e. xsd must be invoked by execute_process. Also I think it's better to generate files to binary directory (follow out-of-source concept):# Create directory if neededfile(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/generated")execute_process( COMMAND ${XSD_EXECUTABLE} cxx-tree --file-list-prologue 'set(XSD_GENERATED_FILES ' --file-list "${CMAKE_BINARY_DIR}/generated/cmake_xsd_filelist.txt" --file-list-epilogue ')' --output-dir "${CMAKE_BINARY_DIR}/generated/xsd" ${XSD_SOURCE_FILES} RESULT_VARIABLE result)Do not forget to check successfull exit status:if(NOT result EQUAL 0) message(FATAL_ERROR "XSD failed")endif()Now you can include sources:include(${CMAKE_BINARY_DIR}/generated/cmake_xsd_filelist.txt)add_executable(Prog ${SOURCE_FILES} ${XSD_GENERATED_FILES})XSD sources updatewhen a file in the 'xsd' folder changes, execute XSD_EXECUTABLEfor generating the .cpp and hpp files.I.e. CMake must run reconfigure if one of the xsd file changes. Here is the trick that helps:foreach(x ${XSD_SOURCE_FILES}) configure_file(${x} ${x} COPYONLY)endforeach()Now if any of the XSD_SOURCE_FILES changes CMake will reconfigure the project effectively re-running XSD_EXECUTABLE and re-including cmake_xsd_filelist.txt. 这篇关于如何包括生成的文件编译与cmake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 13:37