title: CMake adding libraries for Windows/Linux
body: <p>Visual Studio C++ 2008 / GCC 4.4.2</p>
  
  <p>I have written a program to run on Linux and now I have to port my code to run on Windows. I have decided to use <a href="http://en.wikipedia.org/wiki/CMake" rel="noreferrer">CMake</a> as I want to keep the same build system for both platforms.</p>
  
  <p>However, I need to link with some libraries for both platforms. In my CMakeLists.txt I have the following:</p>
  
  <pre><code># Compile with gcc c89 standard
  IF(CMAKE_COMPILER_IS_GNUCXX)
      MESSAGE(STATUS "GCC detected - Adding compiler flags")
      SET(CMAKE_C_FLAGS "-pthread -ggdb -Wextra -Wall -pedantic -std=c89")
  ENDIF(CMAKE_COMPILER_IS_GNUCXX)
  
  IF(WIN32)
  SET(CMAKE_C_FLAGS "ws2_32.lib")
  ENDIF(WIN32)
  </code></pre>
  
  <p>However, when I compile on Visual Studio I get the following error:</p>
  
  <pre><code>fatal error C1083: Cannot open source file: 'ws2_32.lib': No such file or directory
  </code></pre>
  
  <p>What can I do to resolve this problem?</p>
  
  <p>========= Edit
  In the top level directory</p>
  
  <pre><code># Project Client Server
  CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
  
  # Name of project
  PROJECT(CLIENT_SERVER)
  
  # Compile with gcc c89 standard
  IF(CMAKE_COMPILER_IS_GNUCXX)
      MESSAGE(STATUS "GCC detected - Adding compiler flags")
      SET(CMAKE_C_FLAGS "-pthread -ggdb -Wextra -Wall -pedantic -std=c89")
  ENDIF(CMAKE_COMPILER_IS_GNUCXX)
  
  IF(WIN32)
      SET(CMAKE_C_FLAGS "ws2_32")
  ENDIF(WIN32)
  
  # Includes
  INCLUDE_DIRECTORIES(${CLIENT_SERVER_SOURCE_DIR}/cltsvr_ults)
  INCLUDE_DIRECTORIES(${CLIENT_SERVER_SOURCE_DIR}/server)
  INCLUDE_DIRECTORIES(${CLIENT_SERVER_SOURCE_DIR}/client)
  
  # libraries
  LINK_DIRECTORIES($CLIENT_SERVER/cltsvr_ults)
  
  # Add subdirectories
  ADD_SUBDIRECTORY(client)
  ADD_SUBDIRECTORY(server)
  ADD_SUBDIRECTORY(cltsvr_ults)
  ADD_SUBDIRECTORY(test_client)
  ADD_SUBDIRECTORY(test_server)
  </code></pre>
  
  <p>In the subdirectory of client I have this CMakeLists.txt</p>
  
  <pre><code># libray called client from client.c
  ADD_LIBRARY(client client)
  </code></pre>
  
  <p>And in the subdirectory of test_clt where I create and link my executable.</p>
  
  <pre><code># Test client add executable
  INCLUDE_DIRECTORIES($CLIENT_SERVER_SOURCE_DIR/client)
  INCLUDE_DIRECTORIES($CLIENT_SERVER_SOURCE_DIR/cltsvr_ults)
  
  # Link the library
  LINK_DIRECTORIES($CLIENT_SERVER/client)
  
  # Add the executable 
  ADD_EXECUTABLE(clt test_clt)
  
  # Link the executable to the client library
  IF(WIN32)
      TARGET_LINK_LIBRARIES(clt client ws2_32)
  ENDIF(WIN32)
  </code></pre>