- 准备编译环境为wsl
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/99c0a95cdb50be06f12953d516371a5f.png#pic_center)
- 编写cmake文件
源码根下CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(unpv13e-master)
include_directories(./key)
#添库文件
add_subdirectory(./lib)
#add_subdirectory(./libroute)
#添加链接参数 -pthread (make版本比较低的使用-lpthread)
link_libraries(libunp pthread)
#各模块文件
add_subdirectory(./intro)
#add_subdirectory(./names)
#add_subdirectory(./unixdomain)
#add_subdirectory(./nonblock)
#add_subdirectory(./ioctl)
#add_subdirectory(./route)
#add_subdirectory(./key)
lib库下文件CMakeLists.txt
set(source
connect_nonb.c connect_timeo.c daemon_inetd.c daemon_init.c dg_cli.c dg_echo.c error.c get_ifi_info.c gf_time.c
host_serv.c family_to_level.c mcast_leave.c mcast_join.c mcast_get_if.c mcast_get_loop.c mcast_get_ttl.c
mcast_set_if.c mcast_set_loop.c mcast_set_ttl.c my_addrs.c read_fd.c readline.c readn.c readable_timeo.c rtt.c
signal.c signal_intr.c sock_bind_wild.c sock_cmp_addr.c sock_cmp_port.c sock_ntop.c sock_ntop_host.c
sock_get_port.c sock_set_addr.c sock_set_port.c sock_set_wild.c sockfd_to_family.c str_cli.c str_echo.c
tcp_connect.c tcp_listen.c tv_sub.c udp_client.c udp_connect.c udp_server.c wraplib.c wrapsock.c wrapstdio.c
wrapunix.c write_fd.c writen.c writable_timeo.c wrappthread.c
)
add_library(libunp SHARED ${source})
各模块下的 这里使用./intro模块举例
#源文件
aux_source_directory(. source)
message(${source})
foreach(SUBDIR ${source})
STRING(REGEX REPLACE ".+/(.+)\\..*" "\\1" FILE_NAME ${SUBDIR})
message(${FILE_NAME})
add_executable(${FILE_NAME} ${SUBDIR})
endforeach()