[CMake] help with learning add_library

luxInteg lux-integ at btconnect.com
Sat Nov 6 19:39:06 EDT 2010


On Saturday 06 November 2010 21:55:21 luxInteg wrote:
> On Saturday 06 November 2010 17:30:10 Michael Hertling wrote:
> > On 11/06/2010 05:26 PM, luxInteg wrote:
> > > Greetings,
> > > 
> > > I am learning cmake.
> > > 
> > > I have a question on  using add_library.  in my  project(learnCMAKE).
> > > The latter has:-
> > > 
> > > ---a) 2  source files   file1.c  file2.c
> > > ---b) the envar {CFLAGS}  set
> > > ---c)  need to generate  libLearnCMAKE.so and libLearnCMAKE.a
> > 
> > ADD_LIBRARY(LearnCMAKE-shared SHARED file1.c file2.c)
> > ADD_LIBRARY(LearnCMAKE-static STATIC file1.c file2.c)
> > SET_TARGET_PROPERTIES(LearnCMAKE-shared LearnCMAKE-static
> > 
> >     PROPERTIES OUTPUT_NAME LearnCMAKE)
> > > 
> > > ---d) need  to add preprocessor agruments -DXXX to CFLAGS  in the
> > > compilation of  the source files  before liberies are archived  like
> > > so:-
> > > 
> > > gcc ${CFLAGS} -DDINT /path/to/file1.c
> > > gcc ${CFLAGS} -DDINT /path/to/file2.c
> > > 
> > > gcc ${CFLAGS} -DDLONG /path/to/file1.c
> > > gcc ${CFLAGS} -DDLONG /path/to/file2.c
> > 
> > SET_TARGET_PROPERTIES(LearnCMAKE-shared LearnCMAKE-static
> > 
> >     PROPERTIES COMPILE_DEFINITIONS DINT)
> > 
> > or
> > 
> > SET_SOURCE_FILES_PROPERTIES(/path/to/file1.c /path/to/file2.c
> > 
> >     PROPERTIES COMPILE_DEFINITIONS DINT)
> > 
> > or
> > 
> > SET_DIRECTORY_PROPERTIES(
> > 
> >     PROPERTIES COMPILE_DEFINITIONS DINT)
> > 
> > or
> > 
> > ADD_DEFINITIONS(-DDINT)
> > 
> > or
> > 
> > SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDINT")
> 
> thanks  a lot.   I had the most success with  this:-
>  SET_SOURCE_FILES_PROPERTIES(/path/to/file1.c /path/to/file2.c
>     PROPERTIES COMPILE_DEFINITIONS DINT)
> 
> 
> HOWEVER  I need to pass   the files through the compiler twice once with
> _DINT and other with -DDOUBLE before  the  object files are archived..
> 
> I tried this:
> 
> SET_SOURCE_FILES_PROPERTIES(/path/to/file1.c /path/to/file2.c
>     PROPERTIES COMPILE_DEFINITIONS DINT)
> SET_SOURCE_FILES_PROPERTIES(/path/to/file1.c /path/to/file2.c
>     PROPERTIES COMPILE_DEFINITIONS DDOUBLE)
> 
> but only the last one was  done.    I am wondering if the DINT and DDOUBLE
> could be incorporated in a foreach loop?
> 
I tried the following:-

ADD_LIBRARY(LearnCMAKE-shared SHARED   file1.c file2.c )
ADD_LIBRARY(LearnCMAKE-static STATIC  file1.c file2.c )
SET(var "")
FOREACH(var DINT DLONG)
        SET_SOURCE_FILES_PROPERTIES( file1.c file2.c
    PROPERTIES COMPILE_DEFINITIONS ${var})
ENDFOREACH(var)
SET_TARGET_PROPERTIES(LearnCMAKE-shared LearnCMAKE-static
    PROPERTIES OUTPUT_NAME LearnCMAKE)

but my compile logs only show lines with  "-DDLONG -O3 "
and one with  "-DDINT -O3" 

and the generated libs are about half the size they should be.

advice would be appreciated



More information about the CMake mailing list