cmake failing without error messages when importing libraries after moving from single CMakeFile to a subdirectory structure

I have the following CMakeLists.txt file which configures and builds just fine.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>cmake_minimum_required (VERSION 3.22)
project (foo)
add_library (pglib SHARED IMPORTED)
set_target_properties (pglib PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_LIST_DIR}/postgres16/lib/libpq.so)
target_include_directories (pglib INTERFACE ${CMAKE_CURRENT_LIST_DIR}/postgres16/include)
target_link_directories (pglib INTERFACE ${CMAKE_CURRENT_LIST_DIR}/postgres16/lib)
target_link_libraries (pglib INTERFACE pq)
add_executable (bar main.cpp)
target_include_directories (bar PUBLIC ./)
target_link_libraries (bar PUBLIC pglib)
</code>
<code>cmake_minimum_required (VERSION 3.22) project (foo) add_library (pglib SHARED IMPORTED) set_target_properties (pglib PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_LIST_DIR}/postgres16/lib/libpq.so) target_include_directories (pglib INTERFACE ${CMAKE_CURRENT_LIST_DIR}/postgres16/include) target_link_directories (pglib INTERFACE ${CMAKE_CURRENT_LIST_DIR}/postgres16/lib) target_link_libraries (pglib INTERFACE pq) add_executable (bar main.cpp) target_include_directories (bar PUBLIC ./) target_link_libraries (bar PUBLIC pglib) </code>
cmake_minimum_required      (VERSION 3.22)
project                     (foo)

add_library                 (pglib SHARED IMPORTED)

set_target_properties       (pglib PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_LIST_DIR}/postgres16/lib/libpq.so)
target_include_directories  (pglib INTERFACE ${CMAKE_CURRENT_LIST_DIR}/postgres16/include)
target_link_directories     (pglib INTERFACE ${CMAKE_CURRENT_LIST_DIR}/postgres16/lib)
target_link_libraries       (pglib INTERFACE pq)

add_executable              (bar main.cpp)
target_include_directories  (bar PUBLIC ./)
target_link_libraries       (bar PUBLIC pglib)

if I replace postgres16/include with postgres16/blabla, then I get a configuration error and I cannot even try to build. That is what I want, obviously.

Now I separate the CMakeLists.txt file into two different ones in two different directories which I combine via add_subdirectory in a third CMakeLists.txt. My directory structure is as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>├── CMakeLists.txt
├── lib
│ ├── CMakeLists.txt
│ └── postgres16
│ └── ...
└── postgres_test
├── CMakeLists.txt
└── main.cpp
</code>
<code>├── CMakeLists.txt ├── lib │ ├── CMakeLists.txt │ └── postgres16 │ └── ... └── postgres_test ├── CMakeLists.txt └── main.cpp </code>
├── CMakeLists.txt
├── lib
│   ├── CMakeLists.txt
│   └── postgres16
│       └── ...
└── postgres_test
    ├── CMakeLists.txt
    └── main.cpp

./CMakeLists.txt:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>cmake_minimum_required (VERSION 3.22)
project (foo)
set (CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
add_subdirectory (lib)
add_subdirectory (postgres_test)
</code>
<code>cmake_minimum_required (VERSION 3.22) project (foo) set (CMAKE_EXPORT_COMPILE_COMMANDS TRUE) add_subdirectory (lib) add_subdirectory (postgres_test) </code>
cmake_minimum_required      (VERSION 3.22)
project                     (foo)
set                         (CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
add_subdirectory            (lib)
add_subdirectory            (postgres_test)

lib/CMakeLists.txt

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>cmake_minimum_required (VERSION 3.22)
add_library (pglib SHARED IMPORTED)
set_target_properties (pglib PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/lib/postgres16/lib/libpq.so)
target_include_directories (pglib INTERFACE ${PROJECT_SOURCE_DIR}/lib/postgres16/include)
target_link_directories (pglib INTERFACE ${PROJECT_SOURCE_DIR}/lib/postgres16/lib)
target_link_libraries (pglib INTERFACE pq)
</code>
<code>cmake_minimum_required (VERSION 3.22) add_library (pglib SHARED IMPORTED) set_target_properties (pglib PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/lib/postgres16/lib/libpq.so) target_include_directories (pglib INTERFACE ${PROJECT_SOURCE_DIR}/lib/postgres16/include) target_link_directories (pglib INTERFACE ${PROJECT_SOURCE_DIR}/lib/postgres16/lib) target_link_libraries (pglib INTERFACE pq) </code>
cmake_minimum_required      (VERSION 3.22)

add_library                 (pglib SHARED IMPORTED)

set_target_properties       (pglib PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/lib/postgres16/lib/libpq.so)
target_include_directories  (pglib INTERFACE ${PROJECT_SOURCE_DIR}/lib/postgres16/include)
target_link_directories     (pglib INTERFACE ${PROJECT_SOURCE_DIR}/lib/postgres16/lib)
target_link_libraries       (pglib INTERFACE pq)

postgres_test/CMakeLists.txt

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>cmake_minimum_required (VERSION 3.22)
add_executable (bar main.cpp)
target_include_directories (bar PUBLIC ./)
target_link_libraries (bar PUBLIC pglib)
</code>
<code>cmake_minimum_required (VERSION 3.22) add_executable (bar main.cpp) target_include_directories (bar PUBLIC ./) target_link_libraries (bar PUBLIC pglib) </code>
cmake_minimum_required      (VERSION 3.22)
add_executable              (bar main.cpp)
target_include_directories  (bar PUBLIC ./)
target_link_libraries       (bar PUBLIC pglib)

Now I configure via mkdir build; cd build; cmake .. and I see something wired. No error messages, remember that I replaced include with blabla?

So I fix the blabla, reconfigure and compile via cmake --build . --target all. Now I get missing includes.

grep postgres16 compile_commands.json shows nothing, which means that target_include_directories where not honored as before when everything was in one CMakeLists.txt.

BTW, other error messages are emitted. For e.g. if I replace target_include_directories(bar INTERFACE ...) with target_include_direcoties(bar PUBLIC...) then I do get the error message that I need to use INTERFACE.

So my three questions are:

  • why no error message for non existing include path when using target_include_directories in subdirectory?
  • why no warning message when target_include_directory is ignored for some reason?
  • why are target_include_directories ignored in the first place when using add_subdirectory?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật