张杰部落格
博客信息

编译 xgboost 不编译 xgboost4j 下的JNI库libxgboost4j.so的问题解决

发布时间:『 2020-01-03 19:44』  博客类别:Hadoop/Spark  阅读(546) 评论(0)
cmake 3.14.6
gcc  4.8.5
xgboost 0.72

今天照着官网编译xgboost的源码,发现一个问题,xgboost4j下的native包没有编译。

git clone --recursive https://github.com/dmlc/xgboostcd xgboost
mkdir buildcd build
cmake ..
make -j4

于是查看Makefile文件中的命令:

lib/libxgboost.a: $(ALL_DEP)
        @mkdir -p $(@D)
        ar crv $@ $(filter %.o, $?)

lib/xgboost.dll lib/libxgboost.so lib/libxgboost.dylib: $(ALL_DEP)
        @mkdir -p $(@D)
        $(CXX) $(CFLAGS) -shared -o $@ $(filter %.o %a,  $^) $(LDFLAGS)

jvm-packages/lib/libxgboost4j.so: jvm-packages/xgboost4j/src/native/xgboost4j.cpp $(ALL_DEP)
        @mkdir -p $(@D)
        $(CXX) $(CFLAGS) $(JAVAINCFLAGS) -shared -o $@ $(filter %.cpp %.o %.a, $^) $(LDFLAGS)

代码中是有编译的,但是最终没有生成jvm-packages/lib/libxgboost4j.so。非常的困惑。

于是照着生成lib/libxgboost.so的命令:

g++ -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -shared -o lib/libxgboost.so build/logging.o build/learner.o build/predictor/predictor.o build/predictor/cpu_predictor.o build/common/host_device_vector.o build/common/common.o build/common/hist_util.o build/metric/metric.o build/metric/rank_metric.o build/metric/elementwise_metric.o build/metric/multiclass_metric.o build/objective/multiclass_obj.o build/objective/objective.o build/objective/rank_obj.o build/objective/regression_obj.o build/data/sparse_page_dmatrix.o build/data/sparse_page_source.o build/data/sparse_page_writer.o build/data/simple_csr_source.o build/data/data.o build/data/sparse_page_raw_format.o build/data/simple_dmatrix.o build/tree/updater_prune.o build/tree/tree_updater.o build/tree/updater_histmaker.o build/tree/updater_refresh.o build/tree/tree_model.o build/tree/updater_sync.o build/tree/updater_skmaker.o build/tree/updater_colmaker.o build/tree/updater_fast_hist.o build/gbm/gbtree.o build/gbm/gblinear.o build/gbm/gbm.o build/c_api/c_api.o build/c_api/c_api_error.o build/linear/linear_updater.o build/linear/updater_shotgun.o build/linear/updater_coordinate.o dmlc-core/libdmlc.a rabit/lib/librabit.a -pthread -lm  -fopenmp -lrt  -lrt

libxgboost.so是生成成功的,并且在日志里看到上面的信息。

从代码里看到生成libxgboost.so的代码和生成libxgboost4j.so的代码,除了中间的${JAVAINCFLAGS} 和 后面的 %.cpp外,都一样。所以先找到了${JAVAINCFLAGS}

export JAVAINCFLAGS = -I${JAVA_HOME}/include -I./java

于是知道了JAVAINCFLAGS的值就是引用了jdk的目录。

组装生成libxgboost4j.so的g++命令:

g++ -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -I/wls/hadoop/soft/java/include -I./java -shared -o jvm-packages/lib/libxgboost4j.so build/logging.o build/learner.o build/predictor/predictor.o build/predictor/cpu_predictor.o build/common/host_device_vector.o build/common/common.o build/common/hist_util.o build/metric/metric.o build/metric/rank_metric.o build/metric/elementwise_metric.o build/metric/multiclass_metric.o build/objective/multiclass_obj.o build/objective/objective.o build/objective/rank_obj.o build/objective/regression_obj.o build/data/sparse_page_dmatrix.o build/data/sparse_page_source.o build/data/sparse_page_writer.o build/data/simple_csr_source.o build/data/data.o build/data/sparse_page_raw_format.o build/data/simple_dmatrix.o build/tree/updater_prune.o build/tree/tree_updater.o build/tree/updater_histmaker.o build/tree/updater_refresh.o build/tree/tree_model.o build/tree/updater_sync.o build/tree/updater_skmaker.o build/tree/updater_colmaker.o build/tree/updater_fast_hist.o build/gbm/gbtree.o build/gbm/gblinear.o build/gbm/gbm.o build/c_api/c_api.o build/c_api/c_api_error.o build/linear/linear_updater.o build/linear/updater_shotgun.o build/linear/updater_coordinate.o dmlc-core/libdmlc.a rabit/lib/librabit.a jvm-packages/xgboost4j/src/native/xgboost4j.cpp -pthread -lm  -fopenmp -lrt  -lrt

发现报错:

In file included from jvm-packages/xgboost4j/src/native/./xgboost4j.h:2:0,
                 from jvm-packages/xgboost4j/src/native/xgboost4j.cpp:19:
/wls/hadoop/soft/java/include/jni.h:45:20: fatal error: jni_md.h: No such file or directory
 #include "jni_md.h"
                    ^
compilation terminated                      ^

终于知道为什么make的时候,没有编译libxgboost4j.so了。原来是报错了。可能make未打印报错信息。于是,寻找问题答案,找到了一个解决办法:

https://stackoom.com/question/3Ylqj/Makefile-%E8%87%B4%E5%91%BD%E9%94%99%E8%AF%AF-jni-h-%E6%B2%A1%E6%9C%89%E8%BF%99%E6%A0%B7%E7%9A%84%E6%96%87%E4%BB%B6%E6%88%96%E7%9B%AE%E5%BD%95

原来是linux的jdk的jni在include/linux目录下,于是修改命令:

g++ -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude   -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -I/wls/hadoop/soft/java/include -I/wls/hadoop/soft/java/include/linux -I./java -shared -o jvm-packages/lib/libxgboost4j.so build/logging.o build/learner.o build/predictor/predictor.o build/predictor/cpu_predictor.o build/common/host_device_vector.o build/common/common.o build/common/hist_util.o build/metric/metric.o build/metric/rank_metric.o build/metric/elementwise_metric.o build/metric/multiclass_metric.o build/objective/multiclass_obj.o build/objective/objective.o build/objective/rank_obj.o build/objective/regression_obj.o build/data/sparse_page_dmatrix.o build/data/sparse_page_source.o build/data/sparse_page_writer.o build/data/simple_csr_source.o build/data/data.o build/data/sparse_page_raw_format.o build/data/simple_dmatrix.o build/tree/updater_prune.o build/tree/tree_updater.o build/tree/updater_histmaker.o build/tree/updater_refresh.o build/tree/tree_model.o build/tree/updater_sync.o build/tree/updater_skmaker.o build/tree/updater_colmaker.o build/tree/updater_fast_hist.o build/gbm/gbtree.o build/gbm/gblinear.o build/gbm/gbm.o build/c_api/c_api.o build/c_api/c_api_error.o build/linear/linear_updater.o build/linear/updater_shotgun.o build/linear/updater_coordinate.o dmlc-core/libdmlc.a rabit/lib/librabit.a jvm-packages/xgboost4j/src/native/xgboost4j.cpp -pthread -lm  -fopenmp -lrt  -lrt

编译成功!只是增加:

-I/wls/hadoop/soft/java/include/linux
#/wls/hadoop/soft/java/为JAVA_HOME目录

xgboost4j太多的坑了。

关键字:   xgboost4j  
评论信息
暂无评论
发表评论
验证码: 
Powered by IMZHANGJIE.CN Copyright © 2015-2021 粤ICP备14056181号