Include from directory C++ - 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.

Related

Is there a way to include paths in the macros itself in CLING?

I have been using Root built on CLING technology during my internship. I know I can include header files after including the path to it.
.I C:\Me\Code\
.x includeFiles.c
where includeFile.c lies in macros folder:
#include "h1.h"
#include "h2.h"
But this too is a bit of a task. I want to set the include path command in macros itself.
After reading the doc, I tried doing this but it says : "gSystem not found!"
gSystem->AddIncludePath("C:\Me\Code\");
#include "h1.h"
#include "h2.h"
Please help me with this .. !

Why won't cmake permit <> style include?

I am very new to cmake, but I am using it on Visual Studio to develop a program that has to run on linux. I need to include the following in this manner:
#include <xscontroller/xscontrol_def.h>
#include <xscontroller/xsdevice_def.h>
#include <xscontroller/xsscanner.h>
#include <xstypes/xsoutputconfigurationarray.h>
#include <xstypes/xsdatapacket.h>
#include <xstypes/xstime.h>
#include <xscommon/xsens_mutex.h>
However, the files are only recognize by visual studio when I do the following:
#include "xscontroller/xscontrol_def.h"
#include "xscontroller/xsdevice_def.h"
#include "xscontroller/xsscanner.h"
#include "xstypes/xsoutputconfigurationarray.h"
#include "xstypes/xsdatapacket.h"
#include "xstypes/xstime.h"
#include "xscommon/xsens_mutex.h"
The structure of my project in VS is fairly simple:
ANT
-out
-xscommon
-xscontroller
-xstypes
-ANT.cpp
-CMakeLists.txt
.
.
.
The includes I need are in the three xs folder, and I believe they have to be referenced with <> both in visual studio and when the code is compiled onto linux, as the references within each header are done in <> form, which is what causes this error:
xscallbackplainc.h:68:10: fatal error: xstypes/pstdint.h: No such file or directory
#include <xstypes/pstdint.h>
^~~~~~~~~~~~~~~~~~~
at compilation.
Concisely, I really just need to know what command (whether it be in CMakeLists.txt or somewhere else) will allow this kind of referencing within the project and the compiled project over ssh on linux. I am aware of the difference between #include "" and #include <>, I am however new to cmake, and have looked everywhere and cannot find an answer.
The simplest way to achieve this is using include_directories command. Simply add the following to your ANT/CMakeLists.txt:
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
Though I would highly recommend using target_include_directories() instead. The difference between the two is that target_include_directories() specifies include directories just for one target[1].
[1]. A target is anything specified via add_executable() or add_library():
cmake_minimum_required(VERSION 3.12)
project(ANT)
add_executable(ANT ANT.cpp) #other source files as necessary
#format of target_include_directories:
# target_include_directories(target_name PUBLIC|PRIVATE PATH_TO_DIR)
target_include_directories(ANT PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
I have posted the question about linking, hopefully it makes sense. Should be clear I don't know what I'm doing.

Cmake include path for system and program files

I'm wondering if there is a possibility to configure cmake, that it's possible to make a difference if headerfiles are included with #include<...> or #include "..."?
Lets assume that I have a project like this:
src
foo.h
foo.c
lib
foo.h
foo.c
How can be achieved to add the file src/foo.h by writting #include "foo.h" and if the headerfile is included with #include <foo.h> the file at location lib/foo.h is loaded?
It always load the files from the first include_directories() command
include_directories(src)
include_directories(lib)
EDIT
My current problem inside my project:
I have a file named string.h. Which contains some function for custom text handling. Now I have the problem that if I write #include <string.h> my custom file is loaded, but I expect the system file string.h. I don't want to rename my custom file nor I want to move it inside a folder to include it like #include "custum/string.h"
All I want is:
#include <string.h> // load file from system library
#include "string.h" // load my custom file
I used it inside IDE Keil like this ways, but I don't know if it can be done with cmake
Change include order
include_directories(lib)
include_directories(src)
You will need to place your whole code in the src. In this case #include "foo.h" starts search files in the same directory where current file is parsed. #include <foo.h>" starts search in include directories in the order of include_directories.
BTW it is a bad practice to do such things. Your files should be searched first and then lib files. Better way is to do like
include_directories(src)
include_directories(parent_of_lib)
Use #include "foo.h" or #include <foo.h> for your own included files and #include <lib/foo.h> for third-party included files.

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

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.

namespace boost has no member

I downloaded the latest version of Boost libraries 1_60_0 and I tried to use it but I quickly ran into troubles.
boost::unordered_map<int, int> map;
This piece of code says "namespace boost has no member unordered_map". I checkd the file, it is there though. The same happened for basically everything I tried to acces from the boost namespace.
Header includes are as follows:
#include <D:/IP/boost_1_60_0/boost/graph/adjacency_list.hpp>
#include <D:/IP/boost_1_60_0/boost/graph/graph_traits.hpp>
#include <D:/IP/boost_1_60_0/boost/graph/connected_components.hpp>
#include <D:/IP/boost_1_60_0/boost/unordered_map.hpp>
#include <D:/IP/boost_1_60_0/boost/graph/floyd_warshall_shortest.hpp>
#include <D:/IP/boost_1_60_0/boost/numeric/ublas/matrix.hpp>
#include <D:/IP/boost_1_60_0/boost/numeric/ublas/io.hpp>
I'm guessing I should include something more, but no clue what. Any tips?
unordered_map.hpp includes other boost header files this way:
#include <boost/config.hpp>
Which means that the boost folder has to be set as an additional include directory for this to work.
I'm assuming you're compiling on MSVC, if so, right click your project : properties -> C/C++ -> General and add the folder D:/IP/boost_1_60_0/ as Additional Include Directory.
The docs also answers this question for you.