linux下基于官方源码编译ipopt

linux下基于官方源码编译ipopt

1、C++依赖项安装升级

由于需要编译c++所以需要安装一系列的依赖:

shell 复制代码
apt-get update
apt-get -y upgrade
apt install build-essential
apt-get install -y gcc g++ gfortran git patch wget pkg-config liblapack-dev libmetis-dev libblas-dev vim

2、下载需要编译的代码文件

总共有5个重要的文件包:

1. Ipopt :https://github.com/coin-or/Ipopt.git
2. ThirdParty-ASL : https://github.com/coin-or-tools/ThirdParty-ASL.git
3. ThirdParty-HSL : https://github.com/coin-or-tools/ThirdParty-HSL.git
4. ThirdParty-Mumps : https://github.com/coin-or-tools/ThirdParty-Mumps.git
5. coinhsl.zip : 为ThirdParty-HSL编译的依赖项,其官网为: [Coin-HSL available from STFC IP Store](https://licences.stfc.ac.uk/product/coin-hsl) ,该文件下载比较麻烦且很难查找的到

3、编译文件

  1. 编译ThirdParty-ASL

    shell 复制代码
    cd ThirdParty-ASL || exit
    ./get.ASL
    ./configure
    make
    make install
  2. 编译ThirdParty-HSL

    1. 将coinhsl.zip解压到ThirdParty-HSL

      shell 复制代码
      cp coinhsl.zip ThirdParty-HSL
      cd ThirdParty-HSL || exit
      unzip coinhsl.zip
    2. 编译

      shell 复制代码
      ./configure
      make
      make install
  3. 编译ThirdParty-Mumps

    cd ThirdParty-Mumps || exit
    ./get.Mumps
    ./configure
    make
    make install
    
  4. 编译Ipopt

    shell 复制代码
    cd Ipopt || exit
    mkdir build
    cd build || exit
    ../configure
    make
    make test
    make install

4、配置环境变量

shell 复制代码
cd /usr/local/include || exit
cp coin-or coin -r
ln -s /usr/local/lib/libcoinmumps.so.3 /usr/lib/libcoinmumps.so.3
ln -s /usr/local/lib/libcoinhsl.so.2 /usr/lib/libcoinhsl.so.2
ln -s /usr/local/lib/libipopt.so.3 /usr/lib/libipopt.so.3
echo "Add the/usr/local/lib directory to the configuration file of the shared library"
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig

5、一键式操作脚本

  1. 预先下载文件:夸克链接:https://pan.quark.cn/s/8928a21eaf0b
  2. 在文件夹Ipopt_pkg下,存放提供的两个压缩文件:coinhsl.zip ; Ipopt_pkg.zip
  3. 在Ipopt_pkg文件夹的同级地址下执行脚本depoly_ipopt.sh

文件结构树:

-- Ipopt_pkg
	- coinhsl.zip
	- Ipopt_pkg.zip
-- depoly_ipopt.sh

depoly_ipopt.sh :

shell 复制代码
#!/bin/bash
echo "Compile ipopt and dependencies:" # echo is used to printf in terminal
echo "dependencies: gcc g++ gfortran git patch wget pkg-config liblapack-dev libmetis-dev libblas-dev"
apt-get update
apt-get -y upgrade
apt install build-essential
apt-get install -y gcc g++ gfortran git patch wget pkg-config liblapack-dev libmetis-dev libblas-dev vim
echo "===========================dependencies: over====================================="
if [ ! -d "/Ipopt_pkg" ]; then
  mkdir /Ipopt_pkg
fi
cd Ipopt_pkg || exit
unzip Ipopt_pkg.zip
echo "===========================make ASL==========================="
if [ ! -d "/ThirdParty-ASL" ]; then
  git clone https://github.com/coin-or-tools/ThirdParty-ASL.git
fi
cd ThirdParty-ASL || exit
./get.ASL
./configure
make
make install
cd ..
echo "===========================make HSL==========================="
if [ ! -d "/ThirdParty-HSL" ]; then
  git clone https://github.com/coin-or-tools/ThirdParty-HSL.git
fi

if [ ! -d "/ThirdParty-HSL/coinhsl.zip" ]; then
  cp coinhsl.zip ThirdParty-HSL  # 将预先下载好的线性求解器编译文件压缩包添加到文件夹ThirdParty-HSL内
fi

cd ThirdParty-HSL || exit
unzip coinhsl.zip
./configure
make
make install
cd ..
echo "===========================make Mumps==========================="
if [ ! -d "/ThirdParty-Mumps" ]; then
  git clone https://github.com/coin-or-tools/ThirdParty-Mumps.git
fi
cd ThirdParty-Mumps || exit
./get.Mumps
./configure
make
make install
cd ..
echo "===========================make Ipopt==========================="
if [ ! -d "/Ipopt" ]; then
  git clone https://github.com/coin-or/Ipopt.git
fi
cd Ipopt || exit
mkdir build
cd build || exit
../configure
make
make test
make install
echo "===========================Improve the environment==========================="
cd /usr/local/include || exit
cp coin-or coin -r
ln -s /usr/local/lib/libcoinmumps.so.3 /usr/lib/libcoinmumps.so.3
ln -s /usr/local/lib/libcoinhsl.so.2 /usr/lib/libcoinhsl.so.2
ln -s /usr/local/lib/libipopt.so.3 /usr/lib/libipopt.so.3
echo "Add the/usr/local/lib directory to the configuration file of the shared library"
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig
echo "===========================Compile ipopt and dependencies: over!==========================="