CMake错误解释c++程序中的类型

5cnsuln7  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(188)

日安。在不使用cmake“”g++ -lpthread src/how2thread.cpp src/main.cpp -ggdb 3-std=c++17 -Wextra“”的情况下编译程序不会产生错误。但是,使用“”cmake . && make“”会产生以下编译错误:

[ 25%] Building CXX object CMakeFiles/how2thread_lib.dir/src/how2thread.cpp.obj
D:/Scizzors/Dsktp/cpp/how2thread/src/how2thread.cpp: In member function 'how2thread::Request how2thread::Scheduler::get_request()':
D:/Scizzors/Dsktp/cpp/how2thread/src/how2thread.cpp:103:67: error: use of deleted function 'std::lock_guard<_Mutex>::lock_guard(const std::lock_guard<_Mutex>&) [with _Mutex = std::mutex]'
  103 |         auto m_lock = std::lock_guard<std::mutex>(data_arr_protect);
      |                                                                   ^
In file included from C:/msys64/mingw64/include/c++/12.1.0/mutex:43,
                 from D:/Scizzors/Dsktp/cpp/how2thread/src/how2thread.hpp:8,
                 from D:/Scizzors/Dsktp/cpp/how2thread/src/how2thread.cpp:4:
C:/msys64/mingw64/include/c++/12.1.0/bits/std_mutex.h:237:7: note: declared here
  237 |       lock_guard(const lock_guard&) = delete;

我在头文件中指定data_arr_protect是std::mutex对象,

class Scheduler
{
// class for schedule slicers, processors and collecting data
public:
    Scheduler(const std::vector<char>& data,
              const std::string& mask); // construct and parse data
    Request get_request(); // used to get another batch
    void write_request(Request&&);

    void write_finding(Finding&& find);

    using Find_it = decltype(std::declval<std::vector<Finding>&>().cbegin());
    Find_it cbegin();
    Find_it cend();

    const auto& get_slicing_status(){return is_slicing;} //
    std::string mask;

# ifndef DEBUG_V

private:

# endif

    const size_t slicer_num;
    std::queue<Request> data_arr; // consider switching to deQ
    std::mutex data_arr_protect;//for mt rw to vec // might be switching to semaphores

    std::queue<Actor_processor> proc_instances_q;
    std::queue<Actor_slicer> slicer_instances_q;

    std::vector<Finding> result_arr;
    std::mutex result_protect;//for mt write to vec

protected:
    friend class Actor_slicer; // we need access from slicers to mutexes as they're pretty lowlvl
    std::vector<std::mutex> char_segments_protect;
    std::vector<size_t> finish_line_no;
    std::atomic<size_t> is_slicing; // for break cond 
};

我cmake文件看起来

cmake_minimum_required(VERSION 3.10.0)
project(how2thread
        VERSION 0.0.1
        DESCRIPTION "simple mt parser app"
        LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

file(GLOB_RECURSE src_files src/*.cpp)

add_library(how2thread_lib src/how2thread.cpp src/how2thread.hpp)

add_executable(how2thread src/main.cpp)
target_link_libraries(how2thread PRIVATE how2thread_lib)
target_link_libraries(how2thread PRIVATE Threads::Threads)

什么会导致编译器的误解,我必须如何修复它?
我尝试了不同的链接选项,比如用main.cpp文件构建我的how2thread.cpp文件,尽管它没有帮助。而且我没有得到关于未定义类或其他东西的错误,所以,我猜,头文件被包含在.cpp文件中

ql3eal8s

ql3eal8s1#

没有误解任何内容。问题在于以下行:(它只出现在错误消息中,而不是您显示的代码中。请始终提供最少的可重现示例!)

auto m_lock = std::lock_guard<std::mutex>(data_arr_protect);

这是从C17之前的初始化表达式复制初始化m_lock,该表达式要求该类型是可移动构造的,而std::lock_guard不是。基于同样的原因,直接初始化auto(例如auto m_lock(std::lock_guard<std::mutex>(data_arr_protect));)也不起作用。
若要编译这一行,您需要使用C
17或更高版本,但您要求cmake将C++14设置为

set(CMAKE_CXX_STANDARD 14)

因此,将14更改为17
或者,如果您需要与C++14兼容,则必须避免使用auto初始化习惯用法来初始化不可移动类型:

std::lock_guard<std::mutex> m_lock(data_arr_protect);

相关问题