Solved

1) error LNK2001: unresolved external symbol _main 2) Debug/first.exe : fatal error LNK1120: 1 unresolved externals

Posted on 2004-08-31
7
392 Views
Last Modified: 2008-02-01
Hi all, I am very new to VC++ ... I have a book in first chapter, I tried to compiled it and it went well. However, when I tried to build first.exe ... I got two errors .... I do not know how to solve it. I have save this program/project in my own directory("C:\MyFolder\C++\WindowsProg\First\"), not sure if that's effects building the .EXE ... Below are my program code and errors ... Kindly take a look and hope to hear from experts. Thank you very much for your time.

--------------------Program Code------------------------------------------
// first.cpp : Defines a window's appearing for the application.
//

#include "stdafx.h"
#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{

      WNDCLASS WndClass;
      WndClass.style = 0;
      WndClass.cbClsExtra = 0;
      WndClass.cbWndExtra = 0;
      WndClass.lpfnWndProc = WndProc;
      WndClass.hInstance = hInstance;
      WndClass.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
      WndClass.hCursor = 0;
      WndClass.hIcon = 0;
      WndClass.lpszMenuName = 0;
      WndClass.lpszClassName = "WinProg";

      RegisterClass(&WndClass);

      HWND hWindow;

      hWindow = CreateWindow("WinProg", "Window", WS_OVERLAPPEDWINDOW, 0,0, 400, 400, NULL, NULL, hInstance, NULL);


      ShowWindow (hWindow, nCmdShow);

      UpdateWindow (hWindow);

      MSG Message;

      while(GetMessage(&Message, NULL,0,0))
      {
            
            DispatchMessage(&Message);

      }

      return (Message.wParam);

}

// Function: LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

LRESULT CALLBACK WndProc (HWND hWnd, UINT uiMessage, WPARAM wParam, LPARAM lParam)
{
      
      switch(uiMessage)
      {
            
            case WM_DESTROY:
                  PostQuitMessage(0);
                  return 0;

            default:
                  return DefWindowProc (hWnd, uiMessage, wParam, lParam);

      }


}

-------------------------------------------------------------------------------




--------------------Configuration: first - Win32 Debug--------------------
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/first.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

first.exe - 2 error(s), 0 warning(s)

-------------------------------------------------------------------------------
0
Comment
Question by:hbcontract
  • 2
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11943851
Go to your project settings, choose the 'Link' tab and locate '/subsystem:console' in the text field at the bottom of that page. Change that to read '/subsystem:windows'. The error message indicates that your project settings are for a console app, whereas you are trying to create a GUI application.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11943878
You created a Win32 Console Application. That is supposed to have a main() function rather than a WinMain() function.

You need a Win32 Application (no console) for the prog above. Goto the 'Link' tab of your 'Project Settings' and edit the 'Project Options' field. Replace  /subsystem:console by /subsystem:windows and rebuild all.

Regards, Alex


 
0
 

Author Comment

by:hbcontract
ID: 11944151
How did you guys know I am using Win32 console application? I am trying to find what kind of application I was selecting, but not success ... how I can know what kind of application I was selecting? Coz' from what I know, I did selected Win32 Application ... not console ... unless I mis-select Win32 console application ... Thanks.
0
 
LVL 86

Accepted Solution

by:
jkr earned 40 total points
ID: 11944171
>> How did you guys know I am using Win32 console application?

Because of the error message :o)

'main()'  is needed for a console app, whereas 'WinMain()' is for GUI applications.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now