[CMake] Settings different compile options for diff. files in same folder

David.Karr at L-3COM.COM David.Karr at L-3COM.COM
Thu May 14 10:28:43 EDT 2009


ankit jain wrote:

> I have a folder in which there are 5 files
> folder
>   a1.c
>   a2.c
>   a3.c
>   a4.c
>   a5.c
>
> Out of these from 4th and 5th iam making a library if iam working on
windows
> else with the rest three iam building one library for the folder.
> Even on windows also iam making this library but making one more
library for
> the files 4th and 5th.,.

So if I understand correctly, all systems must build a library from
a1.c, a2.c, and a3.c.  Windows must build an additional library from
a4.c and a5.c, but other systems do nothing with those files.

Is there any possible way that you can get this project reorganized so
that you have no more than one library built from any one
folder/directory?  That seems to be the standard practice in my
experience.  Yes, you can put multiple Visual Studio project files in
one folder (and get similar effects from makefiles), but why?

Suppose you could organize the files like this:

folder
  +-- library1
  |      +-- a1.c
  |      +-- a2.c
  |      +-- a3.c
  +-- library2
         +-- a4.c
         +-- a5.c

Then in library1 you would have CMakeLists.txt that sets the appropriate
options for a1.c, a2.c, and a3.c, and in library2 you have
CMakeLists.txt that sets appropriate options for a4.c and a5.c.

In the top folder you would have a CMakeLists.txt that adds the
subdirectory library1; this CMakeLists.txt would also add the
subdirectory library2 if it built on Windows, otherwise it would not add
library2.

I have had very good success telling CMake that I want certain
subdirectories built only when a certain condition is true.  Also, it is
very easy to understand what is actually getting built, since I do
out-of-source builds (the compiled files are all under a hierarchy of
directories completely separate from the directories that hold the
source files); the directories where things actually get built are the
ones found in the build tree.  In your case, all you have to remember is
that library2 is "Windows only"; you don't have to remember the names of
all the individual "Windows-only" files.

David A. Karr



More information about the CMake mailing list