SHTOOLS / SHTOOLS

SHTOOLS - Spherical Harmonic Tools

Home Page:https://shtools.github.io/SHTOOLS/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problems using cSHTOOLS on macOS

MarkWieczorek opened this issue · comments

I am able to get the SHTOOLS C wrapper functions to compile on macOS, and to run the test suite. However, I need to manually alter the LIB string in examples/cpp/Makefile in order to find the gfortran library. This is hindering me to find a common solution so that the tests can be run in github actions and travis on both the macOS and linux platforms.

When using g++-10 (installed via homebrew), I need to manually add the search path -L /usr/local/Cellar/gcc/10.2.0/lib/gcc/10. This is strange to me, because if I force verbose output when linking, the output says explicitly that this path is in the linker's search path. If I don't add this search path manually, I get errors like this:

Library search paths:
	/Users/lunokhod/SphericalHarmonics/shtools-git/lib
	/usr/local/lib
	/usr/local/Cellar/gcc/10.2.0/lib/gcc/10/gcc/x86_64-apple-darwin19/10.2.0
	/usr/local/Cellar/gcc/10.2.0/lib/gcc/10
	/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/lib
Framework search paths:
	/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/
Undefined symbols for architecture x86_64:
  "__ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv", referenced from:
      _main in test.o

If I use g++-9, I get slightly different, but very similar errors, like this:

  "std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const", referenced from:
      _main in test.o

In contrast, if I try to use Apple's clang, g++ or gcc (located in /usr/bin), I simply get an error that it can't find the gfortran library:

clang  -v -o test test.o -L /Users/lunokhod/SphericalHarmonics/shtools-git/lib -lSHTOOLS -lgfortran -ldl -L/usr/local/lib -lfftw3 -lm -llapack -lblas
Apple clang version 12.0.0 (clang-1200.0.32.2)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -dynamic -arch x86_64 -platform_version macos 10.15.0 10.15.6 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o test -L/Users/lunokhod/SphericalHarmonics/shtools-git/lib -L/usr/local/lib -L/usr/local/lib test.o -lSHTOOLS -lgfortran -ldl -lfftw3 -lm -llapack -lblas -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/lib/darwin/libclang_rt.osx.a
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)

It is possible that there is something wrong with the homebrew g++ compiler, and I have opened a discussion here.

I faced exactly the same issue. I had to use:

export LIBRARY_PATH=/usr/local/Cellar/gcc/10.2.0/lib/gcc/10/

in the terminal and then run it. Not very sure about MacOS environment so I don't know why.

Thanks for the suggestion. I did try manually setting the Library path in the terminal, but that didn't help. All it did was to add a duplicate line to the "Library search paths" above.

This is what I see

g++ -v -o test test.o -L ../../lib -lSHTOOLS -lgfortran -ldl -lfftw3 -lm -llapack -lblas
Apple clang version 12.0.0 (clang-1200.0.32.2)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
 "/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -dynamic -arch x86_64 -platform_version macos 10.15.0 10.15.6 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -o test -L../../lib -L/usr/local/Cellar/gcc/10.2.0/lib/gcc/10/ -L/usr/local/lib test.o -lSHTOOLS -lgfortran -ldl -lfftw3 -lm -llapack -lblas -lc++ -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/12.0.0/lib/darwin/libclang_rt.osx.a
ld: warning: could not create compact unwind for _shlocalizedadmitcorr_: stack subq instruction is too different from dwarf stack size
ld: warning: could not create compact unwind for _shmultitapercse_: stack subq instruction is too different from dwarf stack size


./test
SHRead exit status: 0
SHCilmToVector exit status: 0
3.3895e+06 -1735.55 -101.157 -785.374 -5966.19 222.242 -1060.12 119.976 758.445 38.4194 -194.816 -342.892 511.429 -86.9805 364.977 817.819 224.66 -69.8116 341.698 -243.804 -87.1215 6.97955 -196.686 28.2134 -237.52 -320.209 88.5702 -253.118 172.202 -79.6469 -163.68 307.327 39.3193 287.852 -420.73 97.2752 165.879 -28.0242 -138.316 -116.889 81.548 192.149 213.362 -102.405 98.3138 -57.7808 157.829 7.80598 61.4912 196.075 -13.3636 237.226 80.0594 9.74285 4.31236 71.511 66.402 149.996 -142.289 -44.3155 -140.201 4.70634 -34.2314 -134.793 -67.2091 -22.236 42.5351 1.55548 64.8776 -20.8739 -111.288 20.3224 -75.9854 105.082 19.9492 -2.42083 98.658 47.9556 -97.8901 102.213 0.864672 -11.9803 89.3677 -25.984 -53.7882 -15.9483 -123.266 59.6355 10.7198 -50.9656 -73.4738 -170.113 50.4186 -85.9382 50.1499 -7.63339 128.141 -106.215 43.8414 -74.6382 67.5064 81.2152 -45.2571 89.4706 -29.8078 -26.9755 45.0903 -11.5297 14.6132 -84.0519 2.66982 -3.63675 -31.0422 38.7938 -76.5979 -36.9509 -31.2392 -64.2192 29.8505 -112.607 53.5939 -4.05732 -92.7094 -23.9419 -39.0728 -21.3454 74.5288 41.404 -3.37021 -4.39096 -29.1079 69.8436 13.8554 -21.339 15.34 -16.1854 10.095 7.10758 40.7832 -53.2634 58.8055 -15.0454 53.2568 0.960232 51.1805 -11.8935 17.0156 -39.0905 28.8603 69.7857 -27.1099 51.9374 -106.134 73.0624 76.1015 36.2783 15.4683 -23.2197 26.0424 19.1658 -14.3442 17.4707 -74.9058 36.7783 -51.1199 49.8693 75.1868 -14.7429 -2.65042 -40.629 10.6243 -33.715 55.7701 42.6923 -13.8285 -68.9542 -24.5572 -13.1048 89.2731 -32.0745 30.2635 -94.1571 -29.6095 82.1565 3.81127 47.7297 40.2637 0.123983 17.34 16.09 6.34955 60.2813 -40.602 -9.38463 -13.2503 21.4409 66.422 39.3972 30.0866 32.5331 -16.2176 -18.1671 -14.5634 -42.5146 7.18052 36.9935 20.8726 -78.9059 -37.7639 34.5835 16.0421 35.0734 -35.7935 -29.3046 -13.6777 -33.9394 15.3642 5.17005 -4.08783 79.8079 -65.2803 -21.4523 -34.7513 95.4417 -40.8202 36.3634 20.9318 9.14367 -33.5605 -32.1138 -54.1955 -5.67777 34.1548 68.8129 -78.4384 13.1078 -33.0131 104.506 21.1803 16.6879 25.396 -16.0698 -46.1332 -17.9785 -10.4984 -1.25681 26.7552 3.88778 -1.3352 -24.1547 -2.14799 23.4063 36.9053 19.1212 -77.1753 18.2875 
diff to python 0

this is after using the export library path to gfortran. As I see in your verbose output, the cellar path to gfortran is not there as it is in mine here. Maybe this will be of some help.

Interesting. It looks like you are using the default Apple g++ compiler (/usr/bin/g++), which doesn't come with a gfortran library. Then, you link to the gfortran library supplied by homebrew's gcc installation.

This does indeed work, but, I would prefer to use homebrew's g++ compiler (/usr/local/bin/g++-10). If you find a way to get this to work, let me know!

the default Apple g++ compiler (/usr/bin/g++),

This default is clang; they just symlinked it to appear like gcc. So there wouldn't be a Fortran library, AFAIK.