Met problems with my first win32 program
-
code: // Windows Programming Tutorial Series #include "stdafx.h" #include int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox (NULL, "Hello World! This is my first WIN32 program", "Lesson 1", MB_OK); return 0; } 2 errors: Linking... LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main Debug/a.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. a.exe - 2 error(s), 0 warning(s) Who can make some suggestions please? I guess it may be I did not include the lib well.
-
code: // Windows Programming Tutorial Series #include "stdafx.h" #include int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox (NULL, "Hello World! This is my first WIN32 program", "Lesson 1", MB_OK); return 0; } 2 errors: Linking... LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main Debug/a.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. a.exe - 2 error(s), 0 warning(s) Who can make some suggestions please? I guess it may be I did not include the lib well.
The linker thinks this is a console program whose entry point is the main function. You need to tell it that it is a windows program. In the compiler options, preprocess definitions section you will probably see a macro "_CONSOLE" defined. You need to remove that and add in the macro _WINDOWS there.
-
The linker thinks this is a console program whose entry point is the main function. You need to tell it that it is a windows program. In the compiler options, preprocess definitions section you will probably see a macro "_CONSOLE" defined. You need to remove that and add in the macro _WINDOWS there.
when I include this C++ source file into a "Win32 Application" project, it works; when include it into a "Win32 Console Application" project, it fails; my question is how to change the settings of the latter to make it work, without creating a new project of Wind32 Application. I tried include the macro _WINDOWS in the setting->c/c++->preprocess definitions , but it did not work.
-
when I include this C++ source file into a "Win32 Application" project, it works; when include it into a "Win32 Console Application" project, it fails; my question is how to change the settings of the latter to make it work, without creating a new project of Wind32 Application. I tried include the macro _WINDOWS in the setting->c/c++->preprocess definitions , but it did not work.
There is obviously more to it than just the macro and I'm not certain what they are exactly. What I would do is rename the folder your project is in to Lesson1_Old or something like that, create a new project, and then copy the old code files that you changed into the new project's directory (do NOT copy any project files - only .cpp, h, or .rc files) That should be very easy and take only a minute or two. Sorry I couldn't be more helpful. BTW - it could be informative to compare the new project files generated with the old ones that do not work.
-
There is obviously more to it than just the macro and I'm not certain what they are exactly. What I would do is rename the folder your project is in to Lesson1_Old or something like that, create a new project, and then copy the old code files that you changed into the new project's directory (do NOT copy any project files - only .cpp, h, or .rc files) That should be very easy and take only a minute or two. Sorry I couldn't be more helpful. BTW - it could be informative to compare the new project files generated with the old ones that do not work.
I'm just a beginner my self, and in the process of writing my first application. So this might be a shot in the dark, so to speak... But are you trying to display a msgbox in a console application? I was under the impression that that would be more or less impossible (unless you program the actual code for displaying such a thing). If you intend to write something in a console application, shouldn't you just print it to the console? Console::WriteLine(L"Hello World!"); If you want to use a msgbox, I think you either have to make a Win32 Application or a MFC application? This is because in an empty Console Application you don't have window handling etc... Someone please correct me if I'm wrong? TylerD75
-
I'm just a beginner my self, and in the process of writing my first application. So this might be a shot in the dark, so to speak... But are you trying to display a msgbox in a console application? I was under the impression that that would be more or less impossible (unless you program the actual code for displaying such a thing). If you intend to write something in a console application, shouldn't you just print it to the console? Console::WriteLine(L"Hello World!"); If you want to use a msgbox, I think you either have to make a Win32 Application or a MFC application? This is because in an empty Console Application you don't have window handling etc... Someone please correct me if I'm wrong? TylerD75
-
when I include this C++ source file into a "Win32 Application" project, it works; when include it into a "Win32 Console Application" project, it fails; my question is how to change the settings of the latter to make it work, without creating a new project of Wind32 Application. I tried include the macro _WINDOWS in the setting->c/c++->preprocess definitions , but it did not work.
Go to your project's properties. Configuration Properties > Linker > System > SubSystem Change from Console to Windows.
-
no, the original poster is trying make a windows app but the linker thinks he wants a console app.
If that is the case, how come he writes this? Quote:
when I include this C++ source file into a "Win32 Application" project, it works; when include it into a "Win32 Console Application" project, it fails; my question is how to change the settings of the latter to make it work, without creating a new project of Wind32 Application. I tried include the macro _WINDOWS in the setting->c/c++->preprocess definitions , but it did not work.
From this I think he's trying to convert a console application into a Win32 Application, without adding the window handling that is automatically added when creating a new Win32 Application Project. This seem a bit strange to me, but as I stated before: I'm a newbee! Cheers, TylerD75 -
when I include this C++ source file into a "Win32 Application" project, it works; when include it into a "Win32 Console Application" project, it fails; my question is how to change the settings of the latter to make it work, without creating a new project of Wind32 Application. I tried include the macro _WINDOWS in the setting->c/c++->preprocess definitions , but it did not work.
The entry point of a console app is
main()
notWinMain()
. Change it tomain()
(with the right prototype, of course).--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
code: // Windows Programming Tutorial Series #include "stdafx.h" #include int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox (NULL, "Hello World! This is my first WIN32 program", "Lesson 1", MB_OK); return 0; } 2 errors: Linking... LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main Debug/a.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. a.exe - 2 error(s), 0 warning(s) Who can make some suggestions please? I guess it may be I did not include the lib well.
if you wanna program for win32 application you must drive to functions itself. 1)
WinMain
2)WinPorc
try this code :(thanks for Ivor Horton Books. you can download source code & see chapter 7 )int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS WindowClass; // Structure to hold our window's attributes static char szAppName[] = "OFWin"; // Define window class name HWND hWnd; // Window handle MSG msg; // Windows message structure // Redraw the window if the size changes WindowClass.style = CS_HREDRAW | CS_VREDRAW; // Define our procedure for message handling WindowClass.lpfnWndProc = WindowProc; WindowClass.cbClsExtra = 0; // No extra bytes after the window class WindowClass.cbWndExtra = 0; // structure or the window instance WindowClass.hInstance = hInstance; // Application instance handle // Set default application icon WindowClass.hIcon = LoadIcon(0, IDI_APPLICATION); // Set window cursor to be the standard arrow WindowClass.hCursor = LoadCursor(0, IDC_ARROW); // Set gray brush for background color WindowClass.hbrBackground = static_cast(GetStockObject(GRAY_BRUSH)); WindowClass.lpszMenuName = 0; // No menu, so no menu resource name WindowClass.lpszClassName = szAppName; // Set class name // Now register our window class RegisterClass(&WindowClass); // Now we can create the window hWnd = CreateWindow( szAppName, // the window class name "A Basic Window the Hard Way", // The window title WS_OVERLAPPEDWINDOW, // Window style as overlapped CW_USEDEFAULT, // Default screen position of upper left CW_USEDEFAULT, // corner of our window as x,y... CW_USEDEFAULT, // Default window size CW_USEDEFAULT, // .... 0, // No parent window 0, // No menu hInstance, // Program Instance handle 0 // No window creation data ); ShowWindow(hWnd, nCmdShow); // Display the window UpdateWindow(hWnd); // Cause window client area to be drawn // The message loop while(GetMessage(&msg, 0, 0, 0) == TRUE)