# Include PRIVATE generated headers dir under build tree in test execution from a source dir

**URL:** https://discourse.cmake.org/t/include-private-generated-headers-dir-under-build-tree-in-test-execution-from-a-source-dir/1231
**Category:** Usage
**Created:** [May 18, 2020, 9:15pm UTC](https://discourse.cmake.org/t/include-private-generated-headers-dir-under-build-tree-in-test-execution-from-a-source-dir/1231 "2020-05-18T21:15:35Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![yisseamake](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yisseamake/32/897_2.png) [@yisseamake](https://discourse.cmake.org/u/yisseamake)
#### Post date: [May 18, 2020, 9:15pm UTC](https://discourse.cmake.org/t/include-private-generated-headers-dir-under-build-tree-in-test-execution-from-a-source-dir/1231/1 "2020-05-18T21:15:36Z")

</div>

Could you please advise on the proper way to support test execution when test program includes generated file from the build tree without exposing generated file as PUBLIC. When library is built header files are generated under sub directory which is at the same level as the test sub directory as per the following example.  
The problem is that to make test execution work I would need to make $\<BUILD\_INTERFACE:}/…/genned/include \> PUBLIC. Test is only for a particular library is not needed by other libraries being built or might cause conflict with other libraries . I’d like to avoid moving test dir under mylib subdir nor adding copy command  
.  
|-- mylib/  
| |-- CMakeLists.txt  
| `-- prog1.cpp |-- genned/ | |-- CMakeLists.txt | `– include  
genned.h {generated by mylib build}  
`-- test/ |-- CMakeLists.txt `– test.cpp { has #include “genned.h”}

CMakeLists.txt of mylib subdir  
add\_library(my\_lib SHARED)

target\_include\_directories(my\_lib  
PUBLIC  
$\<INSTALL\_INTERFACE:include  
# since we are in src dir and include is at the same level   
$\<BUILD\_INTERFACE:${CMAKE\_CURRENT\_SOURCE\_DIR}/include\>  
#these headers include genned.h

#[===================[  
This works but include exposed as public  
$\<BUILD\_INTERFACE:}/…/genned/include \>  
#]==================]  
PRIVATE  
#generated headers are in —not working  
${CMAKE\_CURRENT\_BINARY\_DIR}/…/genned/include

## )

test/CMakeLists.txt  
test(xtests SCENARIO=)

target\_include\_directories(xtests  
PRIVATE  
${GUNIT\_INCLUDE\_DIRS}

)

target\_link\_libraries(xtests  
my\_lib  
)  
Make fails with an  
ERROR  
In file included from test.cpp  
fatal error…: genned.h: No such file or directory compilation terminated.

To make it work I had to

---

<div class="post-metadata">

### Author: ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)
#### Post date: [May 19, 2020, 1:45pm UTC](https://discourse.cmake.org/t/include-private-generated-headers-dir-under-build-tree-in-test-execution-from-a-source-dir/1231/2 "2020-05-19T13:45:55Z")

</div>

Since you don’t want to attach the include directory information to `my_lib` you have the following options.

1. Construct an INTERFACE target that holds the target include directory, and use that in addition:

```cmake
add_library(my_lib_test_info INTERFACE)
target_include_directories(my_lib_test_info INTERFACE $<BUILD_INTERFACE:}/…/genned/include >)
target_link_libraries(xtests PRIVATE my_lib_test_info)

```

1. Place the include directory information on xtests when you construct it
