Deleting bin and lib directories causes an error with cmake generated Makefile

  c++, cmake, makefile

I have a project structured like this:

  • bin/
    • Linux-Debug/
    • Linux-Release/
  • lib/
    • Linux-Debug/
    • Linux-Release/
  • build/
    • Debug/
    • Release/
  • ExampleSub1/
  • ExampleSub2/
  • CMakeLists.txt

and a CMakeLists.txt file:

cmake_minimum_required(VERSION 3.21.3)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

project(Example)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
    ${CMAKE_SOURCE_DIR}/lib/${CMAKE_SYSTEM_NAME}-$<CONFIG>
)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
    ${CMAKE_SOURCE_DIR}/lib/${CMAKE_SYSTEM_NAME}-$<CONFIG>
)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
    ${CMAKE_SOURCE_DIR}/bin/${CMAKE_SYSTEM_NAME}-$<CONFIG>
)

add_subdirectory(ExampleSub1)
add_subdirectory(ExampleSub2)


If I run cmake ../.. from build/Release it generates a Makefile which in turn successfully creates bin and lib directories and builds the project. But if I manually delete bin or lib directory (simply run rm -r bin lib) after that running Makefile causes an error:

 Linking CXX static library ../../../lib/Linux-Release/libExampleSub1.a
/usr/bin/ar: ../../../lib/Linux-Release/libLyterEngine.a: No such file or directory

My goal is deleting bin and lib and running make without a problem.

Also, make clean cleans bin and lib directories – they become empty.
Now, If I try to rm these empty directories, make still complains with the same error, even though make clean was run before and I only deleted empty directories.
My question is, is this because cmake keeps track of built objects somehow and when I
manually delete them it still thinks the libraries are there ? If so, is there an option to
change this behavior, so that make checks which targets need to be regenerated every time?

Source: Windows Questions C++

LEAVE A COMMENT