Visual Studio 2017 Creating/Debugging Program - c++

I just upgraded to Visual Studio 2017 from 2015. I am taking a computing 2 course in college currently and last year when I used 2015 in computing 1 I'd create my projects by doing
New Project > C++ > Win32 Project > Checked "Empty Project" > Unchecked "SDL" > Then creating a main.c file
Now on 2017 I create a project by doing
New Project > C++ > Empty Project > Then creating a main.c file
Now on 2017 when I attempted to run a simple program by going to "Start without debugging" a terminal pops up really quick and then disappears right away without letting me see my program run.
This is my program
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
int x = 1, i;
for (i = 0; i != 10; i++)
{
printf("%d: %d\n", i, x);
}
return 0;
}
So my question is, How do I set up my projects in Visual Studios 2017 so that I can complete HW for my computing 2 class?

When you run C++ applications from Visual Studio they behave differently depending on a project setting (right click on project > Configuration Properties > System > Subsystem). For a console project /SUBSYSTEM:CONSOLE when the program ends, the console stays open and waits for you to press a key, allowing you to see the output. For other project types it closes.
So, to be able to view the results either create your project as a Win32 Console application OR if you already created it as an Empty project, then change the Subsystem property to Console.

Related

How to work on one SFML-C++ Project with different Visual Studio versions?

I'm working on a C++ project that requires SFML (free portable library) and some dependencies which I can copy onto a usb-stick/drive.
The issue is that I'm working with Visual Studio 2019 at home and very often at uni/work with Visual Studio 2013.
Whenever I try to run the project from one version on the other, exceptions and other errors are thrown around.
The only fix is creating a new project and annoyingly add each dependency & lib folder which I would absolutely 100% positively avoid at all cost...
How can I, aside from making two sepearte projects for each VS version, work on one project with different versions of Visual Studio?
I've tried using a portable codeblocks 17.02 copy but the IDE is not very pleasant to work with, especially since I own a Visual Studio Key and other students will review the code in Visual Studio as well.
This is just some simple window creation example:
#include <SFML\Graphics.hpp>
using namespace sf;
int main()
{
RenderWindow Window;
Window.create(VideoMode(800, 800), "VS2013-VS2019Example");
while (Window.isOpen()) {
Event e;
while (Window.pollEvent(e)) {
if (e.type == Event::Closed) {
Window.close();
}
}
Window.clear();
Window.display();
}
}
I expected the program to create a new window, 800x800 with a title and no interception.
Starting the program will result in VS indicating that line 8, the line where window.create() is called, throws a Access violation reading location 0x000FFF exception at 0x0F15A5E3 (msvcp120.dll) in [project name].exe.

Disabling Auto-generated comments in Visual Studio Enterprise 2017

I would like to disable auto generated comments of Visual Studio every time I create new project. Sample template of visual studio can be seen below. I don't want any of these comments. Thanks for any help.
`
// ex6_page174.cpp : This file contains the 'main' function. Program
execution begins and ends there.
//
#include "pch.h"
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project >
Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open >
Project and select the .sln file
`
You can't.
For better or for worse, this is by design.

Visual studio 2015 openmp support

I'm having trouble getting OpenMP support in Visual Studio 2015.
I've configured the project options to use /openmp (project->properties->C/C++->language->OpenMP support:yes), the code is as follows (very simple code, to test OpenMP):
#include <iostream>
#include <omp.h>
int main(int argc, char* argv[])
{
int n = 0;
#pragma omp parallel
{
std::cout << std::endl << "Hello World!";
}
return 0;
}
Only one thread runs and "Hello World!" is printed only once.
I was able to compile the program with VS2015 Community Version 14.0 Update 1 on Windows 8.1 64bit with OpenMP support.
Below, follow a list of steps that may help:
After create a new project and paste the source code, go to
Project-> Properties -> C/C++ -> Language
Change Open MP Support to Yes(/openmp)
Click Apply
On the left menu, go to Command Line and confirm that /openmp
appears somewhere at the compiler's options.
If it appears, click Ok and build the project.
Before run the program, put a breakpoint at the line:
int n = 0;
Run the the program by clicking on Local Windows Debugger
When the program stops at the breakpoint, go to Debug -> Windows -> Disassembly
Somewhere, near the breakpoint, look for an assembly line like:
call __vcomp_fork (?????????h)
If you find this line, chances are that openmp is ok and running.
Some other checks that can help:
Get a tool from Windows Sysinternals like Process Explorer (GUI) or ListDLLs (command line).
ListDLLs:
With the program stopped at the breakpoint, open task manager and look for the PID of the process.
Open a command prompt and run the command:
listdlls [PID] | findstr -i vcomp
Should appear something like VCOMP140D.DLL or VCOMP140.DLL or VCOMP????.DLL.
If it not appears, probably the compiler couldn't find the openmp dll so you'll have to see if this library is available at some point on your system.
Two last tips that may save your time:
If you change any configuration (e.g. Debug -> Release or x86 -> x64),
check again if Command Line has the /openmp option set ok.
If you try to force the compiler to C language (instead of C++), maybe the pragma:
#pragma omp parallel for
will not work (Update: Apparently this problem does not happen anymore on VS2017).
It shows me the message:
INTERNAL COMPILER ERROR in 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\CL.exe'
Come back the compiler to C++ language and the parallel for will works ok.
Partial answer: I was not able to get the compiler to accept /openmp through the config/GUI, but compiling in console with cl.exe /openmp works.

How do I set up Visual Studio with default headers and comments?

I am fairly new to using Visual Studio. Currently I am using Visual Studio 2015 version. Currently when I set up a new C++ console application project (with Precompiled header off or on) it displays the following:
// Name of project.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int main()
{
return 0;
}
Every time I create a new project I have to waste my time entering in my header information and comments. I would like it to look like this every time I create a new C++ console application project.
// *********************************************************************
// Name:
// Description:
// Date:
// Class Section:
// Title:
// *********************************************************************
//Heading information
#include <iostream>
#include "stdlib.h"
#include "stdafx.h"
using namespace std;
//Main function
int main()
{
//Pause system and end main function
system("pause");
return 0;
}
Is it possible to set up Visual Studio 2015 to display the code above every time I create a new project? Thanks
Create a new console application.
Edit the file to be as you want it in the new one.
Select "Export Template..." in the "File" menu.
Walk through the wizard (choose a name for your new template, etc.)
Be sure the "Automatically import the template into Visual Studio" is checked.
Restart VS
Select "New Project"
Select "Visual C++" in the tree control on the left
Select your new template in the list in the middle
The usual new-project stuff (name and location for the project, etc.)
[Note: As I'm writing this, I'm looking at it in VS 2013. I don't recall it's changing in VS 2015, but it's possible.]

How to compile c++ file in visual studio?

I am new to Visual Studio and I don't know how to compile a .cpp file. I made just a single .cpp file (ctr + n => Visual C++ => C++ file) and I tried to compile it. But in place where normally there's a compile button (like with c#) there is strange 'Attach' button. I don't get what's going on, but I thought, Visual C++ might be some different version of normal C++. If so is that possible to compile normal C++ file in Visual Studio?
The problem is, Visual Studio don't really know, what to do with your .cpp file. Is it a program? Try the following:
File | New project
Visual C++ | Win32 | Win32 Project
Select a name and location for the project
Next
Choose Console application
Choose Empty project
Deselect Precompiled header
(optionally) Deselect SDL checks
Finish
Right-click on Source files and choose Add | New Item...
Choose C++ File
Choose name for this file
Write the following inside:
#include <stdio.h>
int main(int argc, char * argv[])
{
printf("Hello, world!\n");
return 0;
}
Press F5
You should, just as you did for C#, create a C++ project and add your source file to that. Then there will be all the build options you ever dreamed of.