Skip to content

OPTIMIZE_DEPENDENCIES gives make -j failures for STATIC libraries depending on OBJECT libraries

I'm using cmake 3.23.1 but a review of the release notes doesn't make me suspect this is fixed in newer versions.

Consider this environment:

$ mkdir tst
$ cd tst
$ touch obj.c tst.c
$ cat >>CMakeLists.txt <<EOF
cmake_minimum_required(VERSION 3.23)

project(tstopt C)

set(CMAKE_OPTIMIZE_DEPENDENCIES ON)

add_library(obj OBJECT obj.c)

add_library(tst STATIC tst.c)
target_link_libraries(tst PUBLIC obj)
EOF

$ mkdir obj
$ cd obj
$ cmake ..

$ make -j

When I run this I get errors from make:

make[2]: *** No rule to make target 'CMakeFiles/obj.dir/obj.c.o', needed by 'libtst.a'.  Stop.
make[2]: *** Waiting for unfinished jobs....
[ 33%] Building C object CMakeFiles/obj.dir/obj.c.o
[ 66%] Built target obj
[ 66%] Building C object CMakeFiles/tst.dir/tst.c.o
make[1]: *** [CMakeFiles/Makefile2:111: CMakeFiles/tst.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

It appears that some prerequisites are missing so that make tries to build libtst.a before it has generated makefiles that define a rule for obj.c.o... or something? I confess I got lost trying to figure out how the generated make environment is put together.

Note that if I make tst a SHARED library rather than STATIC, I don't see this problem. And, of course, if I disable CMAKE_OPTIMIZE_DEPENDENCIES it works correctly as well. And also, if I don't enable parallel builds in make then it works that way too.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information