本文介绍了将std :: filesystem添加到CMake项目时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CMake项目的新手,我想在项目中使用文件系统库。我正在使用GCC 8.2和CMake 3.13运行Ubuntu 18.04。为了实现这一点,我尝试了两种选择:

I am new to CMake projects and I want to use the file system library in my project. I am running Ubuntu 18.04 with GCC 8.2 and CMake 3.13. In order to achieve this I tried two options:

选择1

cmake_minimum_required(VERSION 3.13)
project(TheFsProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-std=c++17 -lstdc++fs")

这无济于事,因为编译器在此期间仍找不到文件系统

This does not help as the compiler still cannot find the file systemlibrary during compile time.

选项2 (复制自:

make_minimum_required(VERSION 3.13)
project(TheFsProject)

set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_REQUIRED_FLAGS -std=c++17)
include(CheckCXXSymbolExists)
CHECK_CXX_SYMBOL_EXISTS(std::filesystem::path::preferred_separator cxx17fs)

if(cxx17fs)
  add_executable(TheFsProject main.cpp)
  set_property(TARGET TheFsProject PROPERTY CXX_STANDARD 17)
endif()

我收到一个我不
理解的CMake错误。

This does not help either as I get a CMake error which I don'tunderstand.

(CHECK_CXX_SYMBOL_EXISTS):
 CHECK_CXX_SYMBOL_EXISTS Macro invoked with incorrect arguments for macro named: CHECK_CXX_SYMBOL_EXISTS

我对这个主题不了解,这就是为什么我来了我不介意花更多的精力去寻找更多的东西,但我不知道该去哪里。任何帮助将不胜感激!

I feel out of my depth on this topic which is why I came here. I don't mind putting extra work into finding out more but I don't know anymore where to look. Any help would be appreciated!

感谢您的答复!我根据您的反馈做出了选项3

Thanks for the replies so far! I made Option 3 based on your feedback:

cmake_minimum_required(VERSION 3.13)
project(TheFsProject)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(TheFsProject main.cpp)
target_link_libraries(TheFsProject stdc++fs)

不幸的是,它不能解决我的问题。

Sadly it doesn't fix my problem. It still issues an error during compilation that it can't find the compilation header.

在编译期间仍然会出现错误,即找不到编译头。到目前为止的所有答复。所有这些帮助。我最后尝试了Ashkan的答案(因为它似乎令人生畏)。这个返回

Thanks for all the replies so far. All of these help. I tried Ashkan his answer last (because it seemed intimidating). This one returns

所以我猜想那是错误的。从我现在知道可能不是由于我的CMake文件的角度来看,这很有用。现在,我必须找出为什么编译器确实支持文件系统头文件...

so I'm guessing something is wrong on that end. This is useful in the sense that I now know it's probably not due to my CMake file. I now have to find out why the compiler does support the file system header though...

严格来说,回答此问题是因为我的问题与CMake文件有关。我将把Ashkan的答案标记为解决方案,只是因为它产生了我的故障排除搜索的下一步。如果可以的话,我也要标记他的答案,因为我认为这也是一个很好的答案。谢谢大家!

Strictly speaking this question is answered because my question is about the CMake file. I am going to mark Ashkan his answer as the solution simply because it produced the next step in my troubleshooting search. If I could I would also mark lubgr his answer because I think that's a really good answer as well. Thanks everyone!

推荐答案

除了@lubgr的答案。我认为更完整的方法是也进行try_compile来查看您是否可以实际使用文件系统标头。我认为这更好,因为某些编译器尚不支持std :: filesystem。同样在gcc 7.x中,文件系统位于 experimental 命名空间下。这样,您可以在 else 子句中使用单独的try_compile并进行检测。

Besides from @lubgr's answer. I think a more complete way is to also do try_compile to see that you can actually use the filesystem header. This in my opinion is better because some compilers are not supporting std::filesystem yet. Also in gcc 7.x you have the filesystem under experimental namespace. This way you can have a separate try_compile in the else clause and detect that.

这是它的相关内容

# set everything up for c++ 17 features
set(CMAKE_CXX_STANDARD 17)
# Don't add this line if you will try_compile with boost.
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# test that filesystem header actually is there and works
try_compile(HAS_FS "${CMAKE_BINARY_DIR}/temp"
"${CMAKE_SOURCE_DIR}/tests/has_filesystem.cc"
            CMAKE_FLAGS -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=ON
            LINK_LIBRARIES stdc++fs)
if(HAS_FS)
    message(STATUS "Compiler has filesystem support")
else()
#   .... You could also try searching for boost::filesystem here.
    message(FATAL_ERROR "Compiler is missing filesystem capabilities")
endif(HAS_FS)

文件tests / has_filesystem.cc非常简单

The file tests/has_filesystem.cc is very simple

#include <filesystem>

namespace fs = std::filesystem;

int main()
{
    fs::path aPath {"../"};

    return 0;
}

您可以在else子句中将try_compile用于boost :: filesystem并传递指令可以在您决定要使用c ++ 17文件系统还是boost的源文件中使用。

You could in your else clause try_compile for boost::filesystem and pass a directive that can be used in your source file where you decide if you want to use c++17 filesystem or boost.

这篇关于将std :: filesystem添加到CMake项目时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 13:27