【问题标题】:C Warning: No return statement in function returning non-voidC警告:函数中没有返回语句返回非void
【发布时间】:2019-01-10 03:23:21
【问题描述】:

我正在尝试修改 open-vm-tools,它可以在 Linux 中运行。 但是,当我使用 Nmake 时,我会收到“不返回”错误。

函数中没有返回语句返回非 void [-Werror=return-type]

似乎我在这个函数中丢失了一个返回词。但是函数底部有返回字。为了解决这个问题,我在第 17 行添加了一个'return 0'。它可以工作,但是'return 0'之后的以下代码将不会被执行。那不是我想要的。这是函数:

static int
ToolsCoreRunLoop(ToolsServiceState *state)
{

    if (!ToolsCore_InitRpc(state)) {
      return 1;
    }

    if (state->ctx.rpc && !RpcChannel_Start(state->ctx.rpc)) {
      return 1;
    }

    if (state->ctx.rpc) {
      ToolsCoreReportVersionData(state);
    }

    //return 0; Here is what I add

    #if 0
    if (!ToolsCore_LoadPlugins(state)) {
      return 1;
    }
    #if defined(__linux__)
    if (state->mainService) {
      ToolsCore_InitVsockFamily(state);
    }  
    #endif
    if (state->ctx.errorCode == 0 &&
       (state->ctx.isVMware ||
        ToolsCore_GetTcloName(state) == NULL ||
        state->debugPlugin != NULL)) {
        ToolsCore_RegisterPlugins(state);

        if (g_signal_lookup(TOOLS_CORE_SIG_IO_FREEZE,
                      G_OBJECT_TYPE(state->ctx.serviceObj)) != 0) {
          g_signal_connect(state->ctx.serviceObj,
                      TOOLS_CORE_SIG_IO_FREEZE,
                      G_CALLBACK(ToolsCoreIOFreezeCb),
                      state);
        }

        state->configCheckTask = g_timeout_add(CONF_POLL_TIME * 1000,
                                         ToolsCoreConfFileCb,
                                         state);

        #if defined(__APPLE__)
            ToolsCore_CFRunLoop(state);
        #else
            g_main_loop_run(state->ctx.mainLoop);
        #endif
        }
        ToolsCoreCleanup(state);
        return state->ctx.errorCode;
        #endif
  }

int
ToolsCore_Run(ToolsServiceState *state)
{
    return ToolsCoreRunLoop(state);
}

有人可以给我一些建议吗?谢谢。

【问题讨论】:

  • #endif} 之间缺少返回。
  • Here is what I add 实际上是函数的最后一行,经过预处理(函数的其余行是 #if 0 ... #endif 块,转换为无代码)
  • 哦...根据您的评论,我想我需要删除#if 0。

标签: c


【解决方案1】:

#endif} 之间缺少返回。

但是函数底部有return word。

不,没有return

以下是#if 0的一大评论

   #if 0
    if (!ToolsCore_LoadPlugins(state)) {
      return 1;
    }
    #if defined(__linux__)
    if (state->mainService) {
      ToolsCore_InitVsockFamily(state);
    }  
    #endif
    if (state->ctx.errorCode == 0 &&
       (state->ctx.isVMware ||
        ToolsCore_GetTcloName(state) == NULL ||
        state->debugPlugin != NULL)) {
        ToolsCore_RegisterPlugins(state);

        if (g_signal_lookup(TOOLS_CORE_SIG_IO_FREEZE,
                      G_OBJECT_TYPE(state->ctx.serviceObj)) != 0) {
          g_signal_connect(state->ctx.serviceObj,
                      TOOLS_CORE_SIG_IO_FREEZE,
                      G_CALLBACK(ToolsCoreIOFreezeCb),
                      state);
        }

        state->configCheckTask = g_timeout_add(CONF_POLL_TIME * 1000,
                                         ToolsCoreConfFileCb,
                                         state);

        #if defined(__APPLE__)
            ToolsCore_CFRunLoop(state);
        #else
            g_main_loop_run(state->ctx.mainLoop);
        #endif
        }
        ToolsCoreCleanup(state);
        return state->ctx.errorCode;
        #endif

更好的格式会让事情更清晰。

static int ToolsCoreRunLoop(ToolsServiceState *state) {

  if (!ToolsCore_InitRpc(state)) {
    return 1;
  }

  if (state->ctx.rpc && !RpcChannel_Start(state->ctx.rpc)) {
    return 1;
  }

  if (state->ctx.rpc) {
    ToolsCoreReportVersionData(state);
  }

  //return 0; Here is what I add

#if 0
  if (!ToolsCore_LoadPlugins(state)) {
    return 1;
  }
#if defined(__linux__)
  if (state->mainService) {
    ToolsCore_InitVsockFamily(state);
  }
#endif
  if (state->ctx.errorCode == 0 &&
      (state->ctx.isVMware ||
          ToolsCore_GetTcloName(state) == NULL ||
          state->debugPlugin != NULL)) {
    ToolsCore_RegisterPlugins(state);

    if (g_signal_lookup(TOOLS_CORE_SIG_IO_FREEZE,
            G_OBJECT_TYPE(state->ctx.serviceObj)) != 0) {
      g_signal_connect(state->ctx.serviceObj,
          TOOLS_CORE_SIG_IO_FREEZE,
          G_CALLBACK(ToolsCoreIOFreezeCb),
          state);
    }

    state->configCheckTask = g_timeout_add(CONF_POLL_TIME * 1000,
        ToolsCoreConfFileCb,
        state);

#if defined(__APPLE__)
    ToolsCore_CFRunLoop(state);
#else
    g_main_loop_run(state->ctx.mainLoop);
#endif
  }
  ToolsCoreCleanup(state);
  return state->ctx.errorCode;
#endif

// Missing return

}

【讨论】:

  • 好的。我知道问题所在。 #if 0 不是我想要的。我应该删除它并再次检查代码,谢谢。
猜你喜欢
  • 2020-10-03
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-09
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多