# How to set option to a library but only when used by examples

**URL:** https://discourse.cmake.org/t/how-to-set-option-to-a-library-but-only-when-used-by-examples/481
**Category:** Code
**Created:** [January 11, 2020, 2:12pm UTC](https://discourse.cmake.org/t/how-to-set-option-to-a-library-but-only-when-used-by-examples/481 "2020-01-11T14:12:39Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![inetic](https://discourse.cmake.org/user_avatar/discourse.cmake.org/inetic/32/297_2.png) [@inetic](https://discourse.cmake.org/u/inetic)
#### Post date: [January 11, 2020, 2:12pm UTC](https://discourse.cmake.org/t/how-to-set-option-to-a-library-but-only-when-used-by-examples/481/1 "2020-01-11T14:12:39Z")

</div>

I have a CMake project that is a library that also adds an example. Something like this

```auto
/example/
 -> myexample.cpp
 -> CMakeLists.txt
/include/
 -> mylib.h
/src/
 -> mylib.cpp
/CMakeLists.txt

```

My `/CMakeLists.txt` looks like this:

```auto
cmake_minimum_required (VERSION 3.16)
set(BOOST_VERSION 1.72)

option(ASIO_SEPARATE_COMPILATION "Compile Asio separately" ON)

project(mylib)

add_library(mylib STATIC "src/mylib.cpp")
target_include_directories(mylib PUBLIC "include")

if(ASIO_SEPARATE_COMPILATION)
  target_compile_definitions(mylib PUBLIC -DBOOST_ASIO_SEPARATE_COMPILATION)
endif()

add_subdirectory(example)

```

The `/example/CMakeLists.txt` is as simple as it can get:

```auto
add_executable(myexample "myexample.cpp")
target_link_libraries(myexample mylib)

```

Note the `ASIO_SEPARATE_COMPILATION` option: it is there to optionally add the `BOOST_ASIO_SEPARATE_COMPILATION` definition. The details of what that does aren’t important in context of this question, but more can be found [here](https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/using.html) for anyone interested.

Now, for whoever is using `mylib`, it is a good default to set `ASIO_SEPARATE_COMPILATION` option to ON. But for very simple executables such as `myexample.cpp`, it can be set to OFF for convenience. My problem is that I don’t know how to set that option only for `myexample` while leaving it on OFF by default for anyone else using `mylib`.

In other words, when `mylib` is used by `myexample`, I want `mylib` to be compiled with `ASIO_SEPARATE_COMPILATION` `OFF`. And when `mylib` is used by anyone else, I want that option to be `ON` by default, but configurable.

As a side note: it feels backwards that `myexample` is “added” from /CMakeLists.txt defining mainly the `mylib` library (though by looking at [other projects](https://github.com/boostorg/beast/blob/46ac848fa46ce5b81f2bb5b221774d15c0cdd128/CMakeLists.txt#L243-L247) this seems to be the standard approach). Ideally, (IMO) `myexample` should also showcase how an application “adds” `mylibrary` (not vice versa). I think doing it this other way would also solve my problem quite nicely, though I would prefer not to change the directory structure if possible.

---

<div class="post-metadata">

### Author: ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)
#### Post date: [January 13, 2020, 10:27pm UTC](https://discourse.cmake.org/t/how-to-set-option-to-a-library-but-only-when-used-by-examples/481/2 "2020-01-13T22:27:23Z")

</div>

Did you read that topic: [Is target specific stuff transitive to dependencies?](https://discourse.cmake.org/t/is-target-specific-stuff-transitive-to-dependencies/336/8)  
AFAIU you seem to pursue a similar functional goal: _compiling a library depending on its usage_.

---

<div class="post-metadata">

### Author: ![inetic](https://discourse.cmake.org/user_avatar/discourse.cmake.org/inetic/32/297_2.png) [@inetic](https://discourse.cmake.org/u/inetic)
#### Post date: [January 15, 2020, 9:35am UTC](https://discourse.cmake.org/t/how-to-set-option-to-a-library-but-only-when-used-by-examples/481/3 "2020-01-15T09:35:53Z")

</div>

Thanks, I think that’s a step in the right direction. Unfortunately I’m having problems using your solution for my case where I have a more elaborate project structure. [Here is a minimal example I came up with](https://www.dropbox.com/s/u34ogybktej75nx/mytest.tar.bz?dl=0) (I’m a new cmake forum user, so I can’t upload the file here directly).

In the example you’ll find three targets: mylib, myexample and myapp. Both myexample and myapp use mylib. The run.sh script compiles myexample twice, once from mylib and once from myapp.

I tried two different cases:

#1

- Set target\_compile\_definitions(myexample PRIVATE SEPARATE\_COMPILATION=0) in mylib/myexample/CMakeLists.txt
- Set target\_compile\_definitions(myapp PRIVATE SEPARATE\_COMPILATION=1) in myapp/CMakeLists.txt

This results in everything being compiled without SEPARATE\_COMPILATION even being defined when mylib.cpp is being compiled. And by everything I mean myexample when compiled from mylib and both myexample and myapp when compiled from myapp.

#2 (this is the one more akin to what I’m looking for as I’d like SEPARATE\_COMPILATION to be 1 by default)

- Set target\_compile\_definitions(mylib PRIVATE SEPARATE\_COMPILATION=1) in mylib/CMakeLists.txt
- Set target\_compile\_definitions(myexample PRIVATE SEPARATE\_COMPILATION=0) in mylib/myexample/CMakeLists.txt

This results in everything being compiled with SEPARATE\_COMPILATION=1.

This is getting hairy, so I’ll just reiterate that the goal is to compile

- myexample with SEPARATE\_COMPILATION=0 for both cases: where its compiled from mylib as well as from myapp
- myapp with SEPARATE\_COMPILATION=1
