[CMake] The right way to link a library from multiple directories

Andreas Pakulat apaku at gmx.de
Thu Jul 24 12:21:11 EDT 2008


On 24.07.08 17:42:40, Sebastian Krause wrote:
> I have a library where the files are spread over several
> directories, but in the end have to be linked all together into a
> single library:
> 
> ,----
> | In src:
> |    a.cpp b.cpp c.cpp
> | In src/foo:
> |    k.cpp l.cpp m.cpp
> | in src/bar:
> |    x.cpp y.cpp z.cpp
> `----
> 
> The easiest way to apporach this would be to define everything in a
> single CMakeLists.txt in src that does everything at once:
> 
> ,----
> | (set src a.cpp b.cpp c.cpp)
> | (set foo foo/k.cpp foo/l.cpp foo/m.cpp)
> | (set bar bar/x.cpp bar/y.cpp bar/z.cpp)
> | add_library(mylib SHARED ${src} ${foo} ${bar})
> `----
> 
> However, I don't like this approach because it doesn't fullfill the
> following conditions I would like to have:
> 
> - There should be a CMakeLists.txt in each subdirectory where I only
>   define the files which are in this directory. Additionally I would
>   like to be able to set different compile flags for each
>   subdirectory.
> - The directory structure should stay the same if displayed e.g. in
>   Visual Studio. In the first approach, every file would be
>   displayed in the same project no matter of its location.
> 
> So my first idea was to build the foo and bar subdirectories as
> static libraries and link them together later:

Thats not going to work on all platforms. There are various platforms
which don't allow static libs to be linked into shared libs.

But it should be possible to gather the sources in the subdirs into a
variable that the parent cmake-file knows and pre-sets to empty. And
then also set the compile flags by using set_source_files_properties().

Andreas

-- 
A long-forgotten loved one will appear soon.

Buy the negatives at any price.


More information about the CMake mailing list