Generated code dependency fails

Sample code is available in

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

For px I have:
cmake_minimum_required ( VERSION 3.12)
add_custom_target(px_all ALL
DEPENDS /project/px/build /project/px/build/generated.cpp )
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)
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:


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.

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.

That worked, thanks.

For others who have the problem, or more than likely myself in a few months, I have updated as a working demo.

px/CMakeLists.txt got
add_library(px_all OBJECT /project/px/build/generated.cpp )
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>)

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.