On macOS, I get the following error when I try to build a project that uses find_package
to load MyLibConfig.cmake
:
/Users/vdamewood/tmp/projekto/MyConsumer/src/main.c:1:10: fatal error: 'MyLib/MyLib.h' file not found
#include <MyLib/MyLib.h>
^~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/MyConsumer.dir/src/main.c.o] Error 1
make[1]: *** [CMakeFiles/MyConsumer.dir/all] Error 2
make: *** [all] Error 2
I have two projects in subdirectories of ~/tmp/Projekto
named MyLib
and MyConsumer
. This error occurs when running make
when building MyConsumer. This is how I built MyLib:
mkdir ~/tmp/Projekto/build-MyLib
cd ~/tmp/Projekto/build-MyLib
cmake ../MyLib
make
DESTDIR=./install make install
Then I built MyConsumer with this:
mkdir ~/tmp/Projekto/build-MyConsumer
cd ~/tmp/Projekto/build-MyConsumer
CMAKE_PREFIX_PATH=../build-MyLib/install/usr/local cmake ../MyConsumer
make
MyLib/CMakeLists.txt
contains this:
cmake_minimum_required(VERSION 3.14)
project(MyLib
VERSION 1.2.0
LANGUAGES C
)
add_library(MyLib)
set(PUBLIC_HEADERS
include/MyLib/MyLib.h
${CMAKE_CURRENT_BINARY_DIR}/include/MyLib/Api.h
)
target_sources(MyLib
PRIVATE
src/MyLib.c
${PUBLIC_HEADERS}
)
target_include_directories(MyLib
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>"
)
if (APPLE)
# FIXME: framework path
else()
target_include_directories(MyLib
INTERFACE
"$<INSTALL_INTERFACE:include>"
)
endif()
set_target_properties(MyLib PROPERTIES
PUBLIC_HEADER "${PUBLIC_HEADERS}"
C_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN On
FRAMEWORK TRUE
FRAMEWORK_VERSION ${PROJECT_VERSION}
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)
include(GenerateExportHeader)
generate_export_header(MyLib
EXPORT_FILE_NAME include/MyLib/Api.h
EXPORT_MACRO_NAME MYLIB_API
)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
MyLibConfigVersion.cmake
VERSION ${MyLib_VERSION}
COMPATIBILITY SameMajorVersion
)
include(GNUInstallDirs)
install(TARGETS MyLib EXPORT MyLib
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
FRAMEWORK DESTINATION .
RESOURCE DESTINATION ${CMAKE_INSTALL_DATADIR}/MyLib
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/MyLib
)
if(APPLE)
set(CONFIG_DESTINATION MyLib.framework/Resources)
else()
set(CONFIG_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/MyLib)
endif()
install(EXPORT MyLib
DESTINATION ${CONFIG_DESTINATION}
NAMESPACE MyLib::
FILE MyLibConfig.cmake
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/MyLibConfigVersion.cmake
DESTINATION ${CONFIG_DESTINATION}
)
MyConsumer/CMakeLists.txt
contains this:
cmake_minimum_required(VERSION 3.14)
project(MyConsumer
VERSION 1.0.0
LANGUAGES C
)
find_package(MyLib 1.1.0 REQUIRED)
message("MyLib_FOUND ${Siliko_FOUND}")
add_executable(MyConsumer)
target_sources(MyConsumer PUBLIC src/main.c)
target_link_libraries(MyConsumer PRIVATE MyLib::MyLib)
I tried building MyConsumer manually and found that the the difference between getting this error and not was whether or not I used -F ../build-MyLib/install/usr/local
. So, I tried adding this to the generated MyLibConfig.cmake file:
set_target_properties(MyLib::MyLib PROPERTIES
INTERFACE_COMPILE_OPTIONS "-F${_IMPORT_PREFIX}"
)
With this addition, MyConsumer builds, links, and runs without a problem.
Is there some way I can get CMake to generate MyLibConfig.cmake
with this, or somehow get clang to look for the framework at compile time in the directory where it was found?