watcher icon indicating copy to clipboard operation
watcher copied to clipboard

CMake build fails when added to another project (missing include path)

Open ncake opened this issue 2 months ago • 3 comments

There is a compilation error when using either FetchContent or add_subdirectory like in the minimal example below.

Reproducible both on Windows and Linux with CMake 4.x.

cmake_minimum_required(VERSION 3.9)

add_subdirectory(watcher)

[build] ...\watcher\watcher-c\src\watcher-c.cpp(4,10): error C1083: Cannot open include file: 'wtr/watcher.hpp': No such file or directory [...\build\watcher\watcher-c-shared.vcxproj]
[build]   watcher-c.cpp
[build] ...\watcher\watcher-c\src\watcher-c.cpp(4,10): error C1083: Cannot open include file: 'wtr/watcher.hpp': No such file or directory [...\build\watcher\watcher-c-static.vcxproj]

ncake avatar Oct 25 '25 07:10 ncake

Apparently dolphin-emu does this:

add_library(watcher INTERFACE IMPORTED GLOBAL)
set_target_properties(watcher PROPERTIES
	INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_LIST_DIR}/watcher/include
)

which works as a header-only library, but I was expecting to be able to link watcher-c with FetchContent_MakeAvailable.

ncake avatar Oct 25 '25 08:10 ncake

This fixed the error

diff --git "a/CMakeLists.txt" "b/CMakeLists.txt"
index 83ef67a..0db2510 100644
--- "a/CMakeLists.txt"
+++ "b/CMakeLists.txt"
@@ -419,7 +419,7 @@ wtr_add_autosan_lib_target(
   "watcher-c-shared"
   "watcher-c"
   "watcher-c/src/watcher-c.cpp"
-  "watcher-c/include;${CMAKE_SOURCE_DIR}/include"
+  "watcher-c/include;${CMAKE_CURRENT_LIST_DIR}/include"
   "SHARED"
 )
 
@@ -427,7 +427,7 @@ wtr_add_autosan_lib_target(
   "watcher-c-static"
   "watcher-c"
   "watcher-c/src/watcher-c.cpp"
-  "watcher-c/include;${CMAKE_SOURCE_DIR}/include"
+  "watcher-c/include;${CMAKE_CURRENT_LIST_DIR}/include"
   "STATIC"
 )

Though I still had to add this on the app side

include_directories(${wtr.watcher_SOURCE_DIR}/watcher-c/include)

ncake avatar Oct 25 '25 09:10 ncake

Should we add something like that last bit into this project's CMake file as well?

include_directories(${wtr.watcher_SOURCE_DIR}/watcher-c/include)

e-dant avatar Oct 25 '25 13:10 e-dant