Maintainers
Pricing
Enterprise
More
More
Documentation
Try Live
Rankings
Install
Blog
Docs7
Ask
New
Add Docs
ZLToolKit
https://github.com/zlmediakit/zltoolkit
Admin
一个基于C++11的轻量级网络框架,基于线程池技术可以实现大并发网络IO
Tokens:
2,363
Snippets:
17
Trust Score:
6.6
License:
MIT
Update:
1 year ago
Context
Chat
Benchmark
Latest
Show doc for...
Code
Info
Show Results
Tokens:
Raw
Copy
Link
Tokens:
Raw
Copy
Link
### Compile and Install Static Library in CMake Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This CMake snippet compiles a static library named `${PROJECT_NAME}` from the collected source files, includes the project's source directory for headers, and sets its output name. It then installs the static library to the specified library installation path. ```CMake add_library(${PROJECT_NAME}_static STATIC ${SRC_LIST}) target_include_directories(${PROJECT_NAME}_static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src) set_target_properties(${PROJECT_NAME}_static PROPERTIES OUTPUT_NAME "${PROJECT_NAME}") install(TARGETS ${PROJECT_NAME}_static ARCHIVE DESTINATION ${INSTALL_PATH_LIB}) ``` -------------------------------- ### Traverse Source Directories and Install Header Files Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This CMake loop iterates through defined subdirectories, collects source files using `aux_source_directory`, and installs all `.h` header files from these directories to the specified include installation path. ```CMake foreach (SUB_DIR ${SUB_DIR_LIST}) aux_source_directory(src/${SUB_DIR} SRC_LIST) install(DIRECTORY src/${SUB_DIR} DESTINATION ${INSTALL_PATH_INCLUDE} FILES_MATCHING PATTERN "*.h") endforeach () ``` -------------------------------- ### Define CMake Installation Directories Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This snippet sets the installation paths for libraries and include files. On Windows, paths are relative to the user's home directory; otherwise, they are set to standard `lib` and `include` directories. ```CMake if (WIN32) set(INSTALL_PATH_LIB $ENV{HOME}/${PROJECT_NAME}/lib) set(INSTALL_PATH_INCLUDE $ENV{HOME}/${PROJECT_NAME}/include) else () set(INSTALL_PATH_LIB lib) set(INSTALL_PATH_INCLUDE include) endif () ``` -------------------------------- ### Compile and Install Shared Library in CMake Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This CMake snippet compiles a shared library named `${PROJECT_NAME}` from the collected source files, links it against the defined libraries, and sets its output name. It then installs the shared library to the specified library installation path, but only for non-iOS, non-Android, and non-Windows platforms. ```CMake if (NOT IOS AND NOT ANDROID AND NOT WIN32) add_library(${PROJECT_NAME}_shared SHARED ${SRC_LIST}) target_include_directories(${PROJECT_NAME}_shared PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src) target_link_libraries(${PROJECT_NAME}_shared ${LINK_LIB_LIST}) set_target_properties(${PROJECT_NAME}_shared PROPERTIES OUTPUT_NAME "${PROJECT_NAME}") install(TARGETS ${PROJECT_NAME}_shared ARCHIVE DESTINATION ${INSTALL_PATH_LIB} LIBRARY DESTINATION ${INSTALL_PATH_LIB}) endif () ``` -------------------------------- ### CMake: Configure Test Executables and Link Libraries Source: https://github.com/zlmediakit/zltoolkit/blob/master/tests/CMakeLists.txt This CMake script first copies an SSL certificate (`ssl.p12`) to the executable output path. It then iterates through all source files in the current directory, creates an executable for each, and links them against either static or shared project libraries, along with additional platform-specific libraries like `pthread` for non-Android/iOS/Windows systems. ```CMake execute_process(COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/ssl.p12 ${EXECUTABLE_OUTPUT_PATH}/) aux_source_directory(. TEST_SRC_LIST) foreach(TEST_SRC ${TEST_SRC_LIST}) STRING(REGEX REPLACE "^\./|\.c[a-zA-Z0-9_]*$" "" TEST_EXE_NAME ${TEST_SRC}) message(STATUS "添加测试程序:${TEST_EXE_NAME}") add_executable(${TEST_EXE_NAME} ${TEST_SRC}) if(ANDROID OR IOS OR WIN32) target_link_libraries(${TEST_EXE_NAME} ${PROJECT_NAME}_static ${LINK_LIB_LIST}) else() target_link_libraries(${TEST_EXE_NAME} ${PROJECT_NAME}_shared ${LINK_LIB_LIST} pthread) endif() endforeach(TEST_SRC ${TEST_SRC_LIST}) ``` -------------------------------- ### Configure ZLToolKit CMake Project with Socket Feature Checks Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This CMake script initializes the ZLToolKit project, sets the minimum required CMake version, and includes modules for checking struct members and symbol existence. It then performs checks for `mmsghdr` struct, `sendmmsg`, and `recvmmsg` API availability, defining preprocessor macros (`HAVE_MMSG_HDR`, `HAVE_SENDMMSG_API`, `HAVE_RECVMMSG_API`) if these features are found. It also appends `_GNU_SOURCE` to required definitions. ```CMake project(ZLToolKit) cmake_minimum_required(VERSION 3.1.3) include(CheckStructHasMember) include(CheckSymbolExists) list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE) check_struct_has_member("struct mmsghdr" msg_hdr sys/socket.h HAVE_MMSG_HDR) check_symbol_exists(sendmmsg sys/socket.h HAVE_SENDMMSG_API) check_symbol_exists(recvmmsg sys/socket.h HAVE_RECVMMSG_API) if (HAVE_MMSG_HDR) add_definitions(-DHAVE_MMSG_HDR) endif () if (HAVE_SENDMMSG_API) add_definitions(-DHAVE_SENDMMSG_API) endif () if (HAVE_RECVMMSG_API) add_definitions(-DHAVE_RECVMMSG_API) endif () ``` -------------------------------- ### Find and Link MySQL Library in CMake Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This CMake snippet attempts to find the MySQL client library. If found and `ENABLE_MYSQL` is true, it includes MySQL's header directories, defines `ENABLE_MYSQL`, and appends MySQL libraries to the link list. ```CMake find_package(MYSQL QUIET) if (MYSQL_FOUND AND ENABLE_MYSQL) message(STATUS "找到mysqlclient库:\"${MYSQL_INCLUDE_DIR}\",ENABLE_MYSQL宏已打开") include_directories(${MYSQL_INCLUDE_DIR}) include_directories(${MYSQL_INCLUDE_DIR}/mysql) add_definitions(-DENABLE_MYSQL) list(APPEND LINK_LIB_LIST ${MYSQL_LIBRARIES}) endif () ``` -------------------------------- ### Define Platform-Specific Link Libraries in CMake Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This CMake snippet sets the list of libraries to link against. For Windows, it includes `WS2_32`, `Iphlpapi`, and `shlwapi`. For other platforms, the list is initially empty. ```CMake if (WIN32) set(LINK_LIB_LIST WS2_32 Iphlpapi shlwapi) else () set(LINK_LIB_LIST) endif () ``` -------------------------------- ### Set CMake Module, Library, and Executable Paths Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This snippet configures essential project paths: `CMAKE_MODULE_PATH` for custom CMake modules, `LIBRARY_OUTPUT_PATH` for compiled libraries, and `EXECUTABLE_OUTPUT_PATH` for executables. It also defines `SUB_DIR_LIST` for source subdirectories. ```CMake set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) set(SUB_DIR_LIST "Network" "Poller" "Thread" "Util") ``` -------------------------------- ### Define CMake Cache Variables for Optional Features Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This snippet defines CMake cache variables (`ENABLE_OPENSSL`, `ENABLE_MYSQL`, `ASAN_USE_DELETE`) as boolean flags, allowing users to enable or disable optional features during configuration. ```CMake set(ENABLE_OPENSSL ON CACHE BOOL "enable openssl") set(ENABLE_MYSQL ON CACHE BOOL "enable mysql") set(ASAN_USE_DELETE OFF CACHE BOOL "use delele[] or free when asan enabled") ``` -------------------------------- ### Apply Platform-Specific CMake Compile Options Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This CMake block applies different compilation settings based on the operating system. For Windows (WIN32), it adds `WIN32_LEAN_AND_MEAN` to prevent `Windows.h` from including `Winsock.h`, enables UTF-8 for MSVC, and exports all symbols. For non-Windows, it adds a warning suppression option. ```CMake if (WIN32) list(APPEND SUB_DIR_LIST "win32") add_definitions(-DWIN32_LEAN_AND_MEAN) if (MSVC) add_compile_options("/utf-8") endif () set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) else () add_compile_options("-Wno-comment") endif () ``` -------------------------------- ### Enable C++11 Standard and Suppress Warnings in CMake Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This snippet sets the C++ standard to C++11. For non-Windows platforms, it also adds compile options to suppress specific deprecation and predefined identifier warnings. ```CMake message(STATUS "将链接依赖库:${LINK_LIB_LIST}") set(CMAKE_CXX_STANDARD 11) if (NOT WIN32) add_compile_options(-Wno-deprecated-declarations) add_compile_options(-Wno-predefined-identifier-outside-function) endif () ``` -------------------------------- ### Find and Link OpenSSL Library in CMake Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This CMake snippet attempts to find the OpenSSL library. If found and `ENABLE_OPENSSL` is true, it includes OpenSSL's header directories, defines `ENABLE_OPENSSL`, and appends OpenSSL libraries to the link list. ```CMake find_package(OpenSSL QUIET) if (OPENSSL_FOUND AND ENABLE_OPENSSL) message(STATUS "找到openssl库:\"${OPENSSL_INCLUDE_DIR}\",ENABLE_OPENSSL宏已打开") include_directories(${OPENSSL_INCLUDE_DIR}) add_definitions(-DENABLE_OPENSSL) list(APPEND LINK_LIB_LIST ${OPENSSL_LIBRARIES}) endif () ``` -------------------------------- ### Configure Socket Buffer Size in CMake Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This CMake snippet checks if `SOCKET_DEFAULT_BUF_SIZE` is defined. If it is, it sets a preprocessor definition for the socket buffer size. If the value is 0, it indicates that the kernel's default value will be used (Linux specific). ```CMake if (DEFINED SOCKET_DEFAULT_BUF_SIZE) if (SOCKET_DEFAULT_BUF_SIZE EQUAL 0) message(STATUS "Socket default buffer size is not set, use the kernel default value") else () message(STATUS "Socket default buffer size is set to ${SOCKET_DEFAULT_BUF_SIZE}") endif () add_definitions(-DSOCKET_DEFAULT_BUF_SIZE=${SOCKET_DEFAULT_BUF_SIZE}) endif () ``` -------------------------------- ### Enable ASAN Use Delete Option in CMake Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This CMake snippet conditionally adds the `-DASAN_USE_DELETE` preprocessor definition if `ASAN_USE_DELETE` is enabled, which is used to address a specific issue with ASan on MacOS. ```CMake if (ASAN_USE_DELETE) add_definitions(-DASAN_USE_DELETE) endif () ``` -------------------------------- ### Add Tests Subdirectory in CMake Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This CMake snippet conditionally adds the `tests` subdirectory to the build, but only if the current platform is not iOS. ```CMake if (NOT IOS) add_subdirectory(tests) endif () ``` -------------------------------- ### Remove iOS-Specific Source Files on Non-Apple Platforms Source: https://github.com/zlmediakit/zltoolkit/blob/master/CMakeLists.txt This CMake snippet conditionally removes `Socket_ios.mm` from the source file list if the current platform is not Apple, ensuring platform compatibility. ```CMake if (NOT APPLE) list(REMOVE_ITEM SRC_LIST "src/Network/Socket_ios.mm") endif () ```