您提供的示例命令行通过单个命令从源文件创建可执行文件,但通常分两步完成:
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 是不够的。
要了解原因,让我们看一下控制台视图中的输出:
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++ Linker | Expert settings | Command line pattern 来做到这一点。这基本上是 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: