call to non-‘constexpr’ function
概述
在嘗試遷移 openpnp - Smoothieware project 從gcc命令行 + MRI調(diào)試方式 到NXP MCUXpresso工程.
在加了頭文件路徑后, 還有一些語法錯誤. 這和編譯器語法有關(guān)系.
在運行BuildShell.cmd后, 查看gcc版本如下.
D:\3rd_prj\Smoothieware_best-for-pnp>gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-9.2.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-static --enable-shared --enable-threads --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --enable-libgomp --disable-libvtv --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --disable-build-format-warnings --prefix=/mingw --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --enable-nls --with-pkgversion='MinGW.org GCC Build-2'
Thread model: win32
gcc version 9.2.0 (MinGW.org GCC Build-2)
Smoothieware使用c++寫的.
其中一條錯誤如下:
描述 資源 路徑 位置 類型
call to non-'constexpr' function 'uint16_t get_checksum(const char*)' checksumm.h /my_Smoothieware_best-for-pnp/src/libs 第 162 行 C/C++ Problem
查了一下這條錯誤的原因, 就是語法錯誤.
在switch的case處, 用了常量表達式函數(shù)(一個函數(shù)來代替常量, 這個函數(shù)在編譯期就能確定值), 那么這個常量表達式就必須標記為constexpr
使用constexpr比宏更好, 更明確.
做了一個實驗, 可以正常使用constexpr了.文章來源:http://www.zghlxwxcb.cn/news/detail-444191.html
// ConsoleApplication2.cpp : 此文件包含 "main" 函數(shù)。程序執(zhí)行將在此處開始并結(jié)束。
//
#include <iostream>
// constexpr 是c++11的語法, vs2022默認的最低編譯器是c++14, 符合要求.
// constexpr好處是不用再寫宏. 而是由編譯器在編譯時, 就能確定一個常量表達式的值. 這個常量表達式是有值類型的, 比宏好.
// 如果函數(shù)需要作為常量表達式, 必須標記為 constexpr
constexpr int test(int x, int y)
{
return (x + y);
}
int inum_ary[test(1,2)];
class cls_adc {
public:
cls_adc()
{
}
virtual ~cls_adc()
{
}
int get_x()
{
return inum_ary[2];
}
static const int i_ary_cnt = test(1, 2); // 如果constexpr函數(shù)的值要間接作為一個常量用, 這個常量必須為 static const type
int inum_ary[i_ary_cnt];
};
int main()
{
std::cout << "Hello World!\n";
int i = 0;
cls_adc case1;
printf("get_x() = %d\n", case1.get_x());
for (i = 0; i < case1.get_x(); i++)
{
switch (i)
{
// 作為case使用時的常量表達式函數(shù), 必須標記為 constexpr
case test(0, 1):
printf("bp1\n");
break;
default:
break;
}
}
system("pause");
}
備注
在具體工程中的錯誤, 在具體去改.
不清楚為啥同樣的代碼, 在gcc命令行 + makefile下就能編譯過. 也許是編譯器命令行不同. 以后有興趣再查.
現(xiàn)在先嘗試將代碼中的語法限定先改了, 能在MCUXpresso工程中編譯過再說.文章來源地址http://www.zghlxwxcb.cn/news/detail-444191.html
END
到了這里,關(guān)于call to non-‘constexpr‘ function的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!