1

I have been curious about the difference between including a file in another source file with #include filename.h and "adding" a source to a C++ project.

In Visual Studio, adding a source file to a project is done by right clicking and choosing "add existing source to project".

We don't need to do that to a file that is in the "additional directories" path and included using #include.

However, sometimes .cpp files which are not #included need to be added to the project.

So:

When is it that a .cpp file needs to be added to my project?

Also, W/R/T to CMAKE:

When I specify include_directories and point it to the path where I my files to be included are, and those files are referenced in the source, why do I also have to add every header with add_exectuable?

In other words, those files are included with #include and CMAKE knows where to look for them, so what does setting add_executable do?

Startec
  • 12,496
  • 23
  • 93
  • 160
  • 1
    I do not use Visual Studio, I only use CMake (and I am not an expert, I am sure someone can explain it better). The files you include using `#include` are just definitions generally. You need to create. When the compiler finds the function call it tries to link it. To link it you either need the binary or the `.cpp` with the implementation of the function. Maybe visual studio does some magic to automatically compile this, but CMake does not. This is why you need to `add_executable` or `add_library`in CMake. – apalomer Oct 24 '17 at 20:51
  • 1
    As explained at https://stackoverflow.com/a/13703725/1885269, you should pass the headers as arguments to add_executable just to be able to see them under Header Files in your Visual Studio solution. I'm personally using CMake 3.8.12 and still there is no way to circumvent this weird bahaviour. – Yann Oct 24 '17 at 22:07

0 Answers0