astamm / nloptr

nloptr provides an R interface to NLopt, a free/open-source library for nonlinear optimization providing a common interface to a number of different optimization routines which can handle nonlinear constraints and lower and upper bounds for the controls.
https://astamm.github.io/nloptr/
Other
105 stars 35 forks source link

Compiled nlopt version 2.4 for windows is using the wrong compiler from RTools #72

Closed stefanoborini closed 2 years ago

stefanoborini commented 3 years ago

I am trying to install nloptr 1.2.1, but on windows it does not compile correctly. The errors happen at linking and they contain messages such as the following

  1. undefined reference to __gxx_personality_sj
  2. undefined reference to _Unwind_SjLj_Register
  3. undefined reference to _Unwind_SjLj_Unregister
  4. undefined reference to _Unwind_SjLj_Resume

This stackoverflow post seems to indicate that the NLopt package has been compiled on gcc, but RTools 3.6.3 provides the mingw compiler, so the linking does not work.

jyypma commented 3 years ago

Did you compile NLopt from source?

On Mon, Mar 29, 2021 at 4:13 PM Stefano Borini @.***> wrote:

I am trying to install nloptr 1.2.1, but on windows it does not compile correctly. The errors happen at linking and they contain messages such as the following

  1. undefined reference to __gxx_personality_sj
  2. undefined reference to _Unwind_SjLj_Register
  3. undefined reference to _Unwind_SjLj_Unregister
  4. undefined reference to _Unwind_SjLj_Resume

This stackoverflow post https://stackoverflow.com/questions/7751640/undefined-reference-to-gxx-personality-sj0 seems to indicate that the NLopt package has been compiled on gcc, but RTools 3.6.3 provides the mingw compiler, so the linking does not work.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jyypma/nloptr/issues/72, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA4U6ADXBN4YHIQ4EEBJM23TGCKJJANCNFSM4Z7VOUXA .

eddelbuettel commented 3 years ago

@stefanoborini generally you should just let the package do its thing which, especially on Windows, is very standardized, tested and reliable. As you can see at CRAN it builds on Windows (and for this post we can ignore the test error on Windows there for a second).

stefanoborini commented 3 years ago

@eddelbuettel I can't. I am running production here, and I have my own resolver for dependencies. CRAN only stores the compiled packages for the latest package. If they upgrade, they break our build.

stefanoborini commented 3 years ago

@jyypma no. I downloaded the NLopt 2.4 binary build as suggested by the readme.

eddelbuettel commented 3 years ago

@stefanoborini I don't follow. Even if you choose your own "resolver" and you use old versions, those all built at CRAN at the time. What @jyypma and I are trying to convey is that it looks like you are overriding / switching compilers and breaking things in doing so. The SO post you point at is useless (~ 11 years old) yet that type of linker error is seen when you, say, mix and match with different compilers, or otherwise forget to link with the C++ library (say call gcc when you needed to call g++).

Just trust the process and R CMD INSTALL nlopt* for whichever version you have. For Windows, you can also download an external version but you still must plug it in where the src/Makevars.win expects them --- and use the exact same compiler R uses. That has never been different in the 25 years I used R.

Good luck.

stefanoborini commented 3 years ago

@eddelbuettel I am not. I literally downloaded NLopt version 2.4 compiled for windows as indicated here

https://github.com/jyypma/nloptr/blob/master/INSTALL.windows

Then I ran R CMD build on the downloaded nloptr 1.2.2.2 package. I am using R 3.6.3 and RTools35 (which is the one available for R 3.6.3). I have not changed anything. I am following the process strictly as documented and that's what I get. I have no idea why it works at CRAN.

stefanoborini commented 3 years ago

@eddelbuettel This is the actual screenshot of R CMD build execution

Screenshot 2021-04-01 at 10 26 19

This is the version of RTools I am using

Screenshot 2021-04-01 at 10 27 52

And this is the content of the nlopt.pc file I find in NLOpt 2.4, which I put on C:\tmp\nlopt

Screenshot 2021-04-01 at 10 30 26

Windows is a windows 10 machine with nothing else installed. It's out of the box, plus R and Rtools. I am using cmd.exe, despite the powershell window, and I did set NLOPT_HOME to c:/tmp/nlopt

jyypma commented 3 years ago

Some people have had success with the binaries from https://github.com/rwinlib/nlopt The directory structure is different than the one used by the current nloptr Makevars.win file, so you would have to re-arrange the files a bit.

The other option is to install NLopt from source as mentioned in the INSTALL.windows file. This would allow you to make sure that the correct compiler is used. If you happen to be successful, then a patch to the description in INSTALL.windows would of course be much appreciated.

On Thu, Apr 1, 2021 at 11:31 AM Stefano Borini @.***> wrote:

@eddelbuettel https://github.com/eddelbuettel This is the actual screenshot of R CMD build execution

[image: Screenshot 2021-04-01 at 10 26 19] https://user-images.githubusercontent.com/132967/113273601-b9088f00-92d4-11eb-9c78-321f2739014d.png

This is the version of RTools I am using

[image: Screenshot 2021-04-01 at 10 27 52] https://user-images.githubusercontent.com/132967/113274178-4c41c480-92d5-11eb-9f90-e75cd9e3f85f.png

And this is the content of the nlopt.pc file I find in NLOpt 2.4

[image: Screenshot 2021-04-01 at 10 30 26] https://user-images.githubusercontent.com/132967/113274228-5bc10d80-92d5-11eb-9a85-58585a71c54a.png

Windows is a windows 10 machine with nothing else installed. It's out of the box, plus R and Rtools.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jyypma/nloptr/issues/72#issuecomment-811783293, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA4U6AEZU4HOHKWA4CCUV6DTGQ4Q5ANCNFSM4Z7VOUXA .

eddelbuettel commented 3 years ago

@stefanoborini You need figure out where your setup is different from CRAN. They are are not all that generous with details but there is this page. We are not privvy to any more information than you.

stefanoborini commented 3 years ago

@eddelbuettel my setup is a vanilla machine. The machines you are indicating are using GCC, not the RTools compiler, so you are intentionally breaking ABI compatibility with other packages and I am surprised it even works. RTools is the standard compiler used to compile R source packages. By compiling the package with GCC, the binary packages provided by CRAN have a completely different ABI compared to packages compiled in place for which there's no binary on cran or GCC available locally.

stefanoborini commented 3 years ago

@jyypma how did you compile the package provided at http://www.ucl.ac.uk/~uctpjyy/downloads/NLopt-2.4-win-build.zip ? Did you overwrite it with one compiled with the latest Rtools 4.0?

jyypma commented 3 years ago

The build you link to was created a few years ago. On windows I use Rtools in combination with MSYS (to have access to Linux commands such as configure). The script below I have used in the past to build a version of NLopt using MSYS. It defines the compilers specified in Rtools when configuring the NLopt build. Hope this helps.

===Contents of NLoptInstall.sh===

!/bin/bash

R_HOME="C:/tools/R/R-3.3.1"

R_ARCH="i386"

R_ARCH="x64"

NLOPT_SRC_DIR="C:/work/projects/R/nloptr/nlopt-2.4.2" NLOPT_LIB_DIR="C:/work/projects/R/nloptr/nlopt-2.4.2-build"

PATH="C:\tools\R\Rtools\bin;C:\tools\R\Rtools\mingw_64\bin;${PATH}"

Create directory for output

mkdir ${NLOPT_LIB_DIR} mkdir ${NLOPT_LIB_DIR}/${R_ARCH}

Start in source directory

cd ${NLOPT_SRC_DIR}

Get R compilers and flags

CC="${R_HOME}/bin/${R_ARCH}/R.exe" CMD config CC CFLAGS="${R_HOME}/bin/${R_ARCH}/R.exe" CMD config CFLAGS

CPP="${R_HOME}/bin/${R_ARCH}/R.exe" CMD config CPP CPPFLAGS="${R_HOME}/bin/${R_ARCH}/R.exe" CMD config CPPFLAGS

CXX="${R_HOME}/bin/${R_ARCH}/R.exe" CMD config CXX

CXX="C:/tools/R/Rtools/mingw_64/bin/g++.exe" CXXFLAGS="${R_HOME}/bin/${R_ARCH}/R.exe" CMD config CXXFLAGS

CXXCPP="${R_HOME}/bin/${R_ARCH}/R.exe" CMD config CXXCPP

F77="${R_HOME}/bin/${R_ARCH}/R.exe" CMD config F77 FFLAGS="${R_HOME}/bin/${R_ARCH}/R.exe" CMD config FFLAGS

echo "./configure --prefix=\"${NLOPT_LIB_DIR}/${R_ARCH}\" --disable-shared --enable-static --without-octave --without-matlab --without-guile --without-python --with-cxx CC=\"${CC}\" ADD_CFLAGS=\"${CFLAGS}\" CPP=\"${CPP}\" ADD_CPPFLAGS=\"${CPPFLAGS}\" CXX=\"${CXX}\" ADD_CXXFLAGS=\"${CXXFLAGS}\" CXXCPP=\"${CXXCPP}\" F77=\"${F77}\" ADD_FFLAGS=\"${FFLAGS}\""

Configure

./configure --prefix="${NLOPT_LIB_DIR}/${R_ARCH}" --disable-shared --enable-static --without-octave --without-matlab --without-guile --without-python --with-cxx CC="${CC}" ADD_CFLAGS="${CFLAGS}" CPP="${CPP}" ADD_CPPFLAGS="${CPPFLAGS}" CXX="${CXX}" ADD_CXXFLAGS="${CXXFLAGS}" CXXCPP="${CXXCPP}" F77="${F77}" ADD_FFLAGS="${FFLAGS}"

Compile

make make install

On Thu, Apr 8, 2021 at 12:29 PM Stefano Borini @.***> wrote:

@jyypma https://github.com/jyypma how did you compile the package provided at http://www.ucl.ac.uk/~uctpjyy/downloads/NLopt-2.4-win-build.zip ? Did you overwrite it with one compiled with the latest Rtools 4.0?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jyypma/nloptr/issues/72#issuecomment-815648411, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA4U6ABAIUOO56LSGLB6UETTHWAQPANCNFSM4Z7VOUXA .

astamm commented 2 years ago

@stefanoborini can you install the latest 2.0.0 release to fit your needs?

stefanoborini commented 2 years ago

@astamm I don't know. I gave up on this.

astamm commented 2 years ago

Oh I see. No problem. Should I close the issue or do you intend to give it one more try?

stefanoborini commented 2 years ago

you can close it I guess. Thanks