22

I found only this strings

    find_library(WSOCK32_LIBRARY wsock32)
    find_library(WS2_32_LIBRARY ws2_32)

(i'm begginer in cmake) how to link winsock2 (winsock?) in cmake?

Fedcomp
  • 2,175
  • 5
  • 21
  • 23

1 Answers1

46

Since these are both part of the Windows SDK, you shouldn't need to do a search for them. Assuming you have the SDK installed, you can just do something like:

add_executable(MyExe main.cpp)
if(WIN32)
  target_link_libraries(MyExe wsock32 ws2_32)
endif()
Fraser
  • 74,704
  • 20
  • 238
  • 215
  • 2
    indeed, you "shouldn't" need to do a search for them. However, in the off chance that you do... can CMAKE find them? For instance if you're using MinGW through a unusual compiler driver? – cheshirekow Jun 24 '13 at 13:50
  • 2
    Yes, you can use [`find_library`](http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:find_library) for each library to get the full path to it, then just include the result in your `target_link_libraries` call in place of the bare `wsock32` and/or `ws2_32`. – Fraser Jun 24 '13 at 18:58