编译g2o,输入make后../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::parent_path() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator*() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_parent_path() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::_M_find_extension() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::symlink_status(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_root_directory() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::status(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator++()' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::directory_iterator(std::filesystem::__cxx11::path const&, std::filesystem::directory_options, std::error_code*)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::read_symlink(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::replace_extension(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_filename() const' collect2: error: ld returned 1 exit status g2o/examples/data_fitting/CMakeFiles/curve_fit.dir/build.make:99: recipe for target 'bin/curve_fit' failed make[2]: *** [bin/curve_fit] Error 1 CMakeFiles/Makefile2:1924: recipe for target 'g2o/examples/data_fitting/CMakeFiles/curve_fit.dir/all' failed make[1]: *** [g2o/examples/data_fitting/CMakeFiles/curve_fit.dir/all] Error 2 make[1]: *** Waiting for unfinished jobs....../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::parent_path() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator*() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_parent_path() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::_M_find_extension() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::symlink_status(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_root_directory() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::status(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator++()' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::directory_iterator(std::filesystem::__cxx11::path const&, std::filesystem::directory_options, std::error_code*)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::read_symlink(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::replace_extension(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_filename() const' collect2: error: ld returned 1 exit status g2o/examples/data_fitting/CMakeFiles/circle_fit.dir/build.make:99: recipe for target 'bin/circle_fit' failed make[2]: *** [bin/circle_fit] Error 1 CMakeFiles/Makefile2:1896: recipe for target 'g2o/examples/data_fitting/CMakeFiles/circle_fit.dir/all' failed make[1]: *** [g2o/examples/data_fitting/CMakeFiles/circle_fit.dir/all] Error 2Makefile:155: recipe for target 'all' failed make: *** [all] Error 2
时间: 2025-08-06 13:57:02 浏览: 15
在编译 `g2o` 库时,如果遇到涉及 `std::filesystem` 的链接错误,例如:
```
undefined reference to `std::filesystem::path::filename() const'
```
这类错误通常表明编译器在链接阶段未能正确找到 `std::filesystem` 所依赖的库。`std::filesystem` 是 C++17 标准引入的特性,因此需要确保编译环境支持 C++17,并且在链接时显式指定标准库的支持。
### 常见原因与解决方法
1. **启用 C++17 标准支持**
在 `CMakeLists.txt` 文件中,确保设置了 C++ 标准为 C++17 或更高。可以添加以下内容:
```cmake
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
```
这将确保编译器在编译阶段启用 C++17 特性。
2. **链接 `stdc++fs` 库**
在某些编译器实现中(如 GCC),`std::filesystem` 的实现并未默认链接到项目中,需要显式链接 `-lstdc++fs` 或 `-lfilesystem`。可以在 `CMakeLists.txt` 中添加以下内容:
```cmake
target_link_libraries(your_target_name PRIVATE -lstdc++fs)
```
如果使用的是较新的 GCC 版本(如 GCC 9 及以上),可以尝试使用 `-lfilesystem` 替代:
```cmake
target_link_libraries(your_target_name PRIVATE -lfilesystem)
```
3. **检查编译器版本**
确保使用的编译器版本支持 `std::filesystem`。GCC 从版本 8 开始对 `std::filesystem` 提供较好的支持,建议使用 GCC 8 或更新版本。可以通过以下命令检查编译器版本:
```bash
g++ --version
```
4. **使用 `CMake` 的 `find_package` 检查依赖**
如果项目依赖外部库(如 Boost 或其他文件系统相关库),应确保在 `CMakeLists.txt` 中正确配置依赖项。例如,使用 `find_package(Boost REQUIRED)` 来引入 Boost 库,并通过 `target_link_libraries` 链接相关目标。
5. **清理并重新构建项目**
有时,旧的构建文件可能会导致链接问题。可以尝试清理构建目录并重新运行 `CMake`:
```bash
rm -rf build/
mkdir build
cd build
cmake ..
make
```
### 示例:修改后的 CMakeLists.txt 片段
```cmake
cmake_minimum_required(VERSION 3.14)
project(g2o_example)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(g2o_example src/main.cpp)
target_link_libraries(g2o_example PRIVATE -lstdc++fs)
```
### 其他注意事项
- 如果使用的是 Clang 编译器,可能需要通过 `-lc++fs` 来链接 `std::filesystem` 的实现。
- 某些 Linux 发行版的默认工具链可能未完全支持 C++17,建议更新系统或使用自定义安装的编译器。
- 确保 `CMake` 版本不低于 3.14,以更好地支持 C++17 特性。
###
阅读全文
相关推荐













