Closed DusanJovic-NOAA closed 3 days ago
Another issue with installed package config files is that INTERFACE_INCLUDE_DIRECTORIES is not defined for imported targets, so I get this error when I try to build project that requires g2c
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /home/dusan/noaa/g2c/test/b
[ 50%] Building C object CMakeFiles/read_g2.dir/read_g2.c.o
/home/dusan/noaa/g2c/test/read_g2.c:1:10: fatal error: grib2.h: No such file or directory
1 | #include "grib2.h"
| ^~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/read_g2.dir/build.make:76: CMakeFiles/read_g2.dir/read_g2.c.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/read_g2.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
To fix this, the location of installed include file(s) must be defined for both shared and static targets, now it's only defined for "objlib" target which is not installed at all.
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a1ad0c6..bdebf93 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -157,6 +157,14 @@ CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/grib2.h.in" "${CMAKE_CURRENT_BINARY_
target_include_directories(${lib_name}_objlib PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}>"
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)
+if(BUILD_SHARED_LIBS)
+ target_include_directories(${lib_name}_shared PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}>"
+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)
+endif()
+if(BUILD_STATIC_LIBS)
+ target_include_directories(${lib_name}_static PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}>"
+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)
+endif()
if (NOT WIN32)
target_link_libraries(${lib_name}_objlib INTERFACE m)
In the installed cmake package file (
<prefix>/lib64/cmake/g2c/g2c-config.cmake
) there is this line:which assumes that
g2c::g2c
target exists, which is not the case if library is built with-DBUILD_SHARED_LIBS=OFF
. In that case onlyg2c::g2c_static
exists. I get this error when I build my project that uses g2c library:File
cmake/PackageConfig.cmake.in
should be changed like this:to conditionally run get_target_property depending on whether shared or static libraries are installed.