title: CMake Add and Remove a Macro definition to compile Shared Library/Executable
body: <p>I have a c++ code i need to compile in two ways, a Shared Library and an Executable, In order to do so some of my functions need to be undefined when compiling as a shared library. So i decide to used <code>#ifdef MACRO</code> and define <code>MACRO</code> in my CMakeLists.txt. </p>
  
  <p>Here is my case :</p>
  
  <p>File <code>function.cpp</code> :</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  #ifdef _SHARED_LIBRARY_
  
  void printSharedLibrary(void)
  {
      std::cout &lt;&lt; "Shared Library" &lt;&lt; std::endl;
  }
  
  #else
  
  void printExecutable(void)
  {
      std::cout &lt;&lt; "Executable" &lt;&lt; std::endl;
  }
  
  #endif
  </code></pre>
  
  <p>File <code>main.cpp</code> :</p>
  
  <pre><code>#ifdef _SHARED_LIBRARY_
      void printSharedLibrary(void);
  #else
      void printExecutable(void);
  #endif
  
  int main (void)
  {
      #ifdef _SHARED_LIBRARY_
          printSharedLibrary();
      #else
          printExecutable();
      #endif
  }
  </code></pre>
  
  <p>File <code>CMakeLists.txt</code> :</p>
  
  <pre><code>project(ProjectTest)
  
  message("_SHARED_LIBRARY_ ADDED BELOW")
  add_definitions(-D_SHARED_LIBRARY_)
  
  add_library(TestLibrary SHARED functions.cpp)
  add_executable(DefinedExecutable main.cpp) // Only here to be able to test the library
  target_link_libraries(DefinedExecutable TestLibrary)
  
  message("_SHARED_LIBRARY_ REMOVED BELOW")
  remove_definitions(-D_SHARED_LIBRARY_)
  
  add_executable(UndefinedExecutable main.cpp functions.cpp)
  </code></pre>
  
  <p>Output :</p>
  
  <pre><code>$&gt; ./DefinedExecutable
  Executable
  
  $&gt; ./UndefinedExecutable
  Executable
  </code></pre>
  
  <p>Expected Output :</p>
  
  <pre><code>$&gt; ./build/DefinedExecutable
  Shared Library
  
  $&gt; ./build/UndefinedExecutable
  Executable
  </code></pre>
  
  <p>in order to build it i use : <code>rm -rf build/ ; mkdir build ; cd build ; cmake .. ; make ; cd ..</code></p>
  
  <p>So my question is Is there a way to define <code>_SHARED_LIBRARY_</code> for the build of <code>DefinedExecutable</code> and then undefine it for the build of <code>UndefinedExecutable</code>.
  Thanks for the help</p>