首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CMake:“调用非”_Tp“函数”_Tp std::_Tp

CMake:“调用非”_Tp“函数”_Tp std::_Tp
EN

Stack Overflow用户
提问于 2021-04-02 16:17:20
回答 1查看 312关注 0票数 0

在一个CMake项目中,我得到了输出:

代码语言:javascript
复制
[main] Building folder: cmake-test 
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/jonathan/Projects/cmake-test/build --config Debug --target all -- -j 14
[build] Scanning dependencies of target hello_cmake
[build] [ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.o
[build] /home/jonathan/Projects/cmake-test/main.cpp: In function ‘int main()’:
[build] /home/jonathan/Projects/cmake-test/main.cpp:25:43: error: ‘constexpr size_t pushConstantsSize(const std::array<std::variant<unsigned int, float>, NumPushConstants>&) [with long unsigned int NumPushConstants = 1; size_t = long unsigned int]’ called in a constant expression
[build]    25 |     constexpr size_t x = pushConstantsSize(pushConstants);
[build]       |                          ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
[build] /home/jonathan/Projects/cmake-test/main.cpp:9:18: note: ‘constexpr size_t pushConstantsSize(const std::array<std::variant<unsigned int, float>, NumPushConstants>&) [with long unsigned int NumPushConstants = 1; size_t = long unsigned int]’ is not usable as a ‘constexpr’ function because:
[build]     9 | constexpr size_t pushConstantsSize(std::array<std::variant<uint32_t,float>,NumPushConstants> const& pushConstants) {
[build]       |                  ^~~~~~~~~~~~~~~~~
[build] /home/jonathan/Projects/cmake-test/main.cpp:14:47: error: call to non-‘constexpr’ function ‘_Tp std::accumulate(_InputIterator, _InputIterator, _Tp, _BinaryOperation) [with _InputIterator = const std::variant<unsigned int, float>*; _Tp = long unsigned int; _BinaryOperation = pushConstantsSize(const std::array<std::variant<unsigned int, float>, NumPushConstants>&) [with long unsigned int NumPushConstants = 1; size_t = long unsigned int]::<lambda(std::size_t, auto:23)>]’
[build]    14 |     return static_cast<size_t>(std::accumulate(pushConstants.cbegin(),pushConstants.cend(),
[build]       |                                ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[build]    15 |         std::size_t{ 0 },
[build]       |         ~~~~~~~~~~~~~~~~~                      
[build]    16 |         [size_fn](std::size_t acc, auto const var) { return acc + std::visit(size_fn,var); }
[build]       |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[build]    17 |     ));
[build]       |     ~                                          
[build] make[2]: *** [CMakeFiles/hello_cmake.dir/build.make:63: CMakeFiles/hello_cmake.dir/main.cpp.o] Error 1
[build] make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/hello_cmake.dir/all] Error 2
[build] make: *** [Makefile:84: all] Error 2
[build] Build finished with exit code 2

IntelliSense给出的错误:

main.cpp:

代码语言:javascript
复制
#include <numeric>
#include <array> // std::array
#include <numeric> // std::accumulate
#include <variant> // std::variant
#include <stdint.h> // uint32_t
#include <iostream>

template <size_t NumPushConstants>
constexpr size_t pushConstantsSize(std::array<std::variant<uint32_t,float>,NumPushConstants> const& pushConstants) {
    auto size_fn = [](auto const& var) -> size_t {
        using T = std::decay_t<decltype(var)>;
        return sizeof(T);
    };
    return static_cast<size_t>(std::accumulate(pushConstants.cbegin(),pushConstants.cend(),
        std::size_t{ 0 },
        [size_fn](std::size_t acc, auto const var) { return acc + std::visit(size_fn,var); }
    ));
}

int main() {
    constexpr size_t num = 1;
    static std::array<std::variant<uint32_t,float>,num> const pushConstants = {
        uint32_t {  1 }
    };
    constexpr size_t x = pushConstantsSize(pushConstants);
    std::cout << "size:" << x <<std::endl;
}

CMakeLists.txt

代码语言:javascript
复制
cmake_minimum_required(VERSION 3.16)
project (hello_cmake)
set(CMAKE_CXX_STANDARD 20)
add_executable(${PROJECT_NAME} main.cpp)

最小可复制exmaple项目zip:JDq1uJIUuTv/view?usp=共享

在这里实现相同的代码( https://gcc.godbolt.org/z/esPd87r4o )时,它可以工作,这并不意外,因为这可能是CMake配置的一个问题。

对于constexprstd::accumulate实现,我使用CPP 20和GCC 10.2.0,具体如下所示:

数值算法的constexpr

At:支持

CMakeLists.txt包括set(CMAKE_CXX_STANDARD 20)

看看安装在我的系统上的编译器:

代码语言:javascript
复制
jonathan@jonathan-MS-7B22:~$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

jonathan@jonathan-MS-7B22:~$ gcc-10 --version
gcc-10 (Ubuntu 10.2.0-5ubuntu1~20.04) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

jonathan@jonathan-MS-7B22:~$ g++ --version
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

jonathan@jonathan-MS-7B22:~$ g++-10 --version
g++-10 (Ubuntu 10.2.0-5ubuntu1~20.04) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

jonathan@jonathan-MS-7B22:~$ 

安装了CMake:

代码语言:javascript
复制
jonathan@jonathan-MS-7B22:~$ cmake --version
cmake version 3.16.3

CMake suite maintained and supported by Kitware (kitware.com/cmake).
jonathan@jonathan-MS-7B22:~$

为了方便起见,我使用了visual代码CMake工具扩展,设置如下:

我目前的最佳猜测是,某些方面默认使用gcc over gcc-10,这会导致影响整个项目的问题,但我不知道这可能发生在哪里,也不知道如何修复它。

我对这里所做的一切感到迷惑不解,任何帮助都将是非常感谢的,谢谢你阅读了这么多。

更新

我相信我已经找到了错误的来源,在重新配置项目时,我得到了输出:

代码语言:javascript
复制
[main] Configuring folder: cmake-test 
[driver] Removing /home/jonathan/Projects/cmake-test/build/CMakeCache.txt
[driver] Removing /home/jonathan/Projects/cmake-test/build/CMakeFiles
[proc] Executing command: /usr/bin/cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=/bin/gcc-10 -H/home/jonathan/Projects/cmake-test -B/home/jonathan/Projects/cmake-test/build -G "Unix Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 10.2.0
[cmake] -- The CXX compiler identification is GNU 9.3.0
[cmake] -- Check for working C compiler: /bin/gcc-10
[cmake] -- Check for working C compiler: /bin/gcc-10 -- works
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Check for working CXX compiler: /bin/c++
[cmake] -- Check for working CXX compiler: /bin/c++ -- works
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: /home/jonathan/Projects/cmake-test/build

值得注意的是The CXX compiler identification is GNU 9.3.0,我认为这是最终导致错误的原因,我需要将The CXX compiler设置为10.2.0。我不知道该怎么改变这个。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-02 20:24:34

正如更新部分注意的那样,要修复这个问题,请注意The CXX compiler identification is GNU 9.3.0,您可以执行set(CMAKE_CXX_COMPILER "/usr/bin/g++-10") (从https://stackoverflow.com/a/22619145/4301453)并重新加载项目。

这似乎很管用。

虽然我不确定这是最好的答案,但它现在起作用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66922186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档