Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   New c++ programmer and i mean new! (http://www.go4expert.com/forums/cpp-programmer-i-mean-t14458/)

TriG0rZ 9Oct2008 17:37

New c++ programmer and i mean new!
 
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!!

shabbir 9Oct2008 17:58

Re: New c++ programmer and i mean new!
 
Visual Studio is a better option in my opinion

TriG0rZ 9Oct2008 20:14

Re: New c++ programmer and i mean new!
 
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

xpi0t0s 10Oct2008 13:49

Re: New c++ programmer and i mean new!
 
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

TriG0rZ 10Oct2008 15:19

Re: New c++ programmer and i mean new!
 
awesome thaat has really helped and has fixed my problem, thank you so very much!!!

shabbir 10Oct2008 15:25

Re: New c++ programmer and i mean new!
 
If that has help add to the reputation using the http://www.go4expert.com/images/buttons/reputation.gif icon beside the post


All times are GMT +5.5. The time now is 04:08.