C++: Understanding Header Files & Header Guards with Easy Addition Example -


i can't head around headers , header guards. i've read other questions , answers still can't make work in visual studio 2013:

main.cpp

#include "stdafx.h" #include <iostream> #include "add.h"  int _tmain(int argc, _tchar* argv[]) {     std::cout << "3 + 4 = " << add(3, 4) << std::endl;     system("pause");     return 0; } 

add.cpp

#include "stdafx.h" //added later; working (and see question 2 below) #include "add.h" //added later; nor working (and see question 2 below) int add(int x, int y) {     return x + y; } 

add.h

#ifndef add_h #define add_h int add(int x, int y); #endif 

when compile, console window flashes on-screen disappears. error list contains:

error 1 error lnk2019: unresolved external symbol "int __cdecl add(int,int)" (?add@@yahhh@z) referenced in function _wmain c:\users\danny\documents\visual studio 2013\projects\addition program\main\main.obj main

error 2 error lnk1120: 1 unresolved externals c:\users\danny\documents\visual studio 2013\projects\addition program\debug\main.exe main


1. how headers , header guards work? see how #including add.h, makes main.cpp aware of declaration of add(int x, int y), how find definition?


2. have got wrong in code?

my code compiling now. reason code wasn't compiling because had been going file > new > file... add files project, opposed adding them through source files , header files sections of solution explorer in visual studio. needed add #include "stdafx.h add.cpp file.

think of way: each .cpp file preprocessed , compiled separately other files.

so let's first preprocess main.cpp. involves looking @ lines beginning #. file main.cpp has #include lines, copy contents of file they're including. i'm going represent contents of stdafx.h , iostream comment, i'll copy contents of add.h in:

// contents of stdafx.h // contents of iostream #ifndef add_h #define add_h int add(int x, int y); #endif  int _tmain(int argc, _tchar* argv[]) {     std::cout << "3 + 4 = " << add(3, 4) << std::endl;     system("pause");     return 0; } 

see contents of add.h have been brought main.cpp? , happens has brought in more preprocessor directives, we'll need say. first checks if add_h not defined yet (in file), not, , leaves until #endif:

// contents of stdafx.h // contents of iostream #define add_h int add(int x, int y);  int _tmain(int argc, _tchar* argv[]) {     std::cout << "3 + 4 = " << add(3, 4) << std::endl;     system("pause");     return 0; } 

now remaining preprocessor directive defines add_h , we're left final translation unit:

// contents of stdafx.h // contents of iostream int add(int x, int y);  int _tmain(int argc, _tchar* argv[]) {     std::cout << "3 + 4 = " << add(3, 4) << std::endl;     system("pause");     return 0; } 

now file can compiled. if call function add, compiler needs able see declaration of function compile successfully. expected function defined in other translation unit.

so let's @ preprocessing add.cpp. in fact, add.cpp doesn't have preprocessing directives, nothing needs happen. typically, #include "add.h", program still compile if don't. after preprocessing still have:

int add(int x, int y) {     return x + y; } 

this gets compiled , have definition of add function.

after .cpp files have been compiled, linked. linker responsible seeing compiled main.cpp uses function add , looks definition. finds definition in compiled add.cpp , links them together.


you may wonder why have include guards @ all. seemed pretty worthless in example. that's right, in example didn't have use. include guards there prevent same header being included twice in single file. can happen when have more complex project structure. however, let's @ unrealistic example main.cpp includes add.h twice:

#include "stdafx.h" #include <iostream> #include "add.h" #include "add.h"  int _tmain(int argc, _tchar* argv[]) {     std::cout << "3 + 4 = " << add(3, 4) << std::endl;     system("pause");     return 0; } 

preprocessing gives you:

// contents of stdafx.h // contents of iostream #ifndef add_h #define add_h int add(int x, int y); #endif #ifndef add_h #define add_h int add(int x, int y); #endif  int _tmain(int argc, _tchar* argv[]) {     std::cout << "3 + 4 = " << add(3, 4) << std::endl;     system("pause");     return 0; } 

the first #ifndef process, see add_h not yet defined, , until #endif remain. defines add_h.

then second #ifndef processed, @ point add_h has been defined, until #endif discarded.

this important because having multiple definitions of function (and many other things) give error.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -