C++ multiple files with Microsoft Visual C++ 2010 Express - c++

I am brand new to C++. I have a question on how to run a C++ program that contains multiple files. I am using Microsoft Visual C++ 2010 Express as the IDE. I don't know why, but the program won't run. I am pretty sure that my coding is all right. How can I fix this? Any help will be greatly appreciated. Below is my code in the two files (the project name is PracticeConsoleMultipleFiles and I created the project as a Win32 Console Application):
NewFile1.cpp:
int add(int x, int y){
return x + y;
}
Main.cpp:
#include "stdafx.h"
#include <iostream>
int add(int x, int y);
int main(){
using namespace std;
cout << "The sum of 9 and 9 is " << add(9, 9) << endl;
return 0;
}
When I build my program, I get this:
1>------ Build started: Project: PracticeConsoleMultipleFiles, Configuration: Debug
Win32 ------
1> NewFile1.cpp
1>c:\users\timothylee\documents\visual studio 2010 \projects\main\practiceconsolemultiplefiles\newfile1.cpp(4): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?
1> Main.cpp
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
and when I run it, it says that:
This project is out of date: PracticeConsoleMultipleFiles - Debug Win32

I recommend that you, as a beginner to c++, start off with a blank project when using visual studio, this will not place the code including stdafx.h in your main.cpp which should solve the error you are getting

Related

I get an error in visual studio 2017 using the header stdafx.h in the code

Here is the code. I'm getting the following error, what am I doing wrong? I'm using visual studio 2017.
1>------ Build started: Project: Print1, Configuration: Debug Win32 ------
1>Print1.cpp
1>c:\users\kiwiblazer\source\repos\print1\print1\print1.cpp(4): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source?
1>Done building project "Print1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Never fear, C++ is here! ";
return 0;
The error log says Did you forget to add '#include "pch.h"'. This is because they changed the precompiled headers name to pch.h. for VS17
Try replacing stdafx.h with pch.h. I think that would be the easiest workaround.
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Never fear, C++ is here! ";
return 0;
}
Your code is incorrect. Please post complete code. for example closing bracket of main is missing in the code and that might be the reason for failure.
If your code is correct then please check Configuration Properties -> C/C++ -> Precompiled Headers properties of your solution and change your pre compile header files accordingly.

C++ Error for file_size() which belongs to Boost Filesystem

My IDE is MS Visual Studio C++ 2013, and I use Boost Library for Filesystem Operations.
I have written this code:
// BoostFileSystem.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <boost\filesystem.hpp>
using namespace boost::filesystem;
using namespace std;
int main()
{
cout << "File Size: " << endl;
cout << file_size("as.jpg");
return 0;
}
It throws error. The error is that:
1>------ Build started: Project: BoostFileSystem, Configuration: Debug Win32 ------
1> BoostFileSystem.cpp
1> LINK : C:\...\visual studio 2013\Projects\BoostFileSystem\Debug\BoostFileSystem.exe not found or not built by the last incremental link; performing full link
1> BoostFileSystem.vcxproj -> C:\..\documents\visual studio 2013\Projects\BoostFileSystem\Debug\BoostFileSystem.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
'as.jpg' is also included in the Resources folder. When I remove this line:
file_size("as.jpg");
The error is not thrown. Also bear in mind that the compiler reports no failure.
What is the problem? I really cannot figure it out.
Because when I remove file_size() line, the program works fully and also prints the text on the screen ("File Size:"). While when run it as-is, the compiler throws no error, but no text (even the line before the file_size()) is not printer and in the console the following is printed (which is also outputed to the error-area as already pasted above):
UPDATED:
Here is the error shown in the console window:
The system cannot find the file C:\Users\Ali\do
ts\BoostFileSystem\Debug\BoostFileSystem.exe.
The problem is solved. I post it for future visitors.
The problem is with incremental linking enabled in MS-Visual Studio C++. If disable it, then the program works.
Go to the properties of the project, then to the Linker, General and Disable the Incremental Linking.

Building an EXE with Visual Studio 2013

I'm unclear on how to do this. All I can find on the internet is that you build and then the exe will automatically appear in the project file, but that is not the case for me. I just tried setting up an extremely basic C++ project with one main.cpp of this:
#include <iostream>
int main() {
std::cout << "Hey ho!" << std::endl;
}
It runs all nice and well in Visual Studio, but there is no EXE in the project. Am I doing something wrong? Also, for the record, this is the output I get when I build:
1>------ Build started: Project: ConsoleApplication1, Configuration: Release Win32 ------
1> main.cpp
1> Generating code
1> Finished generating code
1> ConsoleApplication1.vcxproj -> c:\users\oysi\documents\visual studio 2013\Projects\ConsoleApplication1\Release\ConsoleApplication1.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Try F5, it will build and run your project. If you put a breakpoint at the closing bracket (}) of main, you'll even see your output in the console.
The .exe won't be added to the project, but it will be created in the output directory. The output you copied here actually tells you, where you can find the .exe:
c:\users\oysi\documents\visual studio 2013\Projects\ConsoleApplication1\Release\ConsoleApplication1.exe
As Visual tells you, you should look for .EXE file in this directory:
c:\users\<username>\documents\visual studio 2013\Projects\ConsoleApplication1\Release\ConsoleApplication1.exe

playing a .wav file

I'm using visual studio 2010 express and I'm trying to write a simple program that will repeat a wave file 5 times(I'm running a windows xp sp3).
This is as far as I got:
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int main ()
{
int a = 5;
while(a >= 1){
cout << "It's a wav file";
PlaySound(L"SomeAudioFile-01.wav", NULL, SND_FILENAME);
--a;
}
return 0;
}
The problem is I keep getting this error message when I'm building it:
1>------ Build started: Project: It's a F**king Country, Configuration: Release Win32 -- ----
1> mycode.cpp
1>..\..\..\..\..\Shaul's documents\Visual Studio 2010\Projects\MyProject\Release\SomeAudioFile-01.wav : fatal error LNK1136: invalid or corrupt file
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The file is perfectly fine and plays with no problems whatsoever outside visual studio.
Help would be truly appreciated.
Instead of adding the WAV file to the project files, add it to the resources and use SND_RESOURCE instead of SND_FILENAME.
You include the sound file as a object file, so the compiler tries to link with it. But it's a binary file that's not linkable (which is what the error message says).
Don't include the sound file in the project, so the environment won't link with it.
P.S. In the future, please refrain from using "bad" words on a public site like this.
To get rid of the linker error, you need to tell the IDE to link with the winmm.lib library also, so open Project/Properties/Configuration Properties/Linker/Input and append winmm.lib in the Additional Dependencies field.
Also, use the following function profile:
PlaySound(L"audio.wav", NULL, SND_APPLICATION);

Building a C++ project in Visual Studio doesn't create any files

I recently decided to start learning Visual Studio so that it replaces my need for CodeBlocks and MinGW for C++ programming.
So, today I made a new Win32 C++ Console Application, wrote down this code in a new .cpp file
#include <iostream>
using namespace std;
int main()
{
int a;
cin >> a;
cout << a << endl;
return 0;
}
and compiled it. The log said
1>------ Build started: Project: CPP_CONSOLE_TEST, Configuration: Debug Win32 ------
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppBuild.targets(357,5): warning MSB8004: Output Directory does not end with a trailing slash. This build instance will add the slash as it is required to allow proper evaluation of the Output Directory.
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
and I though my code was compiled and my .exe was created.
Then, upon trying to debug my program, Visual Studio said:
Unable to start program 'C:\Users\XYZ\Documents\Visual Studio 2013\Projects\CPP_CONSOLE_TEST\Debug\CPP_CONSOLE_TEST.exe'. The system cannot find the file specified.
I then opened the Debug folder of the project and it was completely empty...
I've been searching around Google for some time and I even tried to "Repair" my Visual Studio build with no results. Any help?
Quick edit: Just tried compiling a C# app, just to see if the IDE itself was the problem. It compiled and ran just fine, so it's some issue with the Visual C++ compiler and its settings...
Turns out I hadn't added the source file to the Project... :|
Visual Studio, has its own vision of c++ projects. By default, it needs a #include "stdafx.h" on top of your cpp file, with the associated stdafx.h and stdafx.cpp files.
Then, in a c++ visual studio project, the real definition of the main function is int _tmain(int argc, _TCHAR* argv[]). But it should work with your definition.
Why don't you try to use Serge Rogatch's solution?
There is a bug in Visual Studio which leads to problems when project has long path.