Instrucciones específicas del sistema operativo en CMAKE: ¿Cómo hacerlo?


Soy un principiante en CMAKE. A continuación se muestra un simple archivo cmake que funciona bien en ventanas de entorno mingw. El problema está claramente con la función target_link_libraries() de CMAKE donde estoy enlazando libwsock32.a. En Windows esto funciona y obtengo los resultados.

Sin embargo, como era de esperar, en Linux, el /usr/bin/ld buscará -lwsock32 que NO está allí en el sistema operativo Linux.

Mi problema es: ¿Cómo le indico a CMAKE que evite vincular la biblioteca wsock32 en el sistema operativo Linux???

Cualquier ayuda será muy apreciada.

Mi Simple archivo CMake:

 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 )
Author: Guy Avraham, 2012-02-06

6 answers

Use

if (WIN32)
    #do something
endif (WIN32)

O

if (UNIX)
    #do something
endif (UNIX)

O

if (MSVC)
    #do something
endif (MSVC)

O similar

Ver CMake Variables útiles and CMake Checking Platform

 58
Author: relaxxx,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-29 22:23:33

Dado que este es un problema tan común, geronto-posting:

    if(UNIX AND NOT APPLE)
        set(LINUX TRUE)
    endif()

    # if(NOT LINUX) should work, too, if you need that
    if(LINUX) 
        message(STATUS ">>> Linux")
        # linux stuff here
    else()
        message(STATUS ">>> Not Linux")
        # stuff that should happen not on Linux 
    endif()

CMake boolean logic docs

 44
Author: mlvljr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-27 20:42:53

En General

Puede detectar y especificar variables para varios sistemas operativos como:

Detectar Microsoft Windows

if(WIN32)
    # for Windows operating system in general
endif()

O:

if(MSVC OR MSYS OR MINGW)
    # for detecting Windows compilers
endif()

Detectar Apple macOS

if(APPLE)
    # for MacOS X or iOS, watchOS, tvOS (since 3.10.3)
endif()

Detectar Unix y Linux

if(UNIX AND NOT APPLE)
    # for Linux, BSD, Solaris, Minix
endif()

Su problema específico del enlazador

Para resolver su problema con la biblioteca wsock32 específica de Windows, simplemente elimínela de otros sistemas, así:

if(WIN32)
    target_link_libraries(${PROJECT_NAME} bioutils wsock32)
else
    target_link_libraries(${PROJECT_NAME} bioutils)
endif()
 30
Author: Afri,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-25 13:20:03

Tienes algunas palabras especiales de CMAKE, echa un vistazo:

if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
    // do something for Linux
else
    // do something for other OS
 7
Author: Bruno Soares,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-06 12:58:15

Intenta eso:

if(WIN32)
    set(ADDITIONAL_LIBRARIES wsock32)
else()
    set(ADDITIONAL_LIBRARIES "")
endif()

target_link_libraries(${PROJECT_NAME} bioutils ${ADDITIONAL_LIBRARIES})

Puede encontrar otras variables útiles aquí.

 5
Author: tibur,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-06 12:55:15

Utilice alguna macro de preprocesador para comprobar si está en windows o linux. Por ejemplo

#ifdef WIN32
LIB= 
#elif __GNUC__
LIB=wsock32
#endif

Include-l ((LIB) en el comando de compilación.

También puede especificar algún argumento de línea de comandos para diferenciar ambos.

 -3
Author: Barun Parichha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-14 22:23:21