0

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

Simon Schüpbach
  • 2,625
  • 2
  • 13
  • 26
  • Doubt you can, as they use the same search path – Chris Turner Jan 22 '18 at 12:52
  • 2
    Be sure to very carefully read the [second](https://stackoverflow.com/a/77092/577603) and [third answer](https://stackoverflow.com/a/50266/577603) in [this question](https://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename). After that, you are hopefully convinced that what you are trying to do here is a really bad idea. – ComicSansMS Jan 23 '18 at 08:53

1 Answers1

0

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.

273K
  • 29,503
  • 10
  • 41
  • 64
  • Thank you very much fro the reply. I update my question with a currently use case. I don't want to rename/move my files!!! – Simon Schüpbach Jan 22 '18 at 14:51
  • You have created a problem for yourself from scratch. Try to think how do you solve this problem using the compiler in the command line, is it possible to solve it? – 273K Jan 22 '18 at 15:25