Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incorrect value for target_link_libraries #18

Closed
haampie opened this issue Dec 4, 2024 · 3 comments
Closed

incorrect value for target_link_libraries #18

haampie opened this issue Dec 4, 2024 · 3 comments

Comments

@haampie
Copy link
Contributor

haampie commented Dec 4, 2024

In

cprnc/CMakeLists.txt

Lines 86 to 87 in fec7c42

target_link_libraries (cprnc
PUBLIC ${NetCDF_Fortran_LIBRARIES} ${NetCDF_C_LIBRARIES} ${NetCDF_LIBRARIES})

The NetCDF_*LIBRARIES variables are strings of linker flags -L<path> -l<lib>.

But target_link_libraries expects full paths to libraries like <path>/lib<lib>.so, otherwise CMake does not know what libraries are effectively being linked.

So even though the build works fine, features like CMAKE_INSTALL_RPATH_USE_LINK_PATH=ON do not work, because <path> is not among the known link directories.

Instead of using nc-config --libs, you could use pkg-config, for which CMake has builtin support:

https://cmake.org/cmake/help/latest/module/FindPkgConfig.html

In particular pkg_search_module would then give you *_LIBRARIES which is a list of full paths, which can be used in target_link_libraries, so that CMAKE_INSTALL_RPATH_USE_LINK_PATH=ON works again.

@climbfuji
Copy link

The "correct" way (simply because it is the widely accepted standard across the NWP community) to find and link to netCDF is by (a) updating findNetcdf.cmake to this version https://github.com/NOAA-EMC/CMakeModules/blob/develop/Modules/FindNetCDF.cmake or the ECMWF one (these two versions are nearly the same, and functionally identical). Then, one can simply do (example from Unified Forecast System/CCPP):

find_package(NetCDF 4.7.4 REQUIRED C Fortran)
...
target_link_libraries(myproject PUBLIC NetCDF::NetCDF_Fortran NetCDF::NetCDF_C)

and that takes care of the include and linker flags. And, as an aside, this works with spack.

@rljacob
Copy link
Member

rljacob commented Dec 5, 2024

@climbfuji see changes implemented in #19

@haampie
Copy link
Contributor Author

haampie commented Dec 5, 2024

Closed by #19

@haampie haampie closed this as completed Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants