问题描述
我想在Qt 5.12.0和g ++版本Ubuntu 8.2.0-7ubuntu1中使用std::filesystem
,但出现链接器错误:
I would like to use the std::filesystem
with Qt 5.12.0 with the g++ version Ubuntu 8.2.0-7ubuntu1, but getting linker errors:
g++ -lstdc++fs -Wl,-rpath,/home/user/Qt/5.12.0/gcc_64/lib -o qf_filesystem_test main.o -L/home/user/Qt/5.12.0/gcc_64/lib -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread
/usr/bin/ld: main.o: in function `std::filesystem::exists(std::filesystem::__cxx11::path const&)':
/usr/include/c++/8/bits/fs_ops.h:121: undefined reference to `std::filesystem::status(std::filesystem::__cxx11::path const&)'
/usr/bin/ld: main.o: in function `std::filesystem::__cxx11::path::path<char*, std::filesystem::__cxx11::path>(char* const&, std::filesystem::__cxx11::path::format)':
/usr/include/c++/8/bits/fs_path.h:183: undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status
make: *** [Makefile:257: qf_filesystem_test] Error 1
22:12:16: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project qf_filesystem_test (kit: Desktop Qt 5.12.0 GCC 64bit)
When executing step "Make"
在进行一些谷歌搜索之后,我发现我需要使用链接器标志-lstdc++fs
.我的代码可以使用命令g++ main.cpp -std=c++17 -lstdc++fs
完美构建,但是我似乎无法使其在Qt Creator中运行.我的简单测试代码如下:
After some googling, I found that I need to use the linker flag -lstdc++fs
. My code builds perfectly with the command g++ main.cpp -std=c++17 -lstdc++fs
, but I can't seem to make it work inside Qt Creator. My simple test code is the following:
#include <iostream>
#include <filesystem>
int main(int argc, char *argv[])
{
if(1 < argc)
{
std::cout << argv[1] << " does ";
if(!std::filesystem::exists(std::filesystem::path(argv[1]))) std::cout << "not ";
std::cout << "exist!" << std::endl;
}
return 0;
}
我的.pro文件如下:
My .pro file looks like this:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = qf_filesystem_test
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
CONFIG += c++17
QMAKE_LFLAGS += -lstdc++fs
SOURCES += main.cpp
在使用g ++进行了一些测试之后,在我看来,问题是由g ++标志的顺序引起的,因为Qt将-lstdc++fs
放在前面.
After some tests with g++ It seems to me, that the problem is caused by the order of the g++ flags, because Qt puts the -lstdc++fs
to the front.
- 为什么我仍然需要使用此标志?我以为g ++ 8已经支持C ++ 17,只有在我要使用
std::experimental::filesystem
时才需要此标志. - 如何使我的代码在Qt Creator中构建?
- Why do I need to still use this flag? I thought that g++8 already supports C++17 and this flag is only needed if I want to use the
std::experimental::filesystem
. - How could I make my code build in Qt Creator?
推荐答案
<filesystem>
是GCC 8的单独库().如您所怀疑,您的问题按标志的顺序排列.在文档中仔细研究一下,暗示链接器标志QMAKE_LFLAGS
比库加载更重要,这就是为什么它较早通过的原因(例如-O3
).
<filesystem>
is a separate library for GCC 8 (see this question). Your issue, as you suspected, is in the order of the flags. Poking about a bit in the docs hints that QMAKE_LFLAGS
is more for linker flags than library loads, which is why it gets passed early (e.g. -O3
).
使用LIBS += -lstdc++fs
应该可以.
为此解决方案贷到此reddit响应.
这篇关于如何避免Qt的std :: filesystem链接器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!