-1

I am able to successfully compile and execute a sample C++ program which includes boost library through windows command prompt.

I want to use make and hence I want to use CMAKE(3.15) with MinGW g++.

I used cmake-gui, CLION, and cmake on cmd prompt

Very unfortunately i am not able to find the compiler flag "-lws2_32" included through CMakeLists.txt

I found different solutions on StackOverflow but none helped me.

Below is the CMakeLists.txt

    cmake_minimum_required (VERSION 3.14.0)
    project (BoostCMakeTutorial)
    set(GCC_BOOST_COMPILER_FLAG "-lws2_32")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_BOOST_COMPILER_FLAG}")
    add_executable(BoostCMakeTutorial boostTcpClient.cpp)

I am sure on "-lws2_32" flag is missing from CMAKE because i see the same error on cmd prompt when this not included

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Manoj Shenoy
  • 333
  • 1
  • 3
  • 12
  • Did you verify the flag is added in CMake by printing the variable `CMAKE_CXX_FLAGS`? Did you try running `make` with `VERBOSE=1` to see that the flag is indeed missing? Can you post the exact error you are seeing? – Kevin Jun 18 '19 at 17:35
  • @squareskittles : I tried with `VERBOSE=1`, but no use. In `cmake-gui` the field `CMAKE_CXX_FLAGS` is empty. I manually added it and then generated. The `-lws2_32` is missing. – Manoj Shenoy Jun 18 '19 at 17:43
  • @squareskittles : I also tried to use `target_link_libraries` following [link](https://stackoverflow.com/questions/43136418/how-to-add-l-ell-compiler-flag-in-cmake) but opening the CMakeLists.txt in cmake-gui pops the error because of it. – Manoj Shenoy Jun 18 '19 at 17:46
  • "but no use" - What does this mean? The `VERBOSE` command didn't work? Or it showed that the `-lws2_32` flag was missing? – Kevin Jun 18 '19 at 17:46
  • 1
    Please edit your post to show the full command you tried when using `target_link_libraries`, and the error you got. – Kevin Jun 18 '19 at 17:48
  • @squareskittles : `make VERBOSE=1` shows `g++.exe -lws2_32 -lws2_32`. That is `-lws2_32` is included two times. Am i missing somewhere? – Manoj Shenoy Jun 18 '19 at 17:50
  • `lws2_32` is a linker flag. Although you set it with a different `CMake` variable its best to use `target_link_libraries` – drescherjm Jun 18 '19 at 17:51
  • @squareskittles : I added `-lws2_32` in `CMakeLists.txt` and in `cmake-gui`, so it was appending two times. Now i appended at single place and with `VERBOSE=1` i verified. But though the error is same – Manoj Shenoy Jun 18 '19 at 17:55
  • @drescherjm : Yes exactly but using `target_link_libraries(lw2_32)` gives error while opening `cmake-gui` – Manoj Shenoy Jun 18 '19 at 17:56
  • ***gives error while opening cmake-gui*** What error? – drescherjm Jun 18 '19 at 17:57

1 Answers1

0

To add the -lws2_32 linker flag in modern CMake, the best approach is to use target_link_libraries. I've updated your example below:

cmake_minimum_required (VERSION 3.14.0)
project (BoostCMakeTutorial)
add_executable(BoostCMakeTutorial boostTcpClient.cpp)
target_link_libraries(BoostCMakeTutorial ws2_32)
Kevin
  • 16,549
  • 8
  • 60
  • 74
  • Can you please change `target_link_libraries(BoostCMakeTutorial w2_32)` to `target_link_libraries(BoostCMakeTutorial ws2_32)`, this may help next search people – Manoj Shenoy Jun 18 '19 at 18:08
  • Missing that typo, fixed! – Kevin Jun 18 '19 at 18:10
  • if i interchange `add_executable(BoostCMakeTutorial boostTcpClient.cpp) target_link_libraries(BoostCMakeTutorial w2_32)` these lines it gives error, that where i was missing, Thank you very much squareskittles – Manoj Shenoy Jun 18 '19 at 18:10
  • 1
    Yes, you must first define your executable `BoostCMakeTutorial` with the `add_executable` command before referencing it in the [`target_link_libraries`](https://cmake.org/cmake/help/latest/command/target_link_libraries.html) command. – Kevin Jun 18 '19 at 18:14