【问题标题】:MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartupMSVCRTD.lib(crtexe.obj):错误 LNK2019:函数 ___tmainCRTStartup 中引用的未解析的外部符号 _main
【发布时间】:2015-02-06 19:52:11
【问题描述】:

以下是我教科书中尝试创建顺序访问文件的示例。但是,当我尝试在 VS2008 中编译时,我继续收到此错误:

MSVCRTD.lib(crtexe.obj):错误 LNK2019:函数 ___tmainCRTStartup 中引用的未解析的外部符号 _main。

有什么想法吗?

提前致谢!

#include <stdio.h>

int main( void )
{ 
   unsigned int account; // account number
   char name[ 30 ]; // account name
   double balance; // account balance

   FILE *cfPtr; // cfPtr = clients.dat file pointer   

   // fopen opens file. Exit program if unable to create file 
   if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) {
      puts( "File could not be opened" );
   } // end if
   else { 
      puts( "Enter the account, name, and balance." );
      puts( "Enter EOF to end input." );
      printf( "%s", "? " );
      scanf( "%d%29s%lf", &account, name, &balance );

      // write account, name and balance into file with fprintf
      while ( !feof( stdin ) ) { 
         fprintf( cfPtr, "%d %s %.2f\n", account, name, balance );
         printf( "%s", "? " );
         scanf( "%d%29s%lf", &account, name, &balance );
      } // end while

      fclose( cfPtr ); // fclose closes file   
   } // end else
} // end main

【问题讨论】:

  • 确保 1) #include &lt;windows.h&gt;(其中有一个用于“main()”的宏),以及 2) 将您的 makefile 或 MSVS 项目选项设置为“console app”(而不是“windows app” )。
  • 我应该澄清这是 C,而不是 C++
  • @ChrisDillon 所以正确地标记它 - 但是在这种情况下它并不重要
  • 在这种情况下,两种语言都完全相同。
  • 这也适用于 VS2008。子系统设置为控制台。

标签: c lnk2019


【解决方案1】:

好的——你的程序应该编译和链接没有错误,你不必做任何特别的事情。换句话说,您不必“#include windows.h”(尽管它不会造成伤害)并且您不必显式使用“tmain()”(即使那是实际的条目,在 MSVC 后面宏)。

它应该“正常工作”。

建议:

1) 确认您可以编译、链接和执行ANY C 程序。例如:

a) 输入此程序: notepad tmp.c

#include <stdio.h>

int main() {
  printf ("Hello world!\n");
  return 0;
}

b) 编译:从 MSVS IDE 或命令行(您可以使用 MSVS .bat 文件“vcvars32.bat”来设置命令提示符环境:

d:\temp>cl tmp.c
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.50727.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

tmp.c
Microsoft (R) Incremental Linker Version 11.00.50727.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:tmp.exe
tmp.obj

c:执行你的程序:D:\temp&gt;tmp 世界你好!

2) 我有一个不同的更新版本的 MSVS ...但它应该绝对适用于您的 MSVS 2008。

3) 如果您的“hello world”(新 MSVS 项目和/或命令行构建)失败并出现相同的链接错误,您可能需要考虑重新安装 MSVS。如果可能,请考虑获取更新的版本(例如 Visual Studio 2013 Community):

http://www.visualstudio.com/

MSVS 2012 Express

4) 如果您的“hello world”有效(并且应该),请考虑从头开始创建一个新项目,然后将您的文件复制到新项目中。

这个完整的例子对我来说编译和运行正常:

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

#define MAX_NAME 30
#define MAX_LINE 80
#define CLIENTS_FILE "clients.dat"

int main () { 
   char line[MAX_LINE], acct_name[MAX_NAME];
   unsigned int acct_no;
   double acct_balance;
   int iret;

   /* Open file.  Print error and return non-zero status if error */
   FILE *fp = fopen(CLIENTS_FILE, "w" );
   if (!fp) {
      perror ("clients file open error");
      return 1;
   } 

   do {
     /* Get next record */
     printf ("Enter the account, name, and balance. \"q\" to exit.\n");
     fgets (line, MAX_LINE, stdin);

     /* Check for end of data */
     if (line[0] == 'q')
       break;

     /* Parse data */
     if ((iret = sscanf(line, "%d %s %lf", &acct_no, acct_name, &acct_balance )) == 3) {
       /* Write to file */
       fprintf(fp, "%d %s %lf\n", acct_no, acct_name, acct_balance );
     } else {
       /* Print warning */
       printf ("Error: unable to parse input: #/items parsed = %d, line = %s", iret, line);
     }

    } while (line[0] != 'q');

    /* Close file and return "OK" status */
    fclose (fp);
    printf ("Done.\n");
    return 0;
}

【讨论】:

    【解决方案2】:

    我正在使用 Visual Studio 2017 并收到该错误。对我来说,解决方案是:

    主菜单 -> 项目 -> 项目名称 - 属性。

    然后:

    链接器->系统和右侧的“子系统”我选择了“Windows”。之后我的代码编译得非常完美。

    【讨论】:

      【解决方案3】:

      双重签入 vs 项目源文件包含包含项目主要功能的源文件。对我来说,我的 cmake 文件中的一个错误导致生成的 vs 项目文件丢失了我的源文件,从而导致了这个错误。

      【讨论】:

        猜你喜欢
        • 2011-06-18
        • 1970-01-01
        • 2012-06-30
        • 2014-02-03
        • 2014-08-28
        • 2014-09-23
        • 1970-01-01
        • 2014-02-10
        • 2021-04-18
        相关资源
        最近更新 更多