error C1083 in MSVS 2010

Feb 7, 2014 at 8:49am
Hi.I've written a code in MSVS 2010 and it works fine,but I decided to separate function definition in a separate header file -sud.h and here is the problem.However I changed the option of precompiled headers in project properties ,it's occured errors .Last version with least errors is at Use(/Yu)and the error message is:

sud.cpp(5): fatal error C1083: Cannot open precompiled header file: 'Debug\sud.pch':

This is my include files in sud.cpp:
1
2
3
4
   #include<iostream>
   #include<vector>
   #include<algorithm>
 5 #include<stdafx.h> 

And stdafx.h is:

#pragma once
#include "targetver.h"
#include <sud.h>
#include <stdio.h>
#include <tchar.h>

Any ideas what is wrong ?
Feb 7, 2014 at 9:27am
Precompiled headers on Microsoft compilers is not automatically handled. You have to stick to the rules. And the rules are:
1. All C/C++ source files must have the precompiled header as the first include file in each source file.
2. That include file must be the first code statement in these source files.
3. On file in the project must be configured to create the precompiled header.
4. All other files in the project must be configured to use the precompiled header.

The Visual Studio Wizards name the precompiled header file stdafx.h, and the file that compiles it, stdafx.cpp.

As you should now realise, you've violated many of the rules. In your case, #include<stdafx.h> must be the first include file in all .cpp files.

You don't need #pragma once in #include<stdafx.h> because you should never need to include it in a header file.

In your case, your specific error means that the precompiled header file hasn't been built. So you need to go thru the precompiled header section of all .cpp files in the project and ensure that stdafx.cpp is used to create the precompiled header, and all other files use the precompiled header. You also need to ensure that all .cpp files in the project have the same name for the precompiled header.
Last edited on Feb 7, 2014 at 9:29am
Feb 7, 2014 at 11:04am
you use the precompiled header #include<stdafx.h> . It must be the first #include

Further more you need a file where you tell the compiler to create the precompiled header.

Honestly I recommend to switch off that precompiled header thing. It's supposed to increase the speed of compilation, but I doubt that it works and I don't think it's worth the hassle
Feb 7, 2014 at 11:49am
I did all you told me to do but when I tried to compile sdtafx.cpp I receive error:

stdafx.h(1): fatal error C1083: Cannot open include file: 'sud.h': No such file or directory

My stdafx.h file is :
1
2
3
4
#include <sud.h>
#include "targetver.h"
#include <stdio.h>
#include <tchar.h> 

What is wrong now ?
Feb 7, 2014 at 11:51am
sud.h is a system header file ? I doubt it.
#include "sud.h"
Last edited on Feb 7, 2014 at 11:52am
Feb 7, 2014 at 1:46pm
You really should read my post or just switch off precompiled headers.
Feb 7, 2014 at 2:14pm
I thing I figure out your post and problem is solved ,but now there is a problem with linking,here is a part of error message:


sud.obj : error LNK2001: unresolved external symbol "class std::vector<int,class std::allocator<int> > mrow" (?mrow@@3V?$vector@HV?$allocator@H@std@@@std@@A)
sud.obj : error LNK2001: unresolved external symbol "int hor1" (?hor1@@3HA)
sud.obj : error LNK2001: unresolved external symbol "int ver1" (?ver1@@3HA)
sud.obj : error LNK2001: unresolved external symbol "int value" (?value@@3HA)
sud.obj : error LNK2001: unresolved external symbol "int cicle" (?cicle@@3HA)
sud.obj : error LNK2001: unresolved external symbol "int v" (?v@@3HA)
sud.obj : error LNK2001: unresolved external symbol "int h" (?h@@3HA)
sud.obj : error LNK2001: unresolved external symbol "int y" (?y@@3HA)
sud.obj : error LNK2001: unresolved external symbol "int x" (?x@@3HA)
Feb 7, 2014 at 2:51pm
Can you show where you declare and define mrow?
Feb 10, 2014 at 7:25am
Hi again.I will show you my header file sud.h( a part of it) where mrow is.
In sud.cpp file I fill and clear mrow many times in many functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
extern int val;
extern int value,val6,hor,ver,h,v,countnull,countn,vrt,hrz,nexthor,nextver,cicle,n,x,y,n7;
extern int hor1,ver1,hor2,ver2,hor3,ver3,hor5,ver5,hor6,ver6,hor7,ver7,hrz8,vrt8,count8,countv,ver8,hor8,value8;
extern void algor1(int lim,int xr,int yr);
extern void algor2(int lim);
extern void algor3(int lim);
extern void algor4(int lim);
extern void algor5(int lim);
extern void algor6(int lim,int xr,int yr);
extern void algor7(int lim);
extern void algor8(int lim);
typedef vector<vector<int> > vv;
extern vector<int> tempvalues,mrow, tempnvalues,copytempnvalues,copytempnvalues7,nullrow,savenullinline,indexofnull;
extern vv matrix,savetempnull,savenull, matrixtemp,copymatrix,copymatrix7,matrixtemp7;
extern vv::iterator itv;
Feb 10, 2014 at 7:38am
These are all declarations. You need definitions for those declarations. The linker is complaining because it cannot find where these declarations were defined.

And, by the way, way too many global variables.
Feb 10, 2014 at 7:50am
I know about global variables,but later I will optimize them. Here is the definitions for some of the functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void algor2(int lim) //fills rows and cols with single nulls
{
	n=0;
	while(n<lim)
	{
		for(int i=0;i<9;i++)
			{
			
				if(checkRowForSingleNull(i))//check rows for single null
					matrix[i][ver2]=retSingleNullRowValue(i);//if yes find missing value and return it
			
				if(checkColForSingleNull(i))//check cols for single null
					matrix[hor2][i]=retSingleNullColValue(i);//if yes find missing value and return it
			}
		n++;
	}
}
void algor3(int lim)//fills submatrix[3][3] with single null
{
	n=0;
	while(n<lim)
	{
		for(int i=0;i<9;i+=3)
		{
			for(int j=0;j<9;j+=3)
			{
				if(checkTrmForSingleNull(i,j))//check vector<vector<int> >trm[3][3] with location(x,y) for single null
					matrix[hor3][ver3]=retSingleNullTrmValue(i,j);//return evaluated value of a single null cell(i,j)
			}
		}
		n++;
	}
}
Feb 10, 2014 at 7:57am
It is variables which the linker is complaining about, not functions.
Feb 10, 2014 at 8:19am
I don't understand . algor1() and algor2() aren't functions?
Feb 10, 2014 at 8:33am
They certainly are functions. I would direct you to your error output in an earlier post where you will see no mention of the functions not being defined. It is the variables which the linker can find no definition for.
Feb 10, 2014 at 8:38am
The extern declarations are just that: declarations.
You need to define them (whitout extern) somewhere (in a .cpp file) so that the linker can find them
Feb 10, 2014 at 9:33am
Now I understand.Do I have to declare at all variables in *.h file or I may declare them only in .cpp file.
Feb 10, 2014 at 10:30am
declare them in the *.h file (with extern)
define them in the *.cpp file

The reason: if you define them in the header file and it's included more than once you will have multiple definitions (a linker error)

Note that declaration and definition of variables are usually the same. Except for this special case with extern
Feb 10, 2014 at 11:11am
Ok.Thanks
Topic archived. No new replies allowed.