Search code examples
c++meson-build

Error from meson build for a missing header which exists


From this simple Meson build file I get an error on line *** about a missing header file:

# meson.build - src/

subdir('config')
subdir('testers')
subdir('utilities')

headers += [
]

mainPrj += [
    'entrypoint.cpp'
    ]

autoTest += [
    'entrypoint_test.cpp'
]

Source = [ headers, mainPrj ]
SourceTest = [ headers, autoTest ]

MyProgExe = executable('MyProg', Source)     ***
MyProgTestExe = executable('MyProgTest', SourceTest)

test('Internal test', MyProgTestExe)

This is the error message:

src/meson.build:***:0: ERROR: File dataStructures.hpp does not exist.

The header does exist, it is in directory src/config/, and it does not appear in src/meson.build file, but in src/config/meson.build:

# meson.build - src/config/

headers += [
    'dataStructures.hpp',
    'interface.hpp'
]

If I swap dataStructures.hpp and interface.hpp, I get the error with interface.hpp.

I must be doing something wrong with the meson.build files, but I cannot find what.


Solution

  • You do not need to list your header files. Just add include directories like this:

    inc_dir = include_directories('path/to/include')
    

    in your case:

    inc_dir = include_directories('src/config')
    MyProgExe = executable('MyProg', Source, include_directories: inc_dir, ...)
    

    What I usually do is to declare project dependency:

    project_dep = declare_dependency(include_directories: inc_dir, sources: srcs,
    dependencies[...])
    

    and use that for each target (e.g. app and test executables) like this:

    src_main = ...
    executable('app', sources:srcs_main, dependencies:[project_dep])
    ...
    test_main = ...
    executable('unit_tests', sources:[test_main, test_specific_srcs], dependencies:[project_dep])