Cmake include path for system and program files - c++

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.

Related

How to include all header files with one include in c++ project?

I have a folder containing header (.h) and c++ (.cpp) files.
Folder/ --- file1.h
--- file1.cpp
--- file2.h
--- ...
I have a main.cpp file located in the same directory as the Folder or project (to become library).
// main.cpp
#include "./Folder/pointtoallheaders.h"
How can I include one header file that then includes all of the header files associated with the project?
Unless I'm missing somenthing it's quite trivial
You can have a header file in the same folder of the main.cpp that includes all the others:
For instance allheaders.h
#include "Folder/header1.h"
#include "Folder/header2.h"
#include "Folder/header3.h"
#include "Folder/header4.h"
Then in your main function just include allheaders.h
#include "allheaders.h"
int main(){
}
Create one header file that will import all header files in your project and then import this specific file from the cpp file
Using cmd in windows you can create single header file. this new header file should include all your old header file. Just add the new allheader.h into the main program.
FOR %i IN (*.h) DO echo #include "%i">>allheader.h
if you have folder path to add, then the above code can modify with folder name
FOR %i IN (*.h) DO echo #include "FolderName/%i">>allheader.h
Further you can create a bat file for the same process just copy and paste the above command into text file and rename with .bat extension.

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.

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.

Can't open header file in C++

I have a header file called simpio.h, which is in the same folder as the file which is including it in its header. However, I keep on getting the error "Cannot open include file: 'simpio.h': No such file or directory." I am using Visual C++ 2008 Express Edition. Help would be appreciated.
Thanks
You need to use double quotes:
#include "simpio.h"
You have to know that you should use <> when you are trying to include a standard library header or when you want to include a file for which the path is contained in the Additional include directories option.
You have to use "" when you whant to include a file who doesn't satified the previous explanation, let's say that it is almost always file specific to your project.
Some example :
#include <iostream> // library file
#include <boost/thread.hpp> // there is "C:\SDKS\boost in my Additional include directories
#include "MyHeader.h" // Local file, at the same place of my .vcproj
#include "Header/AnotherHeader.h" // Local file in a folder named "Header"
In your case, we can think that you are in the second case. You just have to do :
#include "simpio.h"
Or if your file is in another folder :
#include "SomeFolders/simpio.h"

C++ Multiple Files & Including Headers

I've been trying to figure this out by myself for months, and that's not an exaggeration.
I feel dirty using the methods I am not since things tend to break and get out of scope when they get complex and I cannot find an answer to this anywhere.
I have a project structure as follows:
Project-Directory/
main.cpp
makefile
SDLMain.h // SDL libraries required to be in the project directory.
SDLMain.m // SDL libraries required to be in the project directory.
--- gamestate/
------ clean.cpp
------ gamestate.cpp
------ gamestate.h
------ init.cpp
--- graphics/
------ graphics.h
------ // more .cpp files
--- logic/
------- logic.h
------- // more .cpp files
--- character
------- character.h
------- // more .cpp files
In main.cpp I have:
// C++ Libraries
#include <iostream>
// Graphical Libraries
//#include <SDL/SDL.h>
//#include <SDL/SDL_opengl.h>
// Custom Libraries
#include "character/character.h"
#include "logic/logic.h"
#include "graphics/graphics.h"
#include "gamestate/gamestate.h"
All the .cpp files in character and graphics etc... include their respective header file which shares the same name as the folder. I.e. clean.cpp, gamestate.cpp and init.cpp and all include gamestate.h.
In each folder there is only one header file, recently reorganised from 1 header file per .cpp.
Basically on this new, more structured system I get scope errors when I attempt to compile my project.
Why is that so if my headerfiles are being included after #include <iostream> and the SDL libraries in main.cpp.
I solved the error by inserting this into all header files:
// C++ Libraries
#include <iostream>
// Graphical Libraries
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
But then I am including the same thing over and over again and surely this is bad practice.
Not only this, but gamestate.h contains a prototype used by gamestate.cpp to a function in logic.cpp which gets a scope error unless I implicitly include logic.h in gamestate.h even though logic.h is included in main.cpp before gamestate.h.
I thought #include is meant to drag the contents of header files into scope and the functions it prototypes so the compiler knows what to expect.
Why am I getting all these errors about scoping and functions not existing?
Should I make a global.h and #include all the SDL and <iostream> stuff there?
Why can't I access the function prototyped in logic.h from another file included afterwards in main.cpp?
This is sort of a "you can do it many ways, and none are completely right or wrong" question.
But technically, a source file needs to include all headers that it depends on. So if "gamestate.cpp" needs something in "logic.cpp", then "gamestate.cpp" needs to include "logic.h". If EVERYWHERE that uses "gamestate.h" also needs "logic.h", then "gamestate.h" probably should include "logic.h", but I have worked on systems where the rules are: If you are going to use "gamestate.h", you must include "logic.h" first. Note that "gamestate.cpp" is not compiled inside "main.cpp" (unless you commit the heinous crime of including "gamestate.cpp" inside "main.cpp" - but please don't do that).
I like it when you can just use a header file directly, without having to remember the list of header files you must add before it.
Using a "global.h" is probably a bad idea.