【问题标题】:C external errors and 1 warningC 外部错误和 1 个警告
【发布时间】:2022-01-21 23:23:14
【问题描述】:

我在尝试编译时遇到这些错误,唯一加下划线的是 strcpy_s(name, strlen(input)+1, input);这是警告。这是作业的一部分,部分说明是制作 Topic.c(下面的代码)和 Topic.h,但没有说明在 .h 文件中放入什么内容。我以前从未见过这些错误并对其进行了研究,但我找不到与此问题相关的参考资料。这不是一个修复代码分配,它应该可以运行我只应该理解这些概念。不知道要问什么问题。我检查了几次以确保代码输入正确。

函数“int __cdecl invoke_main(void)”(?invoke_main@@YAHXZ) 中引用的 LNK2019 未解析的外部符号 main

LNK1120 1 个未解决的外部问题

和 1 个警告

C6387 'name' 可能是 '0':这不符合函数 'strcpy_s' 的规范。

   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>

   /*
   * Swap 1 method with scalars: why does this not work?
   */
   void swap1(int x, int y)
   {
       int temp = x;
       x = y;
       y = temp;
   }

   /*
   * swap 2 method with pointers: why does this work?
   */
   void swap2(int* x, int* y)
   {
       int temp = *x;
       *x = *y;
       *y = temp;
   }

   /*
   * Play Pointer method: Don't forget to clean up your malloc!
   */
   char* playPointer(char* input)
   {
       // Allocate memory to hold a copy of the input array, copy input to it, and 
          return a pointer to a new array
       // What is the advantage of assigning address of a variable to a pointer 
          variable?
       // Why does swap1() not work, and why does swap2() work?
       // How does strpy_s() make code more secure?
       // How does strpy_s() demonstrate defensive coding?
       // Look up strncpy() and compare it to strpy_s() How are they similar?

       char* name = malloc(strlen(input)+1);
       strcpy_s(name, strlen(input)+1, input);
       return name;
   }

【问题讨论】:

  • 您缺少main() 函数。如果这不是主程序,则在编译时使用-c 选项只是创建一个.o 文件,而不是尝试链接它。
  • 警告是因为你没有检查malloc()是否成功。
  • 为什么代码前后都有'''?如果你想标记一个代码块,它应该是 3 个反引号,而不是引号,它们应该在代码之前的行上。但既然你已经缩进了代码,你就不需要那个标记了。
  • 不确定如何检查 malloc 是否成功。尝试添加 main 我需要 int main() 吗?感谢您提交代码的提示
  • if (name != NULL)

标签: c warnings symbols


【解决方案1】:

您的代码中没有main 函数。

C 语言中的main 函数是程序入口点,它必须存在才能运行编译的程序。

在您的情况下,链接器找不到它并报告错误。

【讨论】:

  • 我尝试在整个代码的 {} 之外添加 int main(void) 但仍然得到相同的错误。
【解决方案2】:

如果这应该是一个独立的程序,而不是链接到其他程序的东西,它需要一个main() 函数。

要抑制有关name 的警告,请将调用strcpy_s() 的代码包装在一个检查malloc() 是否成功的if 语句中。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
 * Swap 1 method with scalars: why does this not work?
 */
void swap1(int x, int y)
{
    int temp = x;
    x = y;
    y = temp;
}

/*
 * swap 2 method with pointers: why does this work?
 */
void swap2(int* x, int* y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}

/*
 * Play Pointer method: Don't forget to clean up your malloc!
 */
char* playPointer(char* input)
{
    // Allocate memory to hold a copy of the input array, copy input to it, and return a pointer to a new array
    // What is the advantage of assigning address of a variable to a pointer variable?
    // Why does swap1() not work, and why does swap2() work?
    // How does strpy_s() make code more secure?
    // How does strpy_s() demonstrate defensive coding?
    // Look up strncpy() and compare it to strpy_s() How are they similar?

    char* name = malloc(strlen(input)+1);
    if (name != NULL) {
        strcpy_s(name, strlen(input)+1, input);
    }
    return name;
}

int main(void) {
    return 0;
}

【讨论】:

  • 我试试谢谢
  • 它生成了一个只有代码 0 的输出,但关于名称可能为 0 的警告仍然存在。C6387 'name' 可能为 '0':这不符合函数 'strcpy_s 的规范'。我应该添加删除名称吗? ?我在某处读到过,如果是这样,我该如何定义删除?
  • 你期望什么输出?该程序不执行任何操作。
  • lol 代码 0 应该足够好只是担心名称警告仍然存在谢谢
  • stackoverflow.com/questions/69146879/… 编译器不够聪明,无法检测到检查是否可以防止该问题。
猜你喜欢
  • 1970-01-01
  • 2023-02-05
  • 1970-01-01
  • 2020-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-16
  • 1970-01-01
相关资源
最近更新 更多