-
Notifications
You must be signed in to change notification settings - Fork 17
Description
I had a problem with FindNetCDF and static linking.
There were two NetCDF libraries available: one static and one shared, and I wanted to use the static one, so I changed CMAKE_FIND_LIBRARY_SUFFIXES to ".a;.so" (to favor .a)
The problem is with how nc-config reports the libraries in this block of code:
CMakeModules/Modules/FindNetCDF.cmake
Line 215 in 8aaef34
| #Use nc-config to set per-component LIBRARIES variable if possible |
#Use nc-config to set per-component LIBRARIES variable if possible
netcdf_config( ${NetCDF_${_comp}_CONFIG_EXECUTABLE} ${_${_comp}_libs_flag} _val )
if( _val )
set( NetCDF_${_comp}_LIBRARIES ${_val} )
if(NOT NetCDF_${_comp}_LIBRARY_SHARED AND NOT NetCDF_${_comp}_FOUND) #Static targets should use nc_config to get a proper link line with all necessary static targets.
list( APPEND NetCDF_LIBRARIES ${NetCDF_${_comp}_LIBRARIES} )
endif()
else()
set( NetCDF_${_comp}_LIBRARIES ${NetCDF_${_comp}_LIBRARY} )
if(NOT NetCDF_${_comp}_LIBRARY_SHARED)
message(SEND_ERROR "Unable to properly find NetCDF. Found static libraries at: ${NetCDF_${_comp}_LIBRARY} but could not run nc-config: ${NetCDF_CONFIG_EXECUTABLE}")
endif()
endif()
and then this:
CMakeModules/Modules/FindNetCDF.cmake
Line 242 in 8aaef34
| set_target_properties(NetCDF::NetCDF_${_comp} PROPERTIES |
add_library(NetCDF::NetCDF_${_comp} ${_library_type} IMPORTED)
set_target_properties(NetCDF::NetCDF_${_comp} PROPERTIES
IMPORTED_LOCATION ${NetCDF_${_comp}_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES "${NetCDF_${_comp}_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES ${NetCDF_${_comp}_LIBRARIES} )
It takes the value nc-config reports for libs, which in my case was -L/path/netcdf -lnetcdf and adds it to the imported target's INTERFACE_LINK_LIBRARIES. Then the final link flags are/path/netcdf/lib/libnetcdf.a (from IMPORTED_LOCATION) -L/path/netcdf/ -lnetcdf and that -lnetcdf picks up the dynamic library, overriding the static one. If there were only the static library in the path it would work fine.
The reason the libraries are added to INTERFACE_LINK_LIBRARIES is to pick up the necessary flags to link to dependencies, but in what I've seen is that all it links to is itself (e.g. -L/path/netcdf -lnetcdf) which shouldn't be necessary because its IMPORTED_LOCATIONS contains libnetcdf.a and is added to the link line.
Anyway, I think the solution is to remove NetCDF itself from INTERFACE_LINK_LIBRARIES so that only the .a is added on the link line.
Any thoughts @aerorahul or @edwardhartnett