# Is target specific stuff transitive to dependencies?

**URL:** https://discourse.cmake.org/t/is-target-specific-stuff-transitive-to-dependencies/336
**Category:** Usage
**Created:** [December 9, 2019, 2:08am UTC](https://discourse.cmake.org/t/is-target-specific-stuff-transitive-to-dependencies/336 "2019-12-09T02:08:06Z")
**Posts on this page:** 1
**Showing post:** 8

<div class="post-metadata">

### Author: ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)
#### Post date: [December 9, 2019, 9:03am UTC](https://discourse.cmake.org/t/is-target-specific-stuff-transitive-to-dependencies/336/8 "2019-12-09T09:03:57Z")

</div>

It makes perfectly sense (I did use makefile a lot some time ago), but I bet that in that case commonLib is not really a “library” since it shall be _recompiled_ for different app. What you describe is a way to bring in a new commonLib-A into A by specifying a set of source file that need to be recompiled for A.

So you need to create your interface library, with appropriate associated source files in it:

```cmake
add_library(common INTERFACE)
target_include_directories(common INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_sources(common INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/common.cpp)

```

then:

```cmake
add_executable(A-app a.cpp)
target_link_libraries(A-app common)
target_compile_definitions(A-app PRIVATE withA)

add_executable(B-app b.cpp)
target_link_libraries(B-app common)
target_compile_definitions(B-app PRIVATE withB)

```

and you’ll get what you want (I guess).  
see:

```auto
$ ninja -v 
[1/6 (4) - 0.155] /usr/bin/c++ -DwithB -I../ -MD -MT CMakeFiles/B-app.dir/common.cpp.o -MF CMakeFiles/B-app.dir/common.cpp.o.d -o CMakeFiles/B-app.dir/common.cpp.o -c ../common.cpp
[2/6 (3) - 0.220] /usr/bin/c++ -DwithA -I../ -MD -MT CMakeFiles/A-app.dir/common.cpp.o -MF CMakeFiles/A-app.dir/common.cpp.o.d -o CMakeFiles/A-app.dir/common.cpp.o -c ../common.cpp
[3/6 (2) - 0.240] /usr/bin/c++ -DwithB -I../ -MD -MT CMakeFiles/B-app.dir/b.cpp.o -MF CMakeFiles/B-app.dir/b.cpp.o.d -o CMakeFiles/B-app.dir/b.cpp.o -c ../b.cpp
[4/6 (2) - 0.285] /usr/bin/c++ -DwithA -I../ -MD -MT CMakeFiles/A-app.dir/a.cpp.o -MF CMakeFiles/A-app.dir/a.cpp.o.d -o CMakeFiles/A-app.dir/a.cpp.o -c ../a.cpp
[5/6 (2) - 0.309] : && /usr/bin/c++ CMakeFiles/B-app.dir/b.cpp.o CMakeFiles/B-app.dir/common.cpp.o -o B-app && :
[6/6 (1) - 0.357] : && /usr/bin/c++ CMakeFiles/A-app.dir/a.cpp.o CMakeFiles/A-app.dir/common.cpp.o -o A-app && :

```

Here comes the archive of this:  
[target\_specific\_define.zip](https://discourse.cmake.org/uploads/short-url/kw1QGxb21IEV3R7561ENw0Ii7DT.zip) (1.7 KB)

---

_[View the full topic](https://discourse.cmake.org/t/is-target-specific-stuff-transitive-to-dependencies/336)._
