Question: Strange behavior from CMake when importing a STATIC library

I'm experiencing a strange (bug?) when importing the yaml-cpp static library with CMake.
main.cpp
#include "yaml-cpp/yaml.h"

CMakeLists.txt (working)
add_library(yaml-cpp ${PROJECT_BINARY_DIR}/path/to/libyaml-cpp.a)
target_link_libraries(main yaml-cpp)

CMakeLists.txt (not working)
add_library(yaml-cpp STATIC IMPORTED)                                
set_target_properties(yaml-cpp PROPERTIES IMPORTED_LOCATION ${PROJECT_BINARY_DIR}/path/to/libyaml-cpp.a)  
target_link_libraries(main yaml-cpp)  

When I use the second CMakeLists.txt, my main.cpp cannot find yaml-cpp/yaml.h. When I use the first CMakeLists.txt, it can, however, I get the "ar: no archive members specified" message every time I configure the project, which is annoying. Would like to import it the second way to get rid of that message.


Answer:
For both of you who answered, I appreciate it. Turns out I should have provided more information in my question. The issue was arising basically from the fact that I am attempting to create a portable installation, with the entire source of each of the dependencies within the project folder-structure, which is something that I haven't attempted before. It seemed logical to me that the library files (.a, .dylib, etc..) would contain all of the headers within them, but apparently that is not the case. I will provide a few details on how I was able to fix the issue.

Building the libraries before the rest of the project was the right move, but I forgot to install them. cmake -> make -> make install

BuildLibraries.txt (cmake file)
    set(yaml-cpp_cmakelists "${CMAKE_SOURCE_DIR}/external/yaml-cpp-master")
    set(yaml-cpp_build_location "${CMAKE_BINARY_DIR}/external/yaml-cpp-master")        
    file(MAKE_DIRECTORY ${yaml-cpp_build_location})  

    execute_process(                                                                     
        COMMAND ${CMAKE_COMMAND} -S ${yaml-cpp_cmakelists} -B ${yaml-cpp_build_location} -D CMAKE_INSTALL_PREFIX=${CMAKE_LIBRARY_OUTPUT_DIRECTORY} -D BUILD_SHARED_LIBS=OFF
        WORKING_DIRECTORY ${yaml-cpp_build_location}
        RESULT_VARIABLE result
    )
    if(NOT result EQUAL 0)
        message(FATAL_ERROR "Failed to configure yaml-cpp")
    endif()

    execute_process(                                                                     
        COMMAND make -C ${yaml-cpp_build_location} -j4
        WORKING_DIRECTORY ${yaml-cpp_build_location}
        RESULT_VARIABLE result
    )
    if(NOT result EQUAL 0)
        message(FATAL_ERROR "Failed to generate yaml-cpp")
    endif()

    execute_process(                                                                     
        COMMAND make install -C ${yaml-cpp_build_location} -j4
        WORKING_DIRECTORY ${yaml-cpp_build_location}
        RESULT_VARIABLE result
    )
    if(NOT result EQUAL 0)
        message(FATAL_ERROR "Failed to install yaml-cpp")
    endif()


Inside project-root directory CMakeLists.txt:

ensure that find_package() knows where to look for the libraries that you installed using set(CMAKE_PREFIX_PATH ...)
go ahead and use find_package(). It is, as these users suggested, much easier.



CMakeLists.txt (in root project directory)
       cmake_minimum_required(VERSION 3.26.0)                           
       set(CMAKE_CXX_STANDARD 17)
       set(CMAKE_CXX_STANDARD_REQUIRED ON)

       set(REBUILD_LIBS ON)                                             # choose wether to rebuild libraries during the generate phase

       set(PROJECT_BINARY_DIR "${CMAKE_SOURCE_DIR}/build")              # root build directory
       set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")      # static libraries
       set(CMAKE_INSTALL_LIBDIR ${PROJECT_BINARY_DIR}/lib)
       set(CMAKE_INSTALL_BINDIR ${PROJECT_BINARY_DIR})
       set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")      # shared libraries
       set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")      # executables
       set(CMAKE_PREFIX_PATH                                            # define search-paths for find_package()
           "${PROJECT_BINARY_DIR}/lib/cmake"
       )  

       if(REBUILD_LIBS)
           include(${CMAKE_SOURCE_DIR}/CMakeFiles/BuildLibraries.txt)   # Build external libraries using the BuildLibraries.txt CMakeLists.txt file
       endif()


       project(myProject)     
       add_executable(main main.cpp) 

       find_package (yaml-cpp)

       target_link_libraries(main yaml-cpp)