I 'am running my first C++ project using Visual Studio 2012 This is the architecture of my project:
Source Files
|main.cpp
|stdafx.cpp
|module.cpp
Header files
|stdafx.h
|targetver.h
|module.h
I always try to be modular in my code so i usually decalare variables and structures in the .h and then define them on the cpp files
//module.h
#ifndef MODULE_H
#define MODULE_H
int a;
typedef struct
{
int a;
int b;
}tmastruct;
typedef enum {LUNDI, MARDI}eMaEnum;
extern void fct(void);
#endif
module.cpp:
//module.cpp
#include "stdafx.h"
#include "a.h"
void fct()
{
printf ("Hello World \n");
}
but actually i don't know why i get
error LNK2005: "int a" (?a@@3IA) main.obj
i know that by declaring a as extern int a the problem will be resolved but i wonder why the first declaration works on a c project and not in a c++ project
i also have a question about using stdAfx.h the proper way after checking some posts i make the following rule:
1) stdafx.h should contain the headers that are rarely modified (it means it shouldn't contain the header of your own modules)
2) stdafx.h should be included only in the .cpp files after including stdafx.h you may include your own modules header
do you think that this rule is right and what do you do to handle your modules?