Using lex in a shared library

Philip Herron redbrain at gcc.gnu.org
Fri Jul 2 22:53:24 UTC 2010


On 2 July 2010 23:31, Matthew Fleming <mdf356 at gmail.com> wrote:
> On Fri, Jul 2, 2010 at 2:54 PM, Philip Herron <redbrain at gcc.gnu.org> wrote:
>> On 2 July 2010 22:51, Matthew Fleming <mdf356 at gmail.com> wrote:
>>> I have the following Makefile for a shared library at $work:
>>>
>>> ISI_TOP=        ../..
>>>
>>> LIB=            isi_date
>>> SHLIB_MAJOR=    1
>>> SHLIB_MINOR=    0
>>> SRCS=           date.c date_parser.new.c lex.yy.c
>>> INCS=           date.h
>>> INCLUDEDIR=     /usr/include/isi_date
>>>
>>> YFLAGS+=        -vt
>>> FLEX=           /usr/bin/flex
>>> LDADD=          -ll
>>>
>>> CLEANFILES+=    date_parser.new.c y.tab.h y.tab.c lex.yy.c y.output \
>>>                check_date.log test
>>>
>>> lex.yy.c: date_lexer.new.l
>>>        ${FLEX} $>
>>>
>>> CFLAGS+=        -I${.CURDIR}
>>> #CFLAGS+=       -g
>>>
>>> .include "${ISI_TOP}/isi.lib.mk"
>>>
>>>
>>>
>>> This builds fine as on i386.  I'm trying to get all our user-space to
>>> be 64-bit clean, and I run into an error when building on amd64:
>>>
>>> /data/sb/BR_MDF_64CLEAN/obj/data/sb/BR_MDF_64CLEAN/src/tmp/usr/bin/ld:
>>> /data/sb/BR_MDF_64CLEAN/obj/data/sb/BR_MDF_64CLEAN/src/tmp/usr/lib/libl.a(libyywrap.o):
>>> relocation R_X86_64_32 can not be used when making a shared object;
>>> recompile with -fPIC
>>> /data/sb/BR_MDF_64CLEAN/obj/data/sb/BR_MDF_64CLEAN/src/tmp/usr/lib/libl.a:
>>> could not read symbols: Bad value
>>>
>>> The following diff makes the compile work, but I have no idea (yet)
>>> whether this will run, if it's the right solution, etc.
>>>
>>>
>>> Index: usr.bin/lex/lib/Makefile
>>> ===================================================================
>>> --- usr.bin/lex/lib/Makefile    (revision 153343)
>>> +++ usr.bin/lex/lib/Makefile    (working copy)
>>> @@ -4,11 +4,16 @@
>>>
>>>  LIB=    ln
>>>  SRCS=   libmain.c libyywrap.c
>>> -NO_PIC=
>>> +#NO_PIC=
>>>
>>> +SHLIB_MAJOR=   1
>>> +SHLIB_MINOR=   0
>>> +
>>>  .if ${MK_INSTALLLIB} != "no"
>>>  LINKS=  ${LIBDIR}/libln.a ${LIBDIR}/libl.a
>>>  LINKS+=        ${LIBDIR}/libln.a ${LIBDIR}/libfl.a
>>> +LINKS+=        ${LIBDIR}/libln.so ${LIBDIR}/libl.so
>>> +LINKS+=        ${LIBDIR}/libln${LIB_SUFFIX}.so ${LIBDIR}/libl${LIB_SUFFIX}.so
>>>  .endif
>>>
>>>  .if ${MK_PROFILE} != "no"
>>
>> Although maybe not helpful but have you considered using
>> automake/libtool instead makes it so much simpler in my opinion.
>
> Instead of... ?  Instead of this makefile?
>
> Thanks,
> matthew
>

Yeah to create your self a shared library using automake:

$ rm Makefile
$ cat >> Makefile.am <<EOF
AM_YFLAGS = -d

lib_LTLIBRARIES = lib<yourlibname>.la

libcrules_la_LDFLAGS = -release 0.5.0
libcrules_la_SOURCES = ss_parser.y \
ss_lexical.l \
bb_backend.c \
...
EOF

Automake will auto-handle Lex and Yacc files too. And is extremely portable.

$ automake --gnu --add-missing --copy --force

This will generate you all the Makefie.in files recursively over your
work dir. When building these Makefile.in you only need do it once
really, unless you change your makefile.am files. The makefiles.in are
designed to be portable. All they need is some configuration though
autoconf to tell it what compiler to use etc.

--Phil


More information about the freebsd-hackers mailing list