New c++ programmer and i mean new!

Discussion in 'C++' started by TriG0rZ, Oct 9, 2008.

  1. TriG0rZ

    TriG0rZ New Member

    Joined:
    Oct 2, 2008
    Messages:
    88
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    UK
    ok im trying to run like a hello wolrd program but for some reason i cannot, im using notepad++ and saving it as a .cpp file and a .exe and it doesnt seem to be working so i am just asking which program is best to write c++ in and what file do i save it as to run it and how do i run it ?? thanks for your help!!
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Visual Studio is a better option in my opinion
     
  3. TriG0rZ

    TriG0rZ New Member

    Joined:
    Oct 2, 2008
    Messages:
    88
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    UK
    but is their not a smaller one for on the go like notepd++ i have notePad++ but i dont know how to run a c++ program can any one tell me, i have a program but everything i go to run and choose it nothing happens, the file is .cpp and i have anotehr .exe

    Thanks
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Your first post suggests you're creating the .exe just by copying the source file. This isn't the way to create an executable. Download a hex editor (e.g. xvi32) and have a look at an executable. Be careful not to change anything, or if you do either don't save the file or make a backup of the original executable before you save it. See how it looks nothing at all like source code? (Keep xvi32 installed, hex editors are spectacularly useful to programmers.)

    This is because source code must be transformed into an executable. This is done in several steps. First, the C preprocessor preprocesses the files, mostly looking for comments and lines beginning with #. The preprocessor sorts out header files and simplifies the code for the compiler. The compiler usually invokes the preprocessor for you, and sometimes the preprocessing is done by the compiler itself, so normally you don't need to worry about this step. But it can occasionally be useful to invoke the preprocessor directly.

    Next the compiler takes the preprocessor output and compiles it into what is known as object code. This is like executable code but has not yet had all its references finalised -- meaning, if, say, your program calls printf(), any calls to this function have not yet been resolved.

    Next, the linker takes the object code, possibly from several compiled source code files, and the libraries, and links them together, resolving all references, and produces an executable.

    So let's have an example. Here's a header file, let's call it dave.h:
    Code:
    #define FOO 27
    void func(int x);
    
    The main program file, dave.cpp:
    Code:
    #include "dave'h"
    
    int main()
    {
      func(FOO);
      return 0;
    }
    
    And a second source file func.cpp:
    Code:
    #include "stdio.h"
    
    void func(int x)
    {
      printf("x=%d\n",x);
    }
    
    We can see all this in action with the above. I have a machine with MinGW installed; let's use that. MinGW is Minimalist GCC for Windows; there are other compilers, but this is a good free one.

    When learning, use the command line. IDE's are good, but they hide a lot, which is useful when you're up and running, building complex executables from multiple source files and libraries, but learning in an IDE is bad on two counts: (1) you don't get to see what's really going on (e.g. in Visual Studio you just hit F7 and there's the executable); (2) that ties you to a specific IDE and you have an unnecessarily steep learning curve when you encounter a new IDE. IDE=Integrated Development Environment.

    So let's say the above files are in c:\dev, and MinGW is installed in c:\MinGW, and you've opened a command prompt in c:\dev.
    Compile dave.cpp with the command: \mingw\bin\c++ -c dave.cpp -o dave.o
    Compile func.cpp with the command: \mingw\bin\c++ -c func.cpp -o func.o
    Link the two files with: \mingw\bin\c++ dave.o func.o -o dave.exe

    All being well there won't be any errors (I've worked through this as I've written it so there shouldn't be) and you'll now have dave.exe. Run it just by typing its name (dave), and you should get the output:
    x=27

    Now you've done all that, here's a shorter version of the same. Delete dave.exe, dave.o and func.o, and build the whole lot in one step with:
    \mingw\bin\c++ dave.cpp func.cpp -o dave.exe
     
  5. TriG0rZ

    TriG0rZ New Member

    Joined:
    Oct 2, 2008
    Messages:
    88
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    UK
    awesome thaat has really helped and has fixed my problem, thank you so very much!!!
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    If that has help add to the reputation using the [​IMG] icon beside the post
     
    Last edited: Jan 21, 2017

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice