linux上Ocaml-makefile的一个报错

问题遇到的现象和发生背景

想要用别人编写好的程序,使用make,报了一下错误。尝试用了-fPIC 和-no-pie 都没有用,我猜测可能用的是Ocaml-makefile所以不管用,没有没兄弟精通这一块的,帮忙看看吖。下面是makefile文件

用代码块功能插入代码,请勿粘贴截图
#*********************************************************************#
#                                                                     #
#                           Objective Caml                            #
#                                                                     #
#            Pierre Weis, projet Cristal, INRIA Rocquencourt          #
#                                                                     #
# Copyright 1998, 2004 Institut National de Recherche en Informatique #
# et en Automatique. Distributed only by permission.                  #
#                                                                     #
#*********************************************************************#

#                   Generic Makefile for Objective Caml Programs

# $Id: Exp $

############################ Documentation ######################
#
# To use this Makefile:
# -- You must fix the value of the variable SOURCES below.
# (The variable SOURCES is the list of your Caml source files.)
# -- You must create a file .depend, using
# $touch .depend
# (This file will contain the dependancies between your Caml modules,
#  automatically computed by this Makefile.)

# Usage of this Makefile:
# To incrementally recompile the system, type
#     make
# To recompute dependancies between modules, type
#     make depend
# To remove the executable and all the compiled files, type
#     make clean
# To compile using the native code compiler
#     make opt
#
##################################################################


##################################################################
#
# Advanced usage:
# ---------------

# If you want to fix the name of the executable, set the variable
# EXEC, for instance, if you want to obtain a program named my_prog:
EXEC = ukano

# If you need special libraries provided with the Caml system,
# (graphics, arbitrary precision numbers, regular expression on strings, ...),
# you must set the variable LIBS to the proper set of libraries. For
# instance, to use the graphics library set LIBS to $(WITHGRAPHICS):
# LIBS = $(WITHGRAPHICS)

# You may use any of the following predefined variable
# WITHGRAPHICS : provides the graphics library
# WITHUNIX : provides the Unix interface library
# WITHSTR : provides the regular expression string manipulation library
# WITHNUMS : provides the arbitrary precision arithmetic package
# WITHTHREADS : provides the byte-code threads library
# WITHDBM : provides the Data Base Manager library
#
#
########################## End of Documentation ####################



########################## User's variables #####################
#
# The Caml sources (including camlyacc and camllex source files)

SOURCES_parse = parser.mly lexer.mll pitparser.mly pitlexer.mll
SOURCES = $(shell ocamldep -sort $(wildcard *.ml))

# The executable file to generate (default a.out under Unix)
EXEC = ukano


########################## Advanced user's variables #####################
#
# The Caml compilers.
# You may fix here the path to access the Caml compiler on your machine
CAMLC = ocamlc
CAMLOPT = ocamlopt
CAMLDEP = ocamldep
CAMLLEX = ocamllex
CAMLYACC = ocamlyacc

# The list of Caml libraries needed by the program
# For instance:
# LIBS=$(WITHGRAPHICS) $(WITHUNIX) $(WITHSTR) $(WITHNUMS) $(WITHTHREADS)\
# $(WITHDBM)

LIBS=str.cma unix.cma

# Should be set to -custom if you use any of the libraries above
# or if any C code have to be linked with your program
# (irrelevant for ocamlopt)

CUSTOM=-custom

# Default setting of the WITH* variables. Should be changed if your
# local libraries are not found by the compiler.
WITHGRAPHICS =graphics.cma -cclib -lgraphics -cclib -L/usr/X11R6/lib -cclib -lX11

WITHUNIX =unix.cma -cclib -lunix

WITHSTR =str.cma -cclib -lstr

WITHNUMS =nums.cma -cclib -lnums

WITHTHREADS =threads.cma -cclib -lthreads

WITHDBM =dbm.cma -cclib -lmldbm -cclib -lndbm

################ End of user's variables #####################


##############################################################
################ This part should be generic
################ Nothing to set up or fix here
##############################################################

all: 
    ocamlyacc parser.mly
    ocamllex lexer.mll
    ocamlyacc pitparser.mly
    ocamllex pitlexer.mll
    make build

short:
    make build


build: depend $(EXEC)

opt : $(EXEC).opt

#ocamlc -custom other options graphics.cma other files -cclib -lgraphics -cclib -lX11
#ocamlc -thread -custom other options threads.cma other files -cclib -lthreads
#ocamlc -custom other options str.cma other files -cclib -lstr
#ocamlc -custom other options nums.cma other files -cclib -lnums
#ocamlc -custom other options unix.cma other files -cclib -lunix
#ocamlc -custom other options dbm.cma other files -cclib -lmldbm -cclib -lndbm

SOURCES1 = $(SOURCES:.mly=.ml)
SOURCES2 = $(SOURCES1:.mll=.ml)
OBJS = $(SOURCES2:.ml=.cmo)
OPTOBJS = $(SOURCES2:.ml=.cmx)

$(EXEC): $(OBJS)
    $(CAMLC) $(CUSTOM) -o $(EXEC) $(LIBS) $(OBJS)

$(EXEC).opt: $(OPTOBJS)
    $(CAMLOPT) -o $(EXEC).opt $(LIBS:.cma=.cmxa) $(OPTOBJS)

.SUFFIXES:
.SUFFIXES: .ml .mli .cmo .cmi .cmx .mll .mly

.ml.cmo:
    $(CAMLC) -c $<

.mli.cmi:
    $(CAMLC) -c $<

.ml.cmx:
    $(CAMLOPT) -c $<

.mll.cmo:
    $(CAMLLEX) $<
    $(CAMLC) -c $*.ml

.mll.cmx:
    $(CAMLLEX) $<
    $(CAMLOPT) -c $*.ml

.mly.cmo:
    $(CAMLYACC) $<
    $(CAMLC) -c $*.mli
    $(CAMLC) -c $*.ml

.mly.cmx:
    $(CAMLYACC) $<
    $(CAMLOPT) -c $*.mli
    $(CAMLOPT) -c $*.ml

.mly.cmi:
    $(CAMLYACC) $<
    $(CAMLC) -c $*.mli

.mll.ml:
    $(CAMLLEX) $<

.mly.ml:
    $(CAMLYACC) $<

clean:
    rm -f *.cm[iox] *~ .*~ #*#
    rm -f $(EXEC)
    rm -f $(EXEC).opt

.depend: $(SOURCES2)
    $(CAMLDEP) *.mli *.ml > .depend

depend: $(SOURCES2)
    $(CAMLDEP) *.mli *.ml > .depend

include .depend


运行结果及报错内容

img

relocation R_X86_64_32 against "rodata.str1.1" can not be used when making a PIE object
制作PIE对象时,不能使用针对“rodata.str1.1”的重定位R_X86_64_32
分析:
在生成共享库的工程调用中会报错该库编译时没有使用–fPIC编译
fPIC 作用于编译阶段,告诉编译器产生与位置无关代码(Position-Independent Code),则产生的代码中,没有绝对地址,全部使用相对地址,故而代码可以被加载器加载到内存的任意位置,都可以正确的执行。这正是共享库所要求的,共享库被加载时,在内存的位置不是固定的
解决思路:
出现这个问题检查整个编译过程是否都采用–fPIC的方式编译的应该就可以了
另外提供一个同样报错的实例笔记,供你参考,实例出现该问题是库版本问题:https://www.freesion.com/article/5229856714/
【所以,细心检查下环境匹配也很重要】