一些化学库的从头编译
一些化学库的从头编译
因为python启动有点慢,有些非常常用又简单的小工具笔者实在想转成C++的,于是不得不学习如何编译这些库。
OpenBabel
官网给出的下载链接直通2.4.0源码,令人忍俊不禁。真正的源码要在github上搞到,目前的发行版是3.1.1,下载下来之后解压:
1
tar -jxvf openbabel-3.1.1-source.tar.bz2
创建build文件夹,运行cmake:
1
2
3
4
cd openbabel-3.1.1
mkdir build
cd build
cmake ..
提示cmake版本过低,好在服务器上有现成的cmake3.23(犹记当时不知道服务器有这些高版本的软件,手搓cmake的经历,然后发现编译cmake时报错gcc版本过低👽)
切换3.23后cmake成功了,开始编译。
1
make -j4
少有的一遍过,很顺利,就是时间有点久,花了得有20分钟
1
2
3
4
5
6
[100%] Building CXX object test/CMakeFiles/test_runner.dir/unitcell.cpp.o
[100%] Building CXX object test/CMakeFiles/test_runner.dir/cmlreadfile.cpp.o
[100%] Building CXX object test/CMakeFiles/test_runner.dir/obtest.cpp.o
[100%] Linking CXX executable ../bin/test_runner
[100%] Built target test_runner
(obabel) [gaus@login02 build]$
安装二进制文件:
1
make install
除了没权限写入/usr/local失败以外,安装是成功的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[ 87%] Built target obspectrophore
Consolidate compiler generated dependencies of target test_runner
[ 99%] Built target test_runner
Consolidate compiler generated dependencies of target test_inchiwrite
[100%] Built target test_inchiwrite
Consolidate compiler generated dependencies of target roundtrip
[100%] Built target roundtrip
Install the project...
-- Install configuration: "Release"
CMake Error at cmake_install.cmake:46 (file):
file cannot create directory: /usr/local/include/openbabel3/openbabel.
Maybe need administrative privileges.
make: *** [Makefile:110: install] Error 1
(obabel) [gaus@login02 build]$
验证:
1
2
3
4
5
6
7
8
9
(obabel) [gaus@login02 build]$ ./bin/obabel --version
No input file or format spec or possibly a misplaced option.
Most options must come after the input files. (-i -o -O -m can be anywhwere.)
Open Babel 3.1.0 -- Mar 5 2025 -- 18:43:41
Usage:
obabel [-i<input-type>] <infilename> [-o<output-type>] -O<outfilename> [Options]
Try -H option for more information.
(obabel) [gaus@login02 build]$
RDkit
RDkit的引导相对openbabel来说就很齐全了,准备环境:
1
2
3
4
5
conda create -n rdkitmake python=3.9 -y
conda activate rdkitmake
conda install -y cmake cairo pillow eigen pkg-config
conda install -y boost-cpp boost py-boost
conda install -y gxx_linux-64
注意conda install -y gxx_linux-64
这里有坑,见下方报错与解决。
编译:
1
2
3
4
5
6
7
8
9
10
11
12
13
git clone https://github.com/rdkit/rdkit.git
cd rdkit
mkdir build && cd build
cmake -DPy_ENABLE_SHARED=1 \
-DRDK_INSTALL_INTREE=ON \
-DRDK_INSTALL_STATIC_LIBS=ON \
-DRDK_BUILD_CPP_TESTS=ON \
-DPYTHON_NUMPY_INCLUDE_PATH="$(python -c 'import numpy ; print(numpy.get_include())')" \
-DBOOST_ROOT="$CONDA_PREFIX" \
..
make -j16
make install
测试
1
RDBASE=$PWD/.. PYTHONPATH=$RDBASE:$PYTHONPATH LD_LIBRARY_PATH=$RDBASE/lib:$LD_LIBRARY_PATH ctest
报错与解决
boost与gcc版本不兼容
现象:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
In file included from /root/miniconda/envs/rdkitmake/include/boost/cstdfloat.hpp:22,
from /root/miniconda/envs/rdkitmake/include/boost/multiprecision/detail/float128_functions.hpp:15,
...
from /home/bane/scripts/Libs/rdkit/Code/GraphMol/ChemReactions/Enumerate/Enumerate.cpp:43:
/root/miniconda/envs/rdkitmake/include/boost/math/cstdfloat/cstdfloat_limits.hpp:46:13: error: redefinition of 'class std::numeric_limits<__float128>'
46 | class numeric_limits<boost::math::cstdfloat::detail::float_internal128_t>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /root/miniconda/envs/rdkitmake/include/boost/limits.hpp:19,
...
from /home/bane/scripts/Libs/rdkit/Code/GraphMol/ChemReactions/Enumerate/Enumerate.cpp:33:
/root/miniconda/envs/rdkitmake/lib/gcc/x86_64-conda-linux-gnu/14.2.0/include/c++/limits:2089:12: note: previous definition of 'class std::numeric_limits<__float128>'
2089 | struct numeric_limits<__float128>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
make[2]: *** [Code/GraphMol/ChemReactions/CMakeFiles/ChemReactions_static.dir/build.make:272: Code/GraphMol/ChemReactions/CMakeFiles/ChemReactions_static.dir/Enumerate/Enumerate.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:8944: Code/GraphMol/ChemReactions/CMakeFiles/ChemReactions_static.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
版本:
1
2
3
4
5
6
boost 1.82.0 py39h9ff65d1_0 conda-forge
boost-cpp 1.82.0 hdb19cb5_2
py-boost 1.82.0 py39h3c18c91_2
gcc_linux-64 14.2.0 h5910c8f_8 conda-forge
gxx_linux-64 14.2.0 h9423afd_8 conda-forge
解决办法:降级gcc到<10的版本
1
conda install -y gxx_linux-64=9.5.0
没有安装pytest
现象:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
240/242 Test #240: pyFeatures ............................. Passed 0.15 sec
Start 241: pythonProjectTests
241/242 Test #241: pythonProjectTests .....................***Failed 0.02 sec
Start 242: pythonSourceTests
242/242 Test #242: pythonSourceTests ......................***Failed 0.01 sec
99% tests passed, 2 tests failed out of 242
Total Test time (real) = 254.77 sec
The following tests FAILED:
241 - pythonProjectTests (Failed)
242 - pythonSourceTests (Failed)
Errors while running CTest
Output from these tests are in: /home/bane/scripts/Libs/rdkit/build/Testing/Temporary/LastTest.log
Use "--rerun-failed --output-on-failure" to re-run the failed cases verbosely.
(rdkitmake) root@wcy:/home/bane/scripts/Libs/rdkit/build#
解决:如果不修改源码就不用管这俩
This post is licensed under CC BY 4.0 by the author.