Error C1083: Cannot open include file: 'stdafx.h' - c++

When I compiled this program (from C++ Programming Language 4th edition):
main.cpp
#include <stdafx.h>
#include <iostream>
#include <cmath>
#include "vector.h"
using namespace std;
double sqrt_sum(vector&);
int _tmain(int argc, _TCHAR* argv[])
{
vector v(6);
sqrt_sum(v);
return 0;
}
double sqrt_sum(vector& v)
{
double sum = 0;
for (int i = 0; i != v.size(); ++i)
sum += sqrt(v[i]);
return sum;
}
vector.cpp
#include <stdafx.h>
#include "vector.h"
vector::vector(int s)
:elem{ new double[s] }, sz{ s }
{
}
double& vector::operator[](int i)
{
return elem[i];
}
int vector::size()
{
return sz;
}
vector.h
#include <stdafx.h>
class vector{
public:
vector(int s);
double& operator[](int i);
int size();
private:
double* elem;
int sz;
};
It gave me these errors:
I run it on Microsoft Visual Studio 2013, on Windows 7. How to fix it?

You have to properly understand what is a "stdafx.h", aka precompiled header. Other questions or Wikipedia will answer that. In many cases a precompiled header can be avoided, especially if your project is small and with few dependencies. In your case, as you probably started from a template project, it was used to include Windows.h only for the _TCHAR macro.
Then, precompiled header is usually a per-project file in Visual Studio world, so:
Ensure you have the file "stdafx.h" in your project. If you don't (e.g. you removed it) just create a new temporary project and copy the default one from there;
Change the #include <stdafx.h> to #include "stdafx.h". It is supposed to be a project local file, not to be resolved in include directories.
Secondly: it's inadvisable to include the precompiled header in your own headers, to not clutter namespace of other source that can use your code as a library, so completely remove its inclusion in vector.h.

Just include windows.h instead of stdfax or create a clean project without template.

There are two solutions for it.
Solution number one:
1.Recreate the project. While creating a project ensure that precompiled header is checked(Application settings... *** Do not check empty project)
Solution Number two:
1.Create stdafx.h and stdafx.cpp in your project
2 Right click on project -> properties -> C/C++ -> Precompiled Headers
3.select precompiled header to create(/Yc)
4.Rebuild the solution
Drop me a message if you encounter any issue.

You can fix this problem by adding "$(ProjectDir)" (or wherever the stdafx.h is) to list of directories under Project->Properties->Configuration Properties->C/C++->General->Additional Include Directories.

Add #include "afxwin.h" in your source file. It will solve your issue.

Just running through a Visual Studio Code tutorial and came across a similiar issue.
Replace #include "stdafx.h" with #include "pch.h" which is the updated name for the precompiled headers.

Related

Pre-Compiled Header (why need to include in every source file)

I am trying to understand pre-compiled headers. So I set up the following sample project:
pch.hpp
#include <vector>
pch.cpp
#pragma once
#include "pch.hpp"
Vector.hpp
#pragma once
#include "pch.h"
class Vector {
public:
Vector(const size_t N, const int init);
private:
std::vector<int> m_data;
};
Vector.cpp
#pragma once
#include "pch.hpp"
#include "Vector.hpp"
Vector::Vector(const size_t N, const int init)
: m_data(N, init) { }
Source.cpp
#include "pch.hpp"
#include "Vector.hpp"
int main() {
const Vector v1(3, 5);
}
I did this in VS 2022 by marking pch.hpp as the pre-compiled header file under Project Properties --> C++ --> Pre-Compiled Headers --> Header File with Use (/Yu). And setting pch.cpp as Create (/Yc).
Now, when I remove #include "pch.hpp" in Vector.cpp or Source.cpp, I get this error:
File Vector.cpp
Line 10
Severity Error
Code C1010
Description unexpected end of file while looking for precompiled header.
Did you forget to add '#include "pch.hpp"' to your source?
However, if I disable pre-compiled headers, everything compiles fine. As one would expect.
So, it seems that with pre-compiled headers enabled, I now need to put #include "pch.hpp" into every other source file? Even though #include "Vector.hpp" already includes pch.hpp? Is it possible to avoid having to put #include "pch.hpp" into all my other source files?
I ask, because I have a much larger code base (than the example above), and I thought I could essentially replace all instances of #include <vector> with #include "pch.hpp" and be done. But having to add #include "pch.hpp" to numerous cpp files, requires me to touch a lot of files (and seems counter-intuitive).
Apologies for the possibly naive question, as I am new to pre-compiled headers.

#include "stdafx.h" Error for C++ Tutorial [duplicate]

When I compiled this program (from C++ Programming Language 4th edition):
main.cpp
#include <stdafx.h>
#include <iostream>
#include <cmath>
#include "vector.h"
using namespace std;
double sqrt_sum(vector&);
int _tmain(int argc, _TCHAR* argv[])
{
vector v(6);
sqrt_sum(v);
return 0;
}
double sqrt_sum(vector& v)
{
double sum = 0;
for (int i = 0; i != v.size(); ++i)
sum += sqrt(v[i]);
return sum;
}
vector.cpp
#include <stdafx.h>
#include "vector.h"
vector::vector(int s)
:elem{ new double[s] }, sz{ s }
{
}
double& vector::operator[](int i)
{
return elem[i];
}
int vector::size()
{
return sz;
}
vector.h
#include <stdafx.h>
class vector{
public:
vector(int s);
double& operator[](int i);
int size();
private:
double* elem;
int sz;
};
It gave me these errors:
I run it on Microsoft Visual Studio 2013, on Windows 7. How to fix it?
You have to properly understand what is a "stdafx.h", aka precompiled header. Other questions or Wikipedia will answer that. In many cases a precompiled header can be avoided, especially if your project is small and with few dependencies. In your case, as you probably started from a template project, it was used to include Windows.h only for the _TCHAR macro.
Then, precompiled header is usually a per-project file in Visual Studio world, so:
Ensure you have the file "stdafx.h" in your project. If you don't (e.g. you removed it) just create a new temporary project and copy the default one from there;
Change the #include <stdafx.h> to #include "stdafx.h". It is supposed to be a project local file, not to be resolved in include directories.
Secondly: it's inadvisable to include the precompiled header in your own headers, to not clutter namespace of other source that can use your code as a library, so completely remove its inclusion in vector.h.
Just include windows.h instead of stdfax or create a clean project without template.
There are two solutions for it.
Solution number one:
1.Recreate the project. While creating a project ensure that precompiled header is checked(Application settings... *** Do not check empty project)
Solution Number two:
1.Create stdafx.h and stdafx.cpp in your project
2 Right click on project -> properties -> C/C++ -> Precompiled Headers
3.select precompiled header to create(/Yc)
4.Rebuild the solution
Drop me a message if you encounter any issue.
You can fix this problem by adding "$(ProjectDir)" (or wherever the stdafx.h is) to list of directories under Project->Properties->Configuration Properties->C/C++->General->Additional Include Directories.
Add #include "afxwin.h" in your source file. It will solve your issue.
Just running through a Visual Studio Code tutorial and came across a similiar issue.
Replace #include "stdafx.h" with #include "pch.h" which is the updated name for the precompiled headers.

#include iostream before stdafx.h in c++

I created a C++ Console Application in Visual Studio Community 2017. There is only a main.cpp file in the project. Here is my main.cpp file:
#include <iostream>
#include "stdafx.h"
int main()
{
std::cout << "hello world!";
return 0;
}
I get a compilation error that 'cout' is not a member of std. But if I include iostream after stdafx.h, that is,
#include "stdafx.h"
#include <iostream>
int main()
{
std::cout << "hello world!";
return 0;
}
then it compiles just fine. So why does it not work when I include iostream before stdafx.h?
The answer to your question can be found, with a little puzzling, here.
stdafx.h enables precompiled headers. Based on the error given, and the discussion of how Microsoft implements precompiled headers, it seems that the compiler simply starts compiling from the include of stdafx.h forward. So when stdafx.h is placed after iostream, iostream is not included, producing the mysterious error.

C++ Do I have to include standard libraries for every source file?

I'm a bit confused at the moment because I'm planning to include multiple source and header files for the first time in one of my projects.
So I'm wondering if this would be the right approach?
Do I have to include the string header in every source file that uses it directly?
And what about the "stdafx.hpp" header that Visual C++ wants me to include?
Would that be the way to go?
main.cpp
#include "stdafx.hpp"
#include <string> //?
#include <stringLib1.h>
#include <stringLib2.h>
using std::string;
//use a windows.h function here
//use a stringLib1 function here
//use a stringLib2 function here
stringLib1.h
#include "stdafx.hpp"
#include <string>
using std::string;
class uselessClass1
{
public:
string GetStringBack1(string myString);
};
stringLib1.cpp
#include "stdafx.hpp"
string uselessClass1::GetStringBack1(string myString) {
return myString;
}
stringLib2.h
#include "stdafx.hpp"
#include <string>
using std::string;
class uselessClass2
{
public:
string GetStringBack2(string myString);
};
stringLib2.cpp
#include "stdafx.hpp"
string uselessClass2::GetStringBack2(string myString) {
return myString;
}
A good practice is usually to include only what your code uses in every file. That reduces dependencies on other headers and, on large projects, reduce compilation times (and also helps finding out what depends on what)
Use include guards in your header files
Don't import everything by polluting the global namespace, e.g.
using namespace std;
but rather qualify what you intend to use when you need it
You don't need stdafx.h in your project unless you're using precompiled headers. You can control this behavior in the VS project properties (C/C++ -> Precompiled Headers -> Precompiled Header)
The stdafx.h header is needed if precompiled header is enabled in VS. (Read this one)
You only need to include the stdafx.h in your .cpp files as the first include.
Regarding the header and cpp files (which come in pairs), include things necessary for the declaration in the header, and include everything else (necessary for the definition) in the cpp. Also include the corresponding header in its cpp pair too. And use include guards.
myclass.h
#ifndef MYCLASS_H // This is the include guard macro
#define MYCLASS_H
#include <string>
using namespace std;
class MyClass {
private:
string myString;
public:
MyClass(string s) {myString = s;}
string getString(void) {return myString;}
void generate();
}
myclass.cpp
#include <stdafx.h> // VS: Precompiled Header
// Include the header pair
#include "myclass.h" // With this one <string> gets included too
// Other stuff used internally
#include <vector>
#include <iostream>
void MyClass::generate() {
vector<string> myRandomStrings;
...
cout << "Done\n";
}
#endif
Then in main(...), you can just include myclass.h and call generate() function.
The stdafx include should be at the top of every .cpp file and it should NOT be in .h files.
You could put #include < string > in stdafx.h if you don't want to put it in every other file.
I suppose that you must be having your own header files also which might be requiring in other cpp files and header files. Like the one you gave
#include <stringLib1.h>
#include <stringLib2.h>
In my opinion, its better to create one common header file in which you include all the common library header files and your project header file. This file then you can include in all the other cpp files and header file. And it will be better to use header guards also.
So, considering a common header file "includes.h".
#ifndef INCLUDES_H
#define INCLUDES_H
#include <string>
#include <stringLib1.h>
#include <stringLib2.h>
/***Header files***/
#endif //INCLUDES_H
This is now your common header file. This you can include in all your project files.

Can't access function from header file

//head.h//
extern int sum(int,int);
//head.cpp//
#include "head.h"
#include "stdafx.h"
int sum(int x, int y)
{
return (x+y);
}
//mainfn.cpp//
#include "head.h"
#include "stdafx.h"
#include string
#include iostream
#include stdio.h
using std::string;
using std::cout;
using namespace System;
int main()
{
int x=10,y=2;
printf("value: %d",sum(x,y));
Console::ReadLine();
return 0;
}
While buliding in Visual studio 2005, this vc++ project is giving following error:
error C3861: 'sum': identifier not found.
Can anybody help me out with this?
You need to place the inclusion of head.h after stdafx.h. When precompiled headers are enabled the compiler will ignore the contents of all includes that occur prior to (in this case) the inclusion of stdafx.h .
Either remove stdafx.h from the project, and turn of precompiled headers.. or try moving head.h to be included after stdafx.h instead of before.