I just wrote my first C++ app today and it was the infamous Hello World Tutorial. Can someone help me write an app that will pop up a messagebox on load and that's it? Thanks, Jeremy
Printable View
I just wrote my first C++ app today and it was the infamous Hello World Tutorial. Can someone help me write an app that will pop up a messagebox on load and that's it? Thanks, Jeremy
What compiler are you using?
it's essentially the same as VB
Code:void CForm1::Form_Load()
{
MessageBox("Hello World!", "Sample", MC_ICONEXCLAMATION");
}
or
Code:#include <windows.h>
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,
LPSTR lpCmdLine,int iCmdShow)
{
MessageBox(NULL,"Hello World","Hello", MB_OK);
return 0;
}
john,
I tried your example and I get this:
Got any ideas? Thanks, JeremyCode:D:\Jeremy\DotNET\C++>cl Alert.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
Alert.cpp
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:Alert.exe
Alert.obj
Alert.obj : error LNK2001: unresolved external symbol __imp__MessageBoxA@16
Alert.exe : fatal error LNK1120: 1 unresolved externals
D:\Jeremy\DotNET\C++>
i think that you need to tell the compiler that you need to lik the other librarys as well becuase its a Win32 application, try compiling with
Quote:
CL Filename /link kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
Thanks john...it worked. Can you explain why I had to do that? Also...which one of those libraries is the necessary one to include the MessageBox link library? Thanks, Jeremy
The necessary one is user32.lib.
The reason is in the way C++ works.
First stage is compilation. In this step every .cpp file is translated to machine code, which is stored in a .obj file. In this phase the compiler doesn't care for external functions, all it needs to know is that they are there (thus the header files, which tell the compiler exactly that).
The second step is linking the .obj files together to a .exe or .dll. In this step all the references to external functions get resolved. The linker searches in all .obj files it gets as input as well as additional libraries, the .lib files. There are two types of .lib files. The traditional libraries, which contain all the code of the function. And the import libraries, which only contain a reference to a dll that really contains the program code. MessageBox is a function of the windows API which resides in user32.dll. The import library is user32.lib. This is why you need to tell the linker to use user32.lib when linking.
kernel32.lib Windows Core. Files, threads, processes. CreateFile, CreateThread, ExitProcess.
user32.lib User interface. Windowing stuff and resources. CreateWindow, GetMessage, MessageBox, LoadIcon.
gdi32.lib Graphics. The whole GDI is in this. GetDC, BeginPaint, Rectangle, SetPixel.
winspool.lib Printing.
comdlg32.lib Common dialogs. The dialogs that look the same in all apps are in here. GetOpenFileName.
advapi32.lib Advanced stuff. Registry handling. RegCreateKey.
shell32.lib Windows Shell interface. Explorer-integration, shell namespace directory structure. ShellExecute, Shell_NotifyIcon, IShellFolder.
ole32.lib Basic COM and OLE. API for COM essentials and object linking and embedding. CoInitialize, CoCreateObject.
oleaut32.lib OLE Automation (ActiveX). Supporting API for IDispatch implementations.
uuid.lib More COM stuff.
odbc32.lib ODBC. Database API.
odbccp32.lib Database related. Don't know about the details.
Thanks alot CornedBee. Where can I read up on this a bit more? Thanks, Jeremy
I have no idea. I scraped it all together from books and many online places.
Can you build a form with C++? I imagine so but I can't find any places on how to do it. If so...could you provide a sample script? Thanks, Jeremy
P.S. - You've been a great help to me.
As I said elsewhere: C++ itself is not graphical as VB is. Thus there is no C++-way of building forms.
However Windows lets you do that. You can use dialog resources to create forms, and VC++ let's you design the dialogs graphically.
Okay...let's say that I wanted to build a C++ application, what good would it do me if I couldn't have forms in it? Laters, Jeremy
First, you can have forms. But it isn't built into the language. Rather you have to use libraries, like the WinAPI, MFC, WTL, OWL, GTK+, Qt, to name just the most popular. Some are for Windows, some for X11 (Linux etc.), some for both.
But the designing of the forms is different.
That makes alot more sense. Okay...let's say that I wanted to build a Windows form. What do I have to install/download/reference to begin building forms? Also, as far as the linux stuff goes, I imagine I could get the info on the respective websites for the different Windows Managers like KDE, GNOME and such right? Thanks alot for your help, Jeremy
First, the idea of "Forms" was created together with Visual Basic. A normal application has windows, which are more generic (a form is a window composed of controls, which fire events which the form catches). To create a form you create a window, then create the controls and place them on the window. Usually in code.
In Windows (and probably other systems too) there are also resources. A special resource is the dialog template. A dialog template is basically a form. Dialog designers like the one in Visual C++ allow you to drop controls on the dialog. However, you must still write the code that catches the events (called messages and notifications) yourself, unless you use a library that is in some way supported by your designed (e.g. MFC with VC++).
If you don't have a designer you can still write the template as text (resources are only text too) or write code to create the window and controls directly.
So, given that you start with nothing, you need to buy/download a compiler first. VC++ is very good (especially VC++.Net 2003), but another great (free!) one is gcc, which is basically the standard UNIX compiler, but which has been ported to Windows. Cygwin and MingW provide Windows versions of gcc.
Then you need a code editor. VC++ is an IDE, so it contains the code editor too. Dev-C++ is a free IDE which uses the MingW gcc compiler. Other great editors are EditPlus2, TextPad and various other syntax highlighting editors (try your way throught the editors category on sourceforge ;)).
That's it. Install whatever you got and start coding :)