Staredit Network > Forums > Technology & Computers > Topic: Help me get started with Visual C++
Help me get started with Visual C++
Nov 26 2011, 1:39 pm
By: JaFF  

Nov 26 2011, 1:39 pm JaFF Post #1



Quote
#include <iostream>
using namespace std ;

int main(){
cout << "Hello, World!" ;
return 0 ;
}
That's my code. It compiles. It fails to build a project with the error:
Quote
1>------ Build started: Project: test1, Configuration: Debug Win32 ------
1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup
1>h:\my documents\visual studio 2010\Projects\test1\Debug\test1.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I'm using MS Visual Studio 2010 C++ and I've created a C++ project for a Win32 application. Any help would be greatly appreciated. :)

EDIT:
I went off to try just a general purpose C++ project (yet another option among many others), fiddled around with that, went back to the original I've just stated in my post, pressed "build", and it worked perfectly. I didn't change the code or any option settings for MS Visual Studio. What's going on?!

EDIT2:
I don't understand the order in which you must perform compile and build. The way I see it intuitively is that "compile" just runs the compiler that turns your source code into machine code and creates an .exe file. But what does build do? I don't care for anything else except the .exe file, so why should I even bother? So far, I've been doing it the following way: clean project, compile, build. What I've noticed that unless I clear the project, it doesn't get my latest code. And I often have to repeat the clean/compile/build process twice to get it working on the latest version of the code.

Post has been edited 2 time(s), last time on Nov 26 2011, 4:08 pm by JaFF.



None.

Nov 26 2011, 11:36 pm O)FaRTy1billion[MM] Post #2

👻 👾 👽 💪

I'm not familiar with VS, but I think compile makes the machine code and build takes the compiled files and links them all together into an exe. Usually though all 3 steps (clean compile build) are in one "rebuild all" button.



TinyMap2 - Latest in map compression! ( 7/09/14 - New build! )
EUD Action Enabler - Lightweight EUD/EPD support! (ChaosLauncher/MPQDraft support!)
EUDDB - topic - Help out by adding your EUDs! Or Submit reference files in the References tab!
MapSketch - New image->map generator!
EUDTrig - topic - Quickly and easily convert offsets to EUDs! (extended players supported)
SC2 Map Texture Mask Importer/Exporter - Edit texture placement in an image editor!
\:farty\: This page has been viewed [img]http://farty1billion.dyndns.org/Clicky.php?img.gif[/img] times!

Nov 26 2011, 11:52 pm JaFF Post #3



Yea, I've seen that button there. Thanks, that should save me some clicking time. :)



None.

Nov 27 2011, 5:46 pm CecilSunkure Post #4



You need to compile, link, then build. Did the button Farty showed you get it working?

The error you had was with the linker. Probably didn't link in iostream.h yet when you tried to build.



None.

Nov 27 2011, 7:18 pm JaFF Post #5



I only have Visual Studio at my uni computer, not at home (my computer is too screwed up to install it) so I haven't yet tried it. I will as soon as I get the chance.

I didn't know you have to link things before building. What does linking do? I'll look for a "link" button in the editor. What's the difference between iostream and iostream.h?



None.

Nov 27 2011, 10:28 pm Vrael Post #6



Linking is the process which takes library files like iostream and makes them useful for the program, otherwise things like "cout" are meaningless.

When they standardized C++ they changed some header files from say #include math.h to #include <math> or in your case, #include iostream.h to #include <iostream>. Really there's no difference, unless your library includes math.h but not math, in which case your linker wouldn't know what to link to if you just wrote "math".



None.

Nov 28 2011, 12:56 am ShadowFlare Post #7



The compile command only compiles the current file. Build will compile all the source files and link them into the target file type (exe, dll, etc.). Make sure your program isn't still running and that you aren't still debugging when you try to build. Normally you shouldn't need to clean each time you make a change.



None.

Nov 28 2011, 5:37 pm CecilSunkure Post #8



Preprocessing is the process of including files from one file to another, sort of like copy/pasting an entire file on top of yours which is what happens when the preprocessor sees include iostream.h, it takes that .h file and includes it into yours. Linking is sort of like a syntax check that compiling does, except it doesn't go over your code's syntax, it instead makes sure that all your function calls are real functions, that all your function prototypes have a definition, etc.



None.

Nov 28 2011, 5:56 pm JaFF Post #9



Thanks for all the replies guys, this stuff is making good sense so far. I'll Try it out tomorrow and apply theory to practice, we'll see how it goes. :)



None.

Nov 28 2011, 7:12 pm shmeeps Post #10



Quote from JaFF
What's the difference between iostream and iostream.h?
Here's a nice article about iostream vs iostream.h. As Vrael stated, most compilers contain two versions of this include, a new version, and a legacy (old) version. Essentially, iostream (or any relevant include that is extensionless) is a directive that loads the newest version of the include, and should load the older version only if necessary (although this behavior is very dependent on the specific compiler, many require you include both if using both). Using iostream.h will skip the new version and only load the legacy file. Except in specific instances, you want to use <iostream> (or any extensionless include) instead of <iostream.h>, since iostream will always remain up to date, should be the most stable, and many compilers may not include the legacy versions of standard includes. This is true for other standard C++ libraries as well, though there are many that don't have legacy counterparts.



None.

Nov 29 2011, 2:32 pm JaFF Post #11



I was clicking "Rebuild [project name]" which took two attempts, but I should've been clicking "Rebuild Solution" which is the propper function that gets things done. :) Problem solved. Thanks guys. :) If I have any more trouble I'll probably just revive this thread.



None.

Dec 3 2011, 3:51 pm JaFF Post #12



Quote
#include <iostream>
#include <new>
using namespace std;

int main ()
{
int i,n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
p= new (nothrow) int[i];
if (p == 0)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
return 0;
}
This is an example code I took from some website. It asks you how many numbers you'd like to enter, creates a dynamic array of appropriate size and then saves the numbers you input into that array. I need to define a dynamic 2D array. Natually, I take their code and as my first step, try to do something like this:
Quote
p= new (nothrow) int[i][i];
I then remove the rest of the program (because it revolves around a 1D array), just to check the concept works. It doesn't. Visual Studio tells me this:
Quote
1>h:\my documents\visual studio 2010\projects\test1\testing_ground_1.cpp(69): error C2540: non-constant expression as array bound
1>h:\my documents\visual studio 2010\projects\test1\testing_ground_1.cpp(69): error C2440: '=' : cannot convert from 'int (*)[1]' to 'int *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Help please!

EDIT: Nevermind, I found the answer. I have to specify every dimension of my array. So a set of pointers within a set of pointers. The solution code from some website:
Quote
int **dynamicArray = 0;

//memory allocated for elements of rows.


dynamicArray = new int *[ROWS] ;

//memory allocated for elements of each column.


for( int i = 0 ; i < ROWS ; i++ )
dynamicArray[i] = new int[COLUMNS];

//free the allocated memory


for( int i = 0 ; i < ROWS ; i++ )
delete [] dynamicArray[i] ;
delete [] dynamicArray ;


Post has been edited 1 time(s), last time on Dec 3 2011, 4:42 pm by JaFF.



None.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[01:56 am]
Oh_Man -- cool bit of history, spellsword creator talking about the history of EUD ^
[09:24 pm]
Moose -- denis
[05:00 pm]
lil-Inferno -- benis
[10:41 am]
v9bettel -- Nice
[2024-4-19. : 1:39 am]
Ultraviolet -- no u elky skeleton guy, I'll use em better
[2024-4-18. : 10:50 pm]
Vrael -- Ultraviolet
Ultraviolet shouted: How about you all send me your minerals instead of washing them into the gambling void? I'm saving up for a new name color and/or glow
hey cut it out I'm getting all the minerals
[2024-4-18. : 10:11 pm]
Ultraviolet -- :P
[2024-4-18. : 10:11 pm]
Ultraviolet -- How about you all send me your minerals instead of washing them into the gambling void? I'm saving up for a new name color and/or glow
[2024-4-17. : 11:50 pm]
O)FaRTy1billion[MM] -- nice, now i have more than enough
Please log in to shout.


Members Online: NudeRaider, Roy