[CMake] Shared library for a executable

Tamás Kenéz tamas.kenez at gmail.com
Mon Jan 25 06:02:44 EST 2016


Gonzalo, Ray,

I think your approaches are not in accordance with some CMake best
practices (or at least what I believe they are).

Please take a look at Ray's example after a heavy refactor:
https://github.com/tamaskenez/cmake-2016-jan-21-shared-lib-exe

These are the things I changed:

- In your test executables don't compile the source files of the
library-under-test into your test. Instead, link to the library. Reason (1)
ODR, (2) this way you'll test the linking operation, too.
- Don't reach out from a subdirectory ('B') to include a sibling ('A'). It
creates harmful coupling between the subdirectories. All information that
'B' needs to know about 'A' should be concentrated in the properties of the
target 'A'. So you need to use commands like target_include_directories().
- Unnecessary verbosity makes it harder to read and debug the CMakeLists.

With my changes:

- you can build and test 'A' standalone
- you can build and test 'A', 'B' and 'X' in one project using the
top-level CMakeLists.txt
- you can't build 'B' standalone or can't build 'X' without 'A' and 'B'
being there. If you need that feature, we need to add an install step to
'A' and 'B' and use find_package() in 'B' and 'X' on demand

Tamas



On Mon, Jan 25, 2016 at 10:22 AM, Gonzalo <ggarra13 at gmail.com> wrote:

>
>
> El 22/01/16 a las 10:50, Raymond Wan escribió:
>
>> Hi Gonzalo,
>>
>> I recently tried doing this and wrote it up as a record for myself.
>> Of course, I don't know if it's the right way, but I'm doing it this
>> way until I figure out a better way...
>>
>> See if this helps and let me know if you figure out something better:
>>
>> http://www.rwanwork.info/sysdocs/cmake/overview/
>>
>> Ray
>>
> Thanks, Ray.  Your approach is exactly the same as mine.  We already found
> out that the problem I was having with v3.4.2 is a bug in cmake, which has
> been fixed in the git repository.
>
> --
> Gonzalo Garramuño
> ggarra13 at gmail.com
>
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160125/d5e02c12/attachment-0001.html>


More information about the CMake mailing list