../../../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/apps/g2o_cli/CMakeFiles/g2o_cli_application.dir/build.make:99: recipe for target 'bin/g2o' failed make[2]: *** [bin/g2o] Error 1 CMakeFiles/Makefile2:1230: recipe for target 'g2o/apps/g2o_cli/CMakeFiles/g2o_cli_application.dir/all' failed make[1]: *** [g2o/apps/g2o_cli/CMakeFiles/g2o_cli_application.dir/all] Error 2 Makefile:155: recipe for target 'all' failed make: *** [all] Error 2
时间: 2025-08-06 08:57:07 浏览: 20
### 编译 g2o 时解决 `std::filesystem` 相关符号未定义导致的链接错误
在编译 `g2o` 时,如果遇到与 `std::filesystem` 相关的符号未定义(如 `undefined reference to std::filesystem::...`)的链接错误,通常是因为 C++17 标准未被正确启用,或者未链接标准库的文件系统实现。
#### 启用 C++17 标准
确保在编译项目时启用了 C++17 标准。可以在 `CMakeLists.txt` 中添加以下内容:
```cmake
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
```
这将强制 CMake 使用 C++17 标准进行编译,确保 `<filesystem>` 头文件可以被正确识别和使用[^4]。
#### 显式链接 `-lstdc++fs` 或 `-lc++fs`
在某些系统(如 Ubuntu 18.04)中,即使启用了 C++17 标准,`std::filesystem` 的实现可能仍需要手动链接。可以在 `CMakeLists.txt` 中的 `target_link_libraries` 部分添加 `-lstdc++fs`:
```cmake
target_link_libraries(your_target_name g2o_core g2o_types_slam3d g2o_solver_csparse g2o_stuff g2o_csparse_extension -lstdc++fs)
```
这样可以确保链接器在构建过程中包含 `std::filesystem` 的实现[^4]。
#### 替换为 `boost::filesystem`(可选)
如果由于环境限制无法使用 C++17 的 `<filesystem>`,可以考虑使用 `boost::filesystem` 替代。首先安装 Boost 库:
```bash
sudo apt install libboost-filesystem-dev
```
然后在代码中将:
```cpp
#include <filesystem>
```
替换为:
```cpp
#include <boost/filesystem.hpp>
```
同时修改命名空间调用,将 `std::filesystem::` 替换为 `boost::filesystem::`。这种方式可以避免对 C++17 的依赖[^1]。
#### 确保 g2o 的 CMake 配置正确
如果 `g2o` 的构建系统未正确配置,也可能导致链接错误。确保在构建 `g2o` 时,CMake 能够找到并链接所需的库。可以在 `cmake` 命令中显式指定标准:
```bash
cmake -DCMAKE_CXX_STANDARD=17 ..
```
此外,确保所有依赖库(如 `g2o_core`、`g2o_solver_csparse` 等)都被正确链接到目标中[^1]。
---
阅读全文
相关推荐
















