Ziggo-CaaS-Switch软件配置: undefined reference to pthread_create
Ziggo-CaaS-Switch软件配置编译错误:
[ 88%] Built target time_sync_app
Scanning dependencies of target time_sync
[ 91%] Building C object CMakeFiles/time_sync.dir/time_sync_main_loop.c.o
[ 94%] Linking C executable time_sync
/usr/bin/ld: CMakeFiles/time_sync.dir/time_sync_main_loop.c.o: in function `main':
time_sync_main_loop.c:(.text+0x1be3): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/time_sync.dir/build.make:85: time_sync] Error 1
make[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/time_sync.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
错误分析:pthread_create 属于线程库,当前链接 time_sync 时没有把线程库真正带进去,所以在最后 ld 阶段失败。其实,undefined reference to pthread_create 是典型的“编译时带了 -lpthread,但链接阶段没有正确链接线程库”的问题。你这个仓库的 CMakeLists.txt 把 -lpthread 塞进了 CMAKE_C_FLAGS,这并不可靠,尤其在可执行文件链接阶段经常失效。
解决步骤:
直接改交换机软件目录下的 CMakeLists.txt:
xx/Ziggo-CaaS-Switch-main/Ziggo-CaaS-Switch-main/Software/Time-Synchronization/CMakeLists.txt
把末尾这两段:
add_executable(time_sync time_sync_main_loop.c)
target_link_libraries(time_sync ${PROJECT_NAME})
add_executable(switch_config switch_config_main.c)
target_link_libraries(switch_config ${PROJECT_NAME})
改成更稳妥的写法:
find_package(Threads REQUIRED)
add_executable(time_sync time_sync_main_loop.c)
target_link_libraries(time_sync ${PROJECT_NAME} Threads::Threads m)
add_executable(switch_config switch_config_main.c)
target_link_libraries(switch_config ${PROJECT_NAME} Threads::Threads m)
如果你的环境里 Threads::Threads 解析有问题,也可以先用简单版本:
target_link_libraries(time_sync ${PROJECT_NAME} pthread m)
target_link_libraries(switch_config ${PROJECT_NAME} pthread m)
然后重新构建,最好清掉原来的 build 目录重新跑一遍:
cd /path/to/Software/Time-Synchronization
rm -rf build
mkdir build
cd build
cmake ..
make -j

更多推荐
所有评论(0)