Results 1 to 17 of 17

Thread: C++ Newbie

  1. #1

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677

    C++ Newbie

    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
    He who listens well, speaks well.

  2. #2
    Lively Member
    Join Date
    Aug 2002
    Posts
    78
    What compiler are you using?
    - Your Local Drunk
    AIM: Asharlin
    YIM: Asharlin
    MSN: [email protected]
    ICQ: 177568857

  3. #3
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    it's essentially the same as VB

    Code:
    void CForm1::Form_Load()
    {
        MessageBox("Hello World!", "Sample", MC_ICONEXCLAMATION");
    }
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  4. #4
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    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;
    }

  5. #5

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    john,
    I tried your example and I get this:

    Code:
    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++>
    Got any ideas? Thanks, Jeremy
    He who listens well, speaks well.

  6. #6
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    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

    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

  7. #7

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    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
    He who listens well, speaks well.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Thanks alot CornedBee. Where can I read up on this a bit more? Thanks, Jeremy
    He who listens well, speaks well.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I have no idea. I scraped it all together from books and many online places.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    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.
    He who listens well, speaks well.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    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
    He who listens well, speaks well.

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    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
    He who listens well, speaks well.

  17. #17
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width