gpt4 book ai didi

c++ - 如何在文件系统的 Eclipse C++ 中添加 -lstdc++fs?

转载 作者:行者123 更新时间:2023-11-30 04:59:45 26 4
gpt4 key购买 nike

这是在命令行中工作的 c++ filesystem 上的演示 filesystem-testing.cpp:

环境:gcc/5.5.0

#include <iostream>
#include <string>
#include <experimental/filesystem>

int main()
{
std::string path = std::experimental::filesystem::current_path();

std::cout << "path = " << path << std::endl;
}

编译测试如下:

$ g++ -std=c++11 filesystem-testing.cpp -lstdc++fs
$ ./a.out
path = /home/userid/projects-c++/filesystem-testing

如何在 Eclipse GCC C++ Compiler 和/或 GCC C++ Linker 中添加 -lstdc++fs

以下方法无效并且对 '_ZNSt12experimental10filesystem2v112current_pathB5cxx11Ev' 有 undefined reference

<强>1。案例A

enter image description here

<强>2。案例B

enter image description here

最佳答案

您提供的示例命令行在单个命令中从源文件创建可执行文件,但通常分两步完成:

g++ -c -std=c++11 filesystem-testing.cpp  # creates filesystem-testing.o
g++ filesystem-testing.o -lstdc++fs # creates a.out

第一步调用编译器,第二步调用链接器(g++ 是两者的驱动程序)。

请注意,-lstdc++fs 标志属于链接器命令,而不是编译器命令。

Eclipse 的托管构建系统也在这两个步骤中执行编译。因此,您需要在链接器选项中指定 -lstdc++fs(例如 Tool Settings | GCC C++ Linker | Linker flags)。


更新:好的,我试过了,我发现将标志添加到 Linker flags 是不够的。

要了解原因,让我们看一下控制台 View 中的输出:

23:13:04 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -o test.o ../test.cpp
g++ -lstdc++fs -o test test.o
test.o: In function `main':
/home/nr/dev/projects/c++/test/Debug/../test.cpp:7: undefined reference to `std::experimental::filesystem::v1::current_path[abi:cxx11]()'
collect2: error: ld returned 1 exit status

23:13:05 Build Failed. 1 errors, 0 warnings. (took 880ms)

请注意,它向您显示了它正在运行的命令。它运行的链接器命令是:

g++ -lstdc++fs -o test test.o

这在一个重要方面与我上面写的不同:-lstdc++fs 选项在需要它的输入文件 (test.o) 之前,而不是之后。 Order matters用于 GCC 链接器。

要解决此问题,我们需要让 Eclipse 更改构建系统的参数顺序。您可以通过修改 Tool Settings | 来做到这一点GCC C++ 链接器 |专家设置 |命令行模式。这基本上是 Eclipse 如何构造链接器命令行的模式。它的默认值是:

${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}

请注意 ${FLAGS}(-lstdc++fs 所在的位置)如何出现在 ${INPUTS}(其中是 test.o 所在的位置)。

让我们重新排序为:

${COMMAND} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} ${FLAGS}

然后再次尝试构建:

23:20:26 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
g++ -o test test.o -lstdc++fs

23:20:26 Build Finished. 0 errors, 0 warnings. (took 272ms)

现在一切都很好!

更新 2:以下内容与快照中突出显示的内容相同。重要的是不要添加 -l:

enter image description here

关于c++ - 如何在文件系统的 Eclipse C++ 中添加 -lstdc++fs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51064067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com