Patch for building as library.

Developer
JCip
2007-02-11
2013-05-20
  • JCip

    JCip - 2007-02-11

    Do with this what you will; here's a patch to the Makefile and to tinyxml.h to add a "make lib" rule for building a static library. Go into the same directory as the Makefile, copy the diff below to a file named makelib.diff, and "patch -p1 < makelib.diff" to apply. This is on tinyxml 2.5.2.

    Couple of sketchy things, use your own judgement:

    1) Bash script used in the Makefile. I don't know if make always uses bash/sh or if it just uses the user's current shell. If it's the latter, then that might cause problems for users that use, say, tcsh (in which case they should switch to bash >:). But I don't know, I've never used tcsh -- maybe the commands are the same.

    2) Since this is in library format, if -DTIXML_USE_STL is in the compiler line, it won't be available to applications that use tinyxml.h. To that end, I've removed the -DTIXML_USE_STL thing. Instead, the Makefile generates a little header file named "tinyopt.h" that has the appropriate #define in it, and tinyxml.h now #includes that. That way, TIXML_USE_STL is available to applications. Erm... this is the simplest thing I could come up with that didn't involve using sed to replace text. There's probably a better way.

    3) If TIXML_USE_STL is set, tinystr.h is not placed in the include directory. This seems like it's the right thing to do. Maybe it's not.

    - JC

    Diff begins on the line after this one and goes to the end of this post (crossing my fingers that it shows up correctly):

    diff -ur tinyxml-2.5.2/Makefile tinyxml-2.5.2.mod/Makefile
    --- tinyxml-2.5.2/Makefile    Tue Sep 19 22:04:36 2006
    +++ tinyxml-2.5.2.mod/Makefile    Sun Feb 11 03:21:19 2007
    @@ -52,14 +52,18 @@
        LDFLAGS  := ${LDFLAGS} -pg
    endif

    +OPTHDR := tinyopt.h
    +LIBNAME := libtinyxml.a
    +LIBHDRS := tinyxml.h ${OPTHDR} # tinystr.h included if necessary below.
    +OUTLIBDIR := lib
    +OUTINCDIR := include
    +
    #****************************************************************************
    # Preprocessor directives
    #****************************************************************************

    -ifeq (YES, ${TINYXML_USE_STL})
    -  DEFS := -DTIXML_USE_STL
    -else
    -  DEFS :=
    +ifneq (YES, ${TINYXML_USE_STL})
    +  LIBHDRS := ${LIBHDRS} tinystr.h
    endif

    #****************************************************************************
    @@ -83,8 +87,18 @@

    OUTPUT := xmltest

    +.PHONY: all lib
    +
    all: ${OUTPUT}

    +lib: ${LIBNAME}
    +#    Put stuff in lib & include, let user do rest.
    +    -mkdir -p ${OUTLIBDIR} ${OUTINCDIR}
    +    @cp -v ${LIBNAME} ${OUTLIBDIR}
    +    @cp -v ${LIBHDRS} ${OUTINCDIR}
    +    @echo
    +    @echo "To install, copy the contents of 'lib' and 'include' to"
    +    @echo "a place that you know they will be happy."

    #****************************************************************************
    # Source files
    @@ -101,13 +115,35 @@
    # Output
    #****************************************************************************

    +# Test program
    ${OUTPUT}: ${OBJS}
         ${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIBS} ${EXTRA_LIBS}

    +# Library
    +${LIBNAME}: ${OBJS}
    +    ${AR} ${LIBNAME} ${OBJS}
    +    ${RANLIB} ${LIBNAME}
    +
    +# Option header
    +${OPTHDR}:
    +    @echo "Creating ${OPTHDR}..."
    +    @head tinyxml.h -n 24 > ${OPTHDR}  # license
    +    @echo "#ifndef TINYOPT_INCLUDED" >> ${OPTHDR}
    +    @echo "#define TINYOPT_INCLUDED" >> ${OPTHDR}
    +    @echo "#ifdef TIXML_USE_STL"     >> ${OPTHDR}
    +    @echo "#  undef TIXML_USE_STL"   >> ${OPTHDR}
    +    @echo "#endif"                   >> ${OPTHDR}
    +ifeq (YES, ${TINYXML_USE_STL})
    +    @echo "#define TIXML_USE_STL"    >> ${OPTHDR}
    +endif
    +    @echo "#endif"                   >> ${OPTHDR}
    +
    #****************************************************************************
    # common rules
    #****************************************************************************

    +.PHONY: dist clean depend
    +
    # Rules for compiling source files to object files
    %.o : %.cpp
         ${CXX} -c ${CXXFLAGS} ${INCS} $< -o $@
    @@ -119,12 +155,20 @@
         bash makedistlinux

    clean:
    -    -rm -f core ${OBJS} ${OUTPUT}
    +    -rm -f core ${OBJS} ${OUTPUT} ${LIBNAME} ${OPTHDR} ${OUTLIBDIR}/${LIBNAME}
    +    -for fn in ${LIBHDRS} ; do \ +        rm -f ${OUTINCDIR}/$$fn ; \ +    done
    +    -for dn in ${OUTINCDIR} ${OUTLIBDIR} ; do \ +        if [ -d $$dn ] ; then \ +            rmdir --ignore-fail-on-non-empty $$dn ; \ +        fi ; \ +    done

    depend:
    -    #makedepend ${INCS} ${SRCS}
    +#    makedepend ${INCS} ${SRCS}

    -tinyxml.o: tinyxml.h tinystr.h
    -tinyxmlparser.o: tinyxml.h tinystr.h
    -xmltest.o: tinyxml.h tinystr.h
    -tinyxmlerror.o: tinyxml.h tinystr.h
    +tinyxml.o: tinyxml.h tinystr.h ${OPTHDR}
    +tinyxmlparser.o: tinyxml.h tinystr.h ${OPTHDR}
    +xmltest.o: tinyxml.h tinystr.h ${OPTHDR}
    +tinyxmlerror.o: tinyxml.h tinystr.h ${OPTHDR}
    diff -ur tinyxml-2.5.2/tinyxml.h tinyxml-2.5.2.mod/tinyxml.h
    --- tinyxml-2.5.2/tinyxml.h    Tue Sep 19 22:04:36 2006
    +++ tinyxml-2.5.2.mod/tinyxml.h    Sun Feb 11 03:16:02 2007
    @@ -37,6 +37,7 @@
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    +#include "tinyopt.h"

    // Help out windows:
    #if defined( _DEBUG ) && !defined( DEBUG )
    diff -ur tinyxml-2.5.2/xmltest.cpp tinyxml-2.5.2.mod/xmltest.cpp
    --- tinyxml-2.5.2/xmltest.cpp    Tue Sep 19 22:04:36 2006
    +++ tinyxml-2.5.2.mod/xmltest.cpp    Sun Feb 11 03:17:20 2007
    @@ -2,15 +2,6 @@
        Test program for TinyXML.
    */

    -
    -#ifdef TIXML_USE_STL
    -    #include <iostream>
    -    #include <sstream>
    -    using namespace std;
    -#else
    -    #include <stdio.h>
    -#endif
    -
    #if defined( WIN32 ) && defined( TUNE )
         #include <crtdbg.h>
         _CrtMemState startMemState;
    @@ -18,6 +9,12 @@
    #endif

    #include "tinyxml.h"
    +
    +#ifdef TIXML_USE_STL
    +    using namespace std;
    +#else
    +    #include <stdio.h>
    +#endif

    static int gPass = 0;
    static int gFail = 0;

     
    • JCip

      JCip - 2007-02-11

      Bah, the HTML broke all the spaces. You can view the HTML source of this post and use the diff info from that instead, or feel free to email me at b5901@hotttmail.com (change ttt to single t).

       
    • JCip

      JCip - 2007-02-11

      *Sigh* liek zomg i r such a nub oh noes. After applying the above patch, make the following two modifications to the Makefile to prevent it from including xmltest.o in the compiled library :-/

      1) Replace the "Source files" section with:

      #****************************************************************************
      # Source files
      #****************************************************************************

      LIBSRCS := tinyxml.cpp tinyxmlparser.cpp tinyxmlerror.cpp tinystr.cpp
      SRCS := ${LIBSRCS} xmltest.cpp

      # Add on the sources for libraries
      SRCS := ${SRCS}

      LIBOBJS := $(addsuffix .o,$(basename ${LIBSRCS}))
      OBJS := $(addsuffix .o,$(basename ${SRCS}))

      2) Change the line "${LIBNAME}: ${OBJS}" to "{$LIBNAME}: ${LIBOBJS}".

       
    • JCip

      JCip - 2007-02-11

      :((( And change ${OBJS} -> ${LIBOBJS} in the {$AR} line as well! Forgot to mention that. That's it. I'm never using the internet -again-.

       
      • Ellers

        Ellers - 2007-02-13

        Thanks for your comments...

        but I'm not really sure what specific problems you are solving?

         
    • JCip

      JCip - 2007-02-13

      It's a patch for building tinyxml as a library.

      Type "make lib" and out pops libtinyxml.a. Copy headers and lib from the directories it creates into your include/lib path.

      I generally prefer code like this to be in library form; that way if a new version of TinyXML comes out, I don't have to make sure that I update multiple copies of the TinyXML source I have laying around; only the library.

       
    • smogzer

      smogzer - 2007-04-14

      Go to http://miarn.sf.net i made that a sometime ago.

       
      • Dr.Joyntz

        Dr.Joyntz - 2007-11-08

        ok has someone the hole file for me??? i lost the plot

         
  • lostbard

    lostbard - 2011-04-17

    And another patch, to allow the option for creating/making a static library under linux or now: TINYXML_CREATE_LIB := YES.

    --- Makefile.old        2010-03-23 01:16:36.000000000 -0400
    +++ Makefile    2011-04-16 19:08:39.000000000 -0400
    @@ -17,6 +17,11 @@
     # will not be used. YES will include the STL files.
     TINYXML_USE_STL := NO
    +# TINYXML_CREATE_LIB can be used to create a basic static library for tinyxml
    +# YES will create libtinyxml.a and static link to xmltrest
    +# NO will not create the library and link all as .o files
    +TINYXML_CREATE_LIB := NO
    +
     #****************************************************************************
     CC     := gcc
    @@ -28,7 +33,7 @@
     DEBUG_CFLAGS     := -Wall -Wno-format -g -DDEBUG
     RELEASE_CFLAGS   := -Wall -Wno-unknown-pragmas -Wno-format -O3
    -LIBS            :=
    +LIBS            := 
     DEBUG_CXXFLAGS   := ${DEBUG_CFLAGS}
     RELEASE_CXXFLAGS := ${RELEASE_CFLAGS}
    @@ -83,27 +88,45 @@
     OUTPUT := xmltest
    -all: ${OUTPUT}
    +ifeq (YES, ${TINYXML_CREATE_LIB})
    +  XMLLIB := libtinyxml.a
    +  LIBS := -L. -ltinyxml
    +else
    +  XMLLIB := 
    +endif
    +
    +all: ${XMLLIB} ${OUTPUT}
     #****************************************************************************
     # Source files
     #****************************************************************************
    -SRCS := tinyxml.cpp tinyxmlparser.cpp xmltest.cpp tinyxmlerror.cpp tinystr.cpp
    +ifeq (YES, ${TINYXML_CREATE_LIB})
    +  SRCS := xmltest.o
    +  LIB_SRCS := tinyxml.cpp tinyxmlparser.cpp tinyxmlerror.cpp tinystr.cpp
    +else
    +  SRCS := tinyxml.cpp tinyxmlparser.cpp tinyxmlerror.cpp tinystr.cpp xmltest.o
    +endif
     # Add on the sources for libraries
    -SRCS := ${SRCS}
    +SRCS := ${SRCS} 
     OBJS := $(addsuffix .o,$(basename ${SRCS}))
    +LIB_OBJS := $(addsuffix .o,$(basename ${LIB_SRCS}))
     #****************************************************************************
     # Output
     #****************************************************************************
    -${OUTPUT}: ${OBJS}
    +${OUTPUT}: ${OBJS} ${LIB_OBJS}
            ${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIBS} ${EXTRA_LIBS}
    +ifeq (YES, ${TINYXML_CREATE_LIB})
    +${XMLLIB}: ${LIB_OBJS}
    +       ${AR} ${XMLLIB} ${LIB_OBJS}
    +endif
    +
     #****************************************************************************
     # common rules
     #****************************************************************************
    @@ -119,7 +142,7 @@
            bash makedistlinux
     clean:
    -       -rm -f core ${OBJS} ${OUTPUT}
    +       -rm -f core ${OBJS} ${LIB_OBJS} ${OUTPUT} ${XMLLIB}
     depend:
            #makedepend ${INCS} ${SRCS}
    
     
  • Ma Anguo

    Ma Anguo - 2011-05-15

    The patches would be better posted in as a file attached to a new feature request in the tracker.
    Thanks nonetheless.

    Blessings

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks