How cmake links specified libraries

Viewed 30

I want to implement a simple server on a Windows system. I get a problem that I can't successfully compile it in CMake, because I don't know how to link ws2_32.lib to it. With gcc/g++, my command

g++ -g -o myhttp ../src/main.cpp ../src/start_up.cpp -I ../inc -lws2_32

runs well, but in CMake I don't know how to operate. The following is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.2)

project(MYHTTP_BASE_C++)
set(CMAKE_PROGRAM_PATH ${CMAKE_SOURCE_DIR}/bin)
include_directories(${CMAKE_SOURCE_DIR}/inc)

find_package("ws2_32.lib")

target_link_libraries( SRC)
aux_source_directory(${PROJECT_SOURCE_DIR}/src SRC)
add_executable(myhttp ${SRC})

The error is

CMake Error at CMakeLists.txt:7 (target_link_libraries):
  Cannot specify link libraries for target "ws2_32.lib" which is not built by
  this project
1 Answers

Normally, the following CMakeLists.txt shall work for windows. You have to specify the libs in the target_link_libraries.

project(MYHTTP_BASE_C++)
set(CMAKE_PROGRAM_PATH ${CMAKE_SOURCE_DIR}/bin)
include_directories(${CMAKE_SOURCE_DIR}/inc)
...
add_executable(myhttp ${SRC})
target_link_libraries(myhttp ws2_32)

The ws2_32.lib is the import library for the corresponding dll.

Related