What is the difference between #include "MyClass.h" and #include ".\myclass.h" - c++

I'm developing in VS2010 and looking to add code to an already existing project.
This is a Win32/MFC by the way.
And I couldn't help but notice that in class MyClass (in this case MyClass was an extension of the CDialog Class) the following was included at the top of the cpp file:
#include "MyClass.h"
#include ".\myclass.h"
I noticed that the second include was typed in without capitalization, but I couldn't quite figure out why?

"MyClass.h"
will be searched on INCLUDE_DIR path, which is defined in your project settings.
" ./myclass.h" will be searched in the same directory than the current file.
Windows files names are not case-sensitive so if your working dir is in your include path, these lines are pointing to the same file.
This redundancy is probably a way for VS to ensure the file will be included at least once...
Edit: thanks Arne Vogel, I was tired and wrote false things.
Your compiler will look for your header files only il the file name is like #include <file.h>
But I guess that the redundancy is to be compliant with all file systems.

.\ says to look in the current directory. I'm guessing with the include guards in that header, that wouldn't be a problem.

#include "MyClass.h" is from environment path, while #include ".\myclass.h" from current path.
most of the time, "MyClass.h" in the inc directory under your project, but you MyClass.cpp in other path.

Related

How to include libraries in Visual Studio automatically?

ALWAYS when I start a new class (EX: main.cpp) I need to
#include <iostream>
#include <string>
#include <math.h>
There is a way to make it automatically ? I mean every time when I create a new class they will already be included ?
A cpp file is not a class, it's a source file. A cpp file may include a class, or multiple classes, or no classes. Similar, a header filer is not a librarie, it's just a header file.
Add your includes to a header (.h file), and then your cpp file only has to include that single header to include all those common includes. Visual Studio even has something called a precompiled header, which is exactly meant as such a header with common includes, except that it's precompiled (which means, using that will compile faster than using a regular header). Afaik, you'll still have to include that single header yourself, tho, so you won't get around writing at least one #include ...
My solution is more a workaround than a "real" solution but : You need these lines at least once in your program.
So I'd create a headers_container.hpp file containing stuff my whole program needs, like these #include.
For example :
headers_container.hpp :
#include <iostream>
#include <string>
#include <math.h>
// Some stuff my whole program needs...
in your *.cpp files :
#include "headers_container.cpp"
// Your compiler knows iostream, std::strings and math now
Make sure the path to headers_container.hpp is correct (if the .hpp is not in the same folder as your .cpp
Using this method, you can add one #include in headers_container.hpp and it will update all the .cpp files.
Moreover you can write a small script to generate files (I did the script you can find here : https://gitlab.com/-/snippets/2033889 )
Enjoy your way in programming ! :)

Include from directory C++

I am trying to run this code
http://dlib.net/dlib/statistics/cca.h.html
As you can notice, it contains many include, which I copy them.
But inside each include there are a lot of includes like this :
#include "../matrix.h"
It contains
#include "matrix/matrix.h"
#include "matrix/matrix_utilities.h"
#include "matrix/matrix_subexp.h"
#include "matrix/matrix_math_functions.h"
#include "matrix/matrix_assign.h"
#include "matrix/matrix_la.h"
#include "matrix/symmetric_matrix_cache.h"
#include "matrix/matrix_conv.h"
#include "matrix/matrix_read_from_istream.h"
#include "matrix/matrix_fft.h"
#include "matrix/matrix_generic_image.h"
Is there any method to just include the main class? For example give the directory or the link of the classes?
I bet the problem lies with your include directories.
I assume you downloaded the full zip file from http://dlib.net/ (latest version seems to be 18.18). Inside that .zip you have a bunch of folders: examples, tools, dlib. In dlib folder you have all the header files.
You should add the path to wherever you extracted the .zip contents to the "Additional Include Directories" property of your project:
Then just use dlib in your own code as showed in the examples, for instance 3d_point_cloud_ex.cpp:
#include <dlib/gui_widgets.h>
#include <dlib/image_transforms.h>
Initially there is no need to create extra header files, all seems to be provided with a nice folder structure. I encourage you to read dlib's documentation before start using it.
You might also want to check this answer to another question to help you build a handy project folder structure.

Including library with "/" character

In some pieces of code I see something like this:
#include <SFML/Graphics.hpp>
#include <Qt/qwidget.h>
What is the difference with the "/" character compared to including a header like this:
#include <iostream>
I tried googling but I couldn't find any good results.
Include directives are for including single header files. They essentially replace the directive by the file in question. A header file itself may be part of a library.
The piece before the / is a directory where the header file is placed, relative to a directory in the include search path used for compilation. Say my compiler knows to search in /usr/include, and all my boost headers are in /usr/include/boost. I can then include boost headers in my code using for example
#include <boost/some_header.hpp>
It just states that the header file is located in the directory SFML or Qt.

Question about C++ folder structure for header file

I'm quite new in C++ after few years in Java and eclipse, I got little bit confusing using code::blocks, no autogenerate setter/getter and also implement interface :D.
I wanna ask about code structure in code::blocks, I create new console application, my header will be put to Headers/include folder called Employee.h, then .cpp will be put to src folder.
Now I create main class (I put outside of src folder) who will call the header, I just append the include code like this :
#include "Employee.h"
sure then the errors appeared after compiling:
error : Employee.h: No such file or directory.
how to link the header to the main class properly?
this is my folder structure :
updated :
It works, my include folder needs to be added on build options.
Really thanks
You need to add your include directory to your compiler's include path. This is going to be compiler-specific. e.g., if your structure is:
code
code/src
code/include
and you're running g++ from a terminal in the 'code' directory, you'd need to run (assuming your .cpp is Employee.cpp):
g++ -Iinclude src/Employee.cpp
I suspect you're running some sort of IDE, though: if so, do a search in its help for "include path" and it should tell you how to set it up correctly.
If you want to include your employee.h you must #include "employee.h" not Employee.h. Those are two different files.
You shouldn't be adding include paths to your build options for header files that are actually part of your project. It didn't find the header file from the EmployeeTest.cpp because you didn't use the full relative path.
You need:
#include "include/Employee.h"
You should only be adding include paths to your compiler for additional libraries that aren't added to the typical /usr/local/include or /usr/include directories.

#including a header from the previous directory (../)

Here is what I'm trying to do.
I have a folder called Agui which is the lib's folder. In that folder there is another folder called Widgets. I want a file from Agui/Widgets to #include base.h from Agui folder. How should I do this so that it remains cross platform? Should I simply include <Agui/base.h> ?
Thanks
#include "../base.h". And yes, that is portable.
You can use:
#include "../base.h"
From your Agui/Widgets folder. It should work. It should be cross platform.
Better is
#include "Agui/base.h"
or
#include "../base.h"
depending of whether your library root or current folder are added to the include search path.
Angle brackets are reserved for system libraries (although one can actually use any kind of them).
Its better to have the path setup in $PATH and in $LD_LIBRARY_PATH. By doing the above, you can just refer to the header file :
#include <base.h>
This will help to configure/run in any platform