在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
最近接pb.c 这段代码 static int struct_unpack(lua_State *L) { uint8_t format = luaL_checkinteger(L, 1); size_t len; const uint8_t* buffer = (uint8_t*)luaL_checklstring(L, 2, &len); size_t pos = luaL_checkinteger(L, 3); buffer += pos; uint8_t out[8]; switch(format){ case 'i': { lua_pushinteger(L, *(int32_t*)unpack_fixed32(buffer, out)); break; } case 'q': { lua_pushnumber(L, (lua_Number)*(int64_t*)unpack_fixed64(buffer, out)); break; } case 'f': { lua_pushnumber(L, (lua_Number)*(float*)unpack_fixed32(buffer, out)); break; } case 'd': { lua_pushnumber(L, (lua_Number)*(double*)unpack_fixed64(buffer, out)); break; } case 'I': { lua_pushnumber(L, *(uint32_t*)unpack_fixed32(buffer, out)); break; } case 'Q': { lua_pushnumber(L, (lua_Number)*(uint64_t*)unpack_fixed64(buffer, out)); break; } default: luaL_error(L, "Unknown, format"); } return 1; }
uint8_t out[8]; 这行一直接报错误 错误 error C2275: “uint8_t”: 将此类型用作表达式非法 error C2146: 语法错误: 缺少“;”(在标识符“out”的前面) 按c的规范需要将变量申明放在代码开头部份。 把uint8_t out[8];移到代码段首行,问题解决。代码如下: static int struct_unpack(lua_State *L) { uint8_t out[8]; uint8_t format = luaL_checkinteger(L, 1); size_t len; const uint8_t* buffer = (uint8_t*)luaL_checklstring(L, 2, &len); size_t pos = luaL_checkinteger(L, 3); buffer += pos; switch(format){ case 'i': { lua_pushinteger(L, *(int32_t*)unpack_fixed32(buffer, out)); break; } case 'q': { lua_pushnumber(L, (lua_Number)*(int64_t*)unpack_fixed64(buffer, out)); break; } case 'f': { lua_pushnumber(L, (lua_Number)*(float*)unpack_fixed32(buffer, out)); break; } case 'd': { lua_pushnumber(L, (lua_Number)*(double*)unpack_fixed64(buffer, out)); break; } case 'I': { lua_pushnumber(L, *(uint32_t*)unpack_fixed32(buffer, out)); break; } case 'Q': { lua_pushnumber(L, (lua_Number)*(uint64_t*)unpack_fixed64(buffer, out)); break; } default: luaL_error(L, "Unknown, format"); } return 1; }
另外 windows下面vs编译环境提示缺少 #include <endian.h> 可加入如下代码 #ifdef WIN32 |
请发表评论