多项目结构下Meson custom_target生成源文件输出目录咨询
Let’s walk through how Meson manages output directories for files generated by custom_target, specifically tailored to your multi-project setup (main project + project_A/project_B subprojects):
Default Output Directory Behavior
By default, Meson automatically places all files generated by custom_target in a private, build-dir-specific subdirectory tied to the subproject where the target is defined. For your project_A setup:
- If your root build directory is
build/, the generated files (soapC.cpp,soapH.h, etc.) will live under a path likebuild/subprojects/project_A/[unique-target-identifier]/ - You don’t need to manually reference this exact path—Meson tracks the generated files as part of the
custom_targetobject, so you can use the object directly in your build rules.
Customizing the Output Directory
If you want to control where generated files are placed (e.g., grouping them in a dedicated generated/ folder), use the output_dir parameter in your custom_target definition. For example:
gen_empty_soap_src = custom_target( 'empty_soap_src', input : 'empty.gsoap', output : ['soapC.cpp', 'soapH.h', 'soapStub.h', 'soap.nsmap'], depend_files : soap_gen_depend_files, command : [soap_compiler, '-n', '-I', '.', '@INPUT@'], output_dir : meson.current_build_dir() / 'generated' )
meson.current_build_dir()points to the build directory corresponding to project_A’s source directory, so this will place the files inbuild/subprojects/project_A/generated/- Always use Meson’s built-in path functions (like
current_build_dir(),current_source_dir()) instead of hardcoded paths to maintain out-of-source build compatibility.
Referencing Generated Files Correctly
Within the Same Subproject (project_A)
You can directly pass the custom_target object to your library/executable sources—Meson handles resolving the correct file paths automatically:
# Example: Add generated files to a project_A library lib_project_a = library( 'project_a', sources : ['core.cpp'] + gen_empty_soap_src, # other build parameters... )
Across Subprojects (project_B using project_A’s generated files)
If project_B needs access to these generated files, you’ll need to expose them via a Meson dependency in project_A:
- In project_A’s
meson.build, declare a dependency that includes the generated files:project_a_generated_dep = declare_dependency( sources : gen_empty_soap_src, include_directories : meson.current_build_dir() / 'generated' # Expose headers if needed ) meson.override_dependency('project_a_generated', project_a_generated_dep) - In project_B’s
meson.build, fetch the dependency and use it:project_a_gen_dep = dependency('project_a_generated') lib_project_b = library( 'project_b', sources : ['util.cpp'], dependencies : project_a_gen_dep, # other build parameters... )
Key Notes
- Never hardcode paths to generated files—rely on Meson’s
custom_targetobjects or dependency declarations to avoid breakage when build configurations change. - The
depend_filesparameter you’re using works as intended: it tells Meson to re-run thecustom_targetwhenever any of thesoap_gen_depend_filesare modified, ensuring your generated files stay up-to-date.
内容的提问来源于stack exchange,提问作者Kostiantyn Ponomarenko

