Jump to content

MS Visual C++ not reading code


Tesset

Recommended Posts

I've been trying to create a simple Hello World program using this tutorial that I found online. I haven't been able to get it to work, however. When I run the code (Whether I type it in, or copy paste), I get an error which reads:

 

There were build errors. Would you like to continue and run the last successful build?

and then it puts info in the build section:

 

1>------ Build started: Project: HelloWorld, Configuration: Debug Win32 ------
1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup
1>C:\Users\Tyler\Desktop\Programming projects\HelloWorldC++\HelloWorld\Debug\HelloWorld[Caution: Executable File] : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I (obviously) figured out that something was wrong, and looked for a tutorial specifically for Visual C++, and found this one. I followed the instructions under "The First Time You Run Visual C++" with the thought that I probably just messed up in creating the project. However, I get the same problem as before.

 

The very first time I opened a new project, I was shown this piece of code:

 

// HelloWorld.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
   Console::WriteLine(L"Hello World");
   return 0;
}

 

And it ran perfectly - it showed a command prompt screen that said Hello World, and then it closed. However, if I tried to add basically anything besides another "Console::WriteLine" statement, it would give me the same error as above. Additionally, I am unable to find any documentation on that particular statement, which confuses me.

 

The only thing that I can think of is that I need to be building the program, rather than pressing F5 to debug it. However, I can't find a button to test with that says build rather than debug.

 

What am I missing? Do I need to install something else? Use a different GUI builder? Mess with the settings?

 

And yes, I know that I posted in the Programming basics thread that I wanted to learn Java. I decided I wanted to start with something I knew a little more about - GML is based on C++, so I have some background sort of.

My skin is finally getting soft
I'll scrub until the damn thing comes off

Link to comment
Share on other sites

Is visual C++ the same as the one inclused in Visual Studio? If it is I'm not sure what was your original code, but the way, several years ago, when I first started programming (in college), it was with visual studio pro and this is how we did it.

 

First has to be a win32 application, and you have to choose empty project.

 

This is what it looks like (except I didn't keep the comments and added one)

 

#include <iostream>

using namespace std;

int main(void) //void being optional
{
cout << "Hello World.." << endl;

return(0);
}

 

Of course with this it will only stay open if you run it from Visual C++ (if that really is the name of the program).

 

Write the same thing I have put in the code tags (as copy/paste can sometimes paste the space characters and such) and let me know if this runs in your program or if it still gives you an error.

Link to comment
Share on other sites

Yes, the Visual C++ included in MS Visual Studio.

 

Short answer - I still get a couple errors.

 

[hide=Long answer]I started a new project. It brought me to a screen like this:

 

20941366.png

 

I selected the Win32 Console Application, typed in a name, and clicked ok. Was brought to this screen:

 

96394696.png

 

Selected Empty project, and was given this screen:

 

58100498.png

 

Clicked File->New->File and was shown this:

 

13470537.png

 

Selected C++ file, and was given this:

 

91571259.png

 

Typed in the code exactly as you had it (I didn't type the comment), clicked Debug->Start debugging:

 

60503325.png

 

21175643.png

 

I got those two errors. Same as last time, which makes sense because it's essentially the same code. I still don't know what I'm doing wrong.[/hide]

My skin is finally getting soft
I'll scrub until the damn thing comes off

Link to comment
Share on other sites

Relevant info:

int main(int argc, char *argv[]) You probably need to set Project->properties->linker->system->subsystem to CONSOLE

 

If you have an INCOMPATIBLE NON STANDARD C++ main; breaking all the standards in the book; such as the Microsoft veriety then set the same subsystem property to WINDOWS

 

THE ERRORs if you SET subsystem to CONSOLE is:

 

MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function _mainCRTStartup

 

OR THE ERROR if you SET subsystem to WINDOWS is:

 

MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function _WinMainCRTStartup

 

The error means you do not have the main visible to the the linker. But you are editing the file with main or WinMain in the VS Editor, so what is wrong?

 

Visual Studio can be used as a stand alone editor giving you the illusion that the stuff you are editing can and should be compilable. So the solution is simple you failed to add the file you are editing to the project.

 

Right click on solution name->Add->Existing Item->file with main

 

Ctl-shift-B and it compiles Hello World.

 

Solution:

From the long answer you gave, it is clear that the file you created was not added to the project, you created a completely seperate file. This file was not linked to the project and so was not visible to the linker. To add a file to your project you need to right click the relevant folder on the left(in this case right click "source files") and create a new file from the menu there. Then add your code as normal and all should be fine.

unledajq.png

#include <iostream>

int main()
{
   std::cout << "Hello world" << std::endl;
   return 0;
}

 

Oh and F7 is build. To run a console application and actually see the result you can do either:

 

locate the built [Caution: Executable File] by explorer, usually in project name/Debug/, copy the full directory path (windows vista+ right click the directory path and you can edit it as full text path, copy from there)

 

windows key + r (or start>run) > type "cmd" press enter > type "cd " (include the space) > right click the window click paste > press enter > type the name of the [Caution: Executable File] including the extension [Caution: Executable File] > press enter

 

This will run the program. To run it again all you have to do is press the up arrow to recall the previous command and press enter. If you wish to stop execution of the program early, press CTRL C

 

The other way to see the output is by adding some code just before return 0 of main.

 

#include <iostream>

int main()
{
std::cout << "hello world" << std::endl;
system("pause");
return 0;
}

 

Then all you have to do is double click the file in explorer and it pauses before it exits.

system(""); gives access to the OS Shell eg open up cmd[Caution: Executable File] (windows key + r > type "cmd" press enter > type "pause" press enter).

Link to comment
Share on other sites

I was going to mention that it wasn't added to the project when I saw the pictures but I saw that Markup already did.

 

Good luck in your future projects with C++ :).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.