-1

I m trying to understand how an IDE (I'm using Visual Studio) knows where to find the implementation for a header file's declarations.

Let's say that I have 2 cpp files and 1 header file:

  • main.cpp, contains main() function and includes person.h
  • person.h, contains some class declarations that are implemented in person.cpp
  • person.cpp, contains the implementation of the person.h declarations, and also defines person.h

So my understanding is that main.cpp and person.cpp know where to find the function declarations, but person.h has to search in some .cpp file for these implementations.

Now, how does Visual Studio keeps track of this? Each time a new header file is created, does VS needs to parse every .cpp file in the project to find where the declarations are implemented? Header files don't have any declaration of which .cpp files to search for, yet it finds them! Is Visual Studio scanning each .cpp file in the project?

So, what happens in a large project, with hundreds, or thousands of .cpp files? This does not appear to be very efficient, so I figure VS must do it in a different way. Do you know how?

PS: Not about compiler/linker process, but rather how VS IDE works (even before compiling or link the final exe)

J_PT
  • 479
  • 4
  • 22
  • `person.h` doesn't search for anything, it just holds declarations, which the code in `main.cpp` and `person.cpp` use. The **compiler** simply takes note of the declarations as it sees them and uses them where needed while compiling each `.cpp` file. The resulting `.obj` files contain machine code with unresolved references to any outside functions and class methods that were used. After compiling is finished, the **linker** then brings everything together, matching up the references to the appropriate `.obj` files so it can fill in the references in the final executable. – Remy Lebeau Apr 19 '18 at 22:52
  • I was more interested on how VS handles this parsing, even before using the compiler and linker – J_PT Apr 19 '18 at 22:54
  • What parsing are you referring to? There is only the preprocessor, compiler, and linker stages. The project tells the preprocessor and compiler which `.cpp` files to handle, and tells the linker which `.obj` files to link with. That's all. `.h` files are processed by the **preprocessor** whenever an `#include` statement is processed, and then the **compiler** processes the code that results from the **preprocessor** replacing the `#include` statement with the actual contents of the `.h` file. – Remy Lebeau Apr 19 '18 at 22:54
  • Well when you re editing cpp files and header files, the IDE will alert you if you re missing the implementation of any declaration without compiling the project/file, or if you miss some signature, variable etc.. – J_PT Apr 19 '18 at 22:57
  • I don't use VS, so I don't know what functionality you are referring to. But it is not uncommon for an IDE to have a built-in slimmed down compiler to parse the code as you are writing it, looking for errors, invoking auto-completion, etc. – Remy Lebeau Apr 19 '18 at 23:00
  • Your project file has a list of cpp files to actually compile. They should #include the relevant headers, which they find on the include path. Check project properties for include path. – Kenny Ostrom Apr 19 '18 at 23:27

1 Answers1

2

Intellisense is the name of the Visual Studio feature which links definitions to declarations; which lists completions when typing identifiers; and which draws red squiggly lines under errors.

Intellisense works by continually scanning every source file in a solution, including new definitions as you type them. Records about all of the constructs in the source code are stored in a database, which is located on disk in a "solution_name.VC.db" file. When a new definition or declaration is added, whether to a header or source file, Intellisense doesn't have to take very long to look up any related items in the database.

If you delete the .VC.db file, and then open the solution, it will take a noticeably long time for Intellisense to rescan everything before it starts working very well. The IDE will also tell you that it is "parsing" files in the status bar.

Jack C.
  • 744
  • 3
  • 7