You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSL2 Ubuntu编译Facebook Folly库遇clock_gettime兼容性错误求助

WSL2 Ubuntu 20.04编译Folly库时clock_gettime兼容性错误的解决办法

问题情况

环境信息:

$ uname -r
5.15.90.1-microsoft-standard-WSL2
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.5 LTS
Release:        20.04
Codename:       focal

编译到Time.cpp时出现报错,关键信息如下:

/home/orange/folly/folly/portability/Time.cpp:278:2: error: No clock_gettime(3) compatibility wrapper available for this platform.
#error No clock_gettime(3) compatibility wrapper available for this platform.

已尝试操作:

  • 搜索相关Issue未找到有效方案
  • 添加-lrt编译参数无效

可行修复方案

1. 强制在Time.cpp中定义_GNU_SOURCE

虽然编译命令已包含-D_GNU_SOURCE,但部分模块可能未正确传递该宏。直接修改folly/portability/Time.cpp,在文件最开头添加:

#define _GNU_SOURCE

修改后重新执行编译流程。

2. 让CMake强制链接librt库

两种方式可选:

  • 直接修改Folly根目录的CMakeLists.txt,找到target_link_libraries相关配置,给folly_base添加rt库依赖:
target_link_libraries(folly_base PUBLIC rt)
  • 编译时通过CMake参数传递:
cmake -DFOLLY_LINK_RT=ON ..

完成配置后重新编译。

3. 升级gcc版本

Ubuntu 20.04默认gcc版本可能存在兼容性问题,升级到gcc 9尝试:

sudo apt update
sudo apt install gcc-9 g++-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 90

切换版本后重新初始化编译环境再执行编译。

4. 更新WSL2内核与系统

先更新Ubuntu系统:

sudo apt update && sudo apt upgrade -y

再在Windows管理员权限的PowerShell中更新WSL2内核:

wsl --update

重启WSL后重新编译。

验证

完成上述操作后,重新执行编译命令:

cmake --build /tmp/fbcode_builder_getdeps-ZhomeZorangeZfollyZbuildZfbcode_builder/build/folly --target install --config Release -j 3

确认编译是否成功。

内容的提问来源于stack exchange,提问作者orange river

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 04:23:13