简体   繁体   中英

MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

The following is a sample from my textbook attempting to create a sequential access file. However I continue to receive this error when I try to compilein VS2008:

MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup.

Any ideas?

Thanks in advance!

#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

OK - your program should compile and link without the error, without you having to do anything special. In other words, you shouldn't have to "#include windows.h" (although it couldn't hurt) and you shouldn't have to explicitly use "tmain()" (even though that's the actual entry, behind the MSVC macros).

It should "just work".

SUGGESTIONS:

1) Verify that you can compile, link and execute ANY C program. For example:

a) Type in this program: notepad tmp.c

#include <stdio.h>

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

b) Compile: from the MSVS IDE, or from the command line (you can use the MSVS .bat file "vcvars32.bat" to set your command prompt environment:

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: Execute your program: D:\\temp>tmp Hello world!

2) I have a different, newer version of MSVS ... but it should absolutely work the same for your MSVS 2008.

3) If your "hello world" (new MSVS project and/or command line build) fails with the same link error, you might want to consider re-installing MSVS. Consider getting a newer version (eg Visual Studio 2013 Community), if at all possible:

http://www.visualstudio.com/

MSVS 2012 Express

4) If your "hello world" works (and it should), consider creating a new project from scratch, then copying your files into the new project.

This complete example compiles and runs OK for me:

#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;
}

I was working with Visual Studio 2017 and received that error. For me the solution was:

Main Menu -> Project -> Projectname - properties.

Then:

Linker->System and on right side for "SubSystem" I chose "Windows". My code got compiled perfectly afterwards.

Double check in vs project the source files contain the source file containing your main function from the project. For me, a bug in my cmake file lead to the generation of vs project files missing my source files which resulted in this error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM