njoy / NJOY2016

Nuclear data processing with legacy NJOY
https://www.njoy21.io/NJOY2016
Other
98 stars 86 forks source link

How to configure to link with static option #39

Open jlconlin opened 7 years ago

jlconlin commented 7 years ago

If I invoke CMake with this command:

make -D static_libraries=True -D static_njoy=True../

then I don't get statically linked libraries as I would think. The command ldd (otool -L on Mac) lists the libraries that are statically linked and includes (on Linux gcc 6.3):

linux-vdso.so.1 =>  (0x00007ffc8ddfd000)
libgfortran.so.3 => /opt/local/packages/gcc/6.3.0/lib64/libgfortran.so.3 (0x00007fc9f68b9000)
libm.so.6 => /lib64/libm.so.6 (0x000000305f800000)
libgcc_s.so.1 => /opt/local/packages/gcc/6.3.0/lib64/libgcc_s.so.1 (0x00007fc9f667c000)
libquadmath.so.0 => /opt/local/packages/gcc/6.3.0/lib64/libquadmath.so.0 (0x00007fc9f643d000)
libc.so.6 => /lib64/libc.so.6 (0x000000305f400000)
/lib64/ld-linux-x86-64.so.2 (0x000000305f000000)

On my Mac (gcc 7.2) I get:

njoy:
  /usr/local/opt/gcc/lib/gcc/7/libgfortran.4.dylib (compatibility version 5.0.0, current version 5.0.0)
  /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.0.0)
  /usr/local/lib/gcc/7/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
  /usr/local/opt/gcc/lib/gcc/7/libquadmath.0.dylib (compatibility version 1.0.0, current version 1.0.0)
paulromano commented 7 years ago

You probably need to add -static-libgfortran and -static-libgcc on the link line. Don't know if you can get libquadmath statically linked though.

whaeck commented 7 years ago

You actually need to use an additional CMAKE flag to add "-static" to the linker flags:

-D CMAKE_EXE_LINKER_FLAGS="-static"

I had to do this when I was compiling njoy at IRSN.

jlconlin commented 7 years ago

@whaeck this seems to do the trick—at least on Linux. On my Mac, I get:

$ cmake -D static_libraries=TRUE -D CMAKE_EXE_LINKER_FLAGS="-static" ../
-- The Fortran compiler identification is GNU 7.2.0
-- Checking whether Fortran compiler has -isysroot
-- Checking whether Fortran compiler has -isysroot - yes
-- Checking whether Fortran compiler supports OSX deployment target flag
-- Checking whether Fortran compiler supports OSX deployment target flag - yes
-- Check for working Fortran compiler: /usr/local/bin/gfortran
-- Check for working Fortran compiler: /usr/local/bin/gfortran  -- broken
CMake Error at /usr/local/Cellar/cmake/3.7.2/share/cmake/Modules/CMakeTestFortranCompiler.cmake:44 (message):
  The Fortran compiler "/usr/local/bin/gfortran" is not able to compile a
  simple test program.

  It fails with the following output:

   Change Dir: /Users/jlconlin/NJOY21/Code/NJOY2016/bin/CMakeFiles/CMakeTmp

  Run Build Command:"/usr/bin/make" "cmTC_9dcd7/fast"

  /Applications/Xcode.app/Contents/Developer/usr/bin/make -f
  CMakeFiles/cmTC_9dcd7.dir/build.make CMakeFiles/cmTC_9dcd7.dir/build

  Building Fortran object CMakeFiles/cmTC_9dcd7.dir/testFortranCompiler.f.o

  /usr/local/bin/gfortran -isysroot
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk
  -mmacosx-version-min=10.12 -c
  /Users/jlconlin/NJOY21/Code/NJOY2016/bin/CMakeFiles/CMakeTmp/testFortranCompiler.f
  -o CMakeFiles/cmTC_9dcd7.dir/testFortranCompiler.f.o

  Linking Fortran executable cmTC_9dcd7

  /usr/local/Cellar/cmake/3.7.2/bin/cmake -E cmake_link_script
  CMakeFiles/cmTC_9dcd7.dir/link.txt --verbose=1

  /usr/local/bin/gfortran -static -isysroot
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk
  -mmacosx-version-min=10.12
  CMakeFiles/cmTC_9dcd7.dir/testFortranCompiler.f.o -o cmTC_9dcd7

  ld: library not found for -lcrt0.o

  collect2: error: ld returned 1 exit status

  make[1]: *** [cmTC_9dcd7] Error 1

  make: *** [cmTC_9dcd7/fast] Error 2

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:5 (project)

-- Configuring incomplete, errors occurred!
See also "/Users/jlconlin/NJOY21/Code/NJOY2016/bin/CMakeFiles/CMakeOutput.log".
See also "/Users/jlconlin/NJOY21/Code/NJOY2016/bin/CMakeFiles/CMakeError.log".
whaeck commented 7 years ago

I have a similar problem on my linux. I will try adjust the LD_LIBRARY_PATH to see if that fixes it. I will update as required.

whaeck commented 6 years ago

I just tried the following on snow and it allows me to build a static executable (i.e. ldd njoy will tell me that it is not a dynamic executable): cmake -Dstatic_libraries=ON -Dstatic_njoy=ON -DCMAKE_EXE_LINKER_FLAGS=-static ../