# Generated code dependency fails

**URL:** https://discourse.cmake.org/t/generated-code-dependency-fails/1480
**Category:** Usage
**Tags:** gen:makefiles
**Created:** [July 2, 2020, 2:40pm UTC](https://discourse.cmake.org/t/generated-code-dependency-fails/1480 "2020-07-02T14:40:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Marc\_Pawlowsky](https://discourse.cmake.org/user_avatar/discourse.cmake.org/marc_pawlowsky/32/719_2.png) [@Marc\_Pawlowsky](https://discourse.cmake.org/u/Marc_Pawlowsky)
#### Post date: [July 2, 2020, 2:40pm UTC](https://discourse.cmake.org/t/generated-code-dependency-fails/1480/1 "2020-07-02T14:40:53Z")

</div>

Sample code is available in [https://github.com/marcpawl/cmake\_generated\_source.git](https://github.com/marcpawl/cmake_generated_source.git)

I have two directories px and pxlib. px generates code, and pxlib consumes it.

For px I have:  
cmake\_minimum\_required ( VERSION 3.12)  
set(CMAKE\_EXPORT\_COMPILE\_COMMANDS ON )  
set(CMAKE\_BUILD\_WITH\_INSTALL\_RPATH ON )  
add\_custom\_target(px\_all ALL  
DEPENDS /project/px/build /project/px/build/generated.cpp )  
add\_custom\_command(  
OUTPUT /project/px/build /project/px/build/generated.cpp  
COMMAND /project/px/generator  
MAIN\_DEPENDENCY /project/px/generator  
)

And for pxlib I have  
cmake\_minimum\_required (VERSION 3.12)  
set(CMAKE\_EXPORT\_COMPILE\_COMMANDS ON)  
set(CMAKE\_BUILD\_WITH\_INSTALL\_RPATH ON)  
add\_library( pxlib STATIC /project/px/build/generated.cpp)  
set\_target\_properties(pxlib PROPERTIES CXX\_STANDARD 17)  
add\_dependencies(pxlib px\_all)

But I cannot generate the makefiles unless the generated code is present  
cmake3 -G “Unix Makefiles” …  
  
CMake Error at pxlib/CMakeLists.txt:4 (add\_library):  
Cannot find source file:

```
/project/px/build/generated.cpp

Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
.hpp .hxx .in .txx

CMake Error at pxlib/CMakeLists.txt:4 (add_library):
No SOURCES given to target: pxlib

```

If I run the generator first to provide the C++ flie then I can build my targets. What am I missing to say that the generator script needs to be executed before compiling the library?

P.S. The absolute paths is because I am in the middle of transforming hand written Makefiles to CMakeLists.txt files, they will be made into relative paths in a further step.

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [July 2, 2020, 5:44pm UTC](https://discourse.cmake.org/t/generated-code-dependency-fails/1480/2 "2020-07-02T17:44:43Z")

</div>

Instead of a custom target use an OBJECT library and use those objects. As an alternative, put the custom command in the same directory as the static library.

---

<div class="post-metadata">

### Author: ![Marc\_Pawlowsky](https://discourse.cmake.org/user_avatar/discourse.cmake.org/marc_pawlowsky/32/719_2.png) [@Marc\_Pawlowsky](https://discourse.cmake.org/u/Marc_Pawlowsky)
#### Post date: [July 2, 2020, 8:51pm UTC](https://discourse.cmake.org/t/generated-code-dependency-fails/1480/3 "2020-07-02T20:51:30Z")

</div>

That worked, thanks.

For others who have the problem, or more than likely myself in a few months, I have updated [https://github.com/marcpawl/cmake\_generated\_source](https://github.com/marcpawl/cmake_generated_source) as a working demo.

px/CMakeLists.txt got  
add\_library(px\_all OBJECT /project/px/build/generated.cpp )  
add\_custom\_command(  
OUTPUT /project/px/build /project/px/build/generated.cpp  
COMMAND /project/px/generator  
MAIN\_DEPENDENCY /project/px/generator  
)

and pxlib/CMakeLists.txt got  
add\_library( pxlib STATIC $\<TARGET\_OBJECTS:px\_all\>)

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [July 3, 2020, 7:43am UTC](https://discourse.cmake.org/t/generated-code-dependency-fails/1480/4 "2020-07-03T07:43:22Z")

</div>

If you use a newer CMake version, you can link to the object library like any other library using target\_link\_libraries(). Also you should avoid the hard-coded paths and use the various variables for source and build directories instead.
