I am a beginner to CMAKE. Below is a simple cmake file which works well in mingw environment windows. The problem is clearly with target_link_libraries()
function of CMAKE where I am linking libwsock32.a. In windows this works and I get the results.
However, as expected, in Linux, the /usr/bin/ld
will look for -lwsock32
which is NOT there on the Linux OS.
My Problem is: How do I instruct CMAKE to avoid linking wsock32 library in Linux OS???
Any help will be greatly appreciated.
My Simple CMake file:
PROJECT(biourl)
set (${PROJECT_NAME}_headers ./BioSocketAddress.h ./BioSocketBase.h ./BioSocketBuffer.h ./BioSocketCommon.h ./BioSocketListener.h ./BioSocketPrivate.h ./BioSocketStream.h ./BioUrl.h BioDatabase.h )
set (${PROJECT_NAME}_sources BioSocketAddress.C BioSocketBase.C BioSocketCommon.C BioSocketStream.C BioUrl.C BioDatabase.C )
add_library(${PROJECT_NAME} STATIC ${${PROJECT_NAME}_headers} ${${PROJECT_NAME}_sources} )
# linkers
#find_library(ws NAMES wsock32 PATHS ${PROJECT_SOURCE_DIR} NO_SYSTEM_ENVIRONMENT_PATH NO_DEFAULT_PATH)
target_link_libraries(${PROJECT_NAME} bioutils wsock32)
install (TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/archive )
You have some special words from CMAKE, take a look:
Use some preprocessor macro to check if it's in windows or linux. For example
include -l$(LIB) in you build command.
You can also specify some command line argument to differentiate both.
In General
You can detect and specify variables for several operating systems like that:
Detect Microsoft Windows
Or:
Detect Apple MacOS
Detect Unix and Linux
Your specific linker issue
To solve your issue with the Windows-specific
wsock32
library, just remove it from other systems, like that:Try that:
You can find other useful variables here.
Use
or
or
or similar
see CMake Useful Variables and CMake Checking Platform
Given this is such a common issue, geronto-posting:
CMake boolean logic docs