1

In the following files:

app/main.cpp
app/config.hpp
app/show.hpp
app/file.hpp

lib/append.hpp
lib/clear.hpp
lib/reopen.hpp
lib/crypt.hpp

I have a problem. Imagine a few of my files use lib/crypt.hpp. For example in app/file.hpp I should write:

#include "../lib/crypt.hpp"

If I take this file to lib folder it does not work anymore. So I need a something like:

#include "LIB/crypt.hpp"

So, this LIB/ has meaning of (empty) in lib directory while it has meaning of "../lib/" in app directory.

Then I will have no worry about moving file.hpp from app to lib. I just need to fix the callers of it. But not what it calls.

Similar to concept of web programming frameworks. Is it possible in C/C++?

ar2015
  • 5,558
  • 8
  • 53
  • 110
  • You can not use macro as a part of the path, but you can make a macro that expands to the complete string `"../lib/crypt.hpp"`. – Some programmer dude Mar 31 '15 at 08:34
  • 5
    This sounds complicated and unnecessary, if I understood correctly, I would just include the lib's parent path to the include directories and then use `#include ` – Marco A. Mar 31 '15 at 08:35
  • @JoachimPileborg any way is ok. I am looking for a similar concept to the last line from [this code](https://github.com/bcit-ci/CodeIgniter/blob/develop/index.php). – ar2015 Mar 31 '15 at 08:39
  • @MarcoA. my program is prone to have a lot of changes and fixing the codes because of moving the files at first is fine but after a while it gets boring. my code was like what you say at first. but the problem is that i want each file works fine without including extra stuffs. – ar2015 Mar 31 '15 at 08:40
  • @ar2015 if you do as I wrote, unless you move the lib headers around, you should be fine with the other files that are part of the project – Marco A. Mar 31 '15 at 08:41
  • 2
    Standard solution is to use either `#include "crypt.hpp"` and compile with option `-I../lib`, or use `#include "lib/crypt.hpp"` and compile with `-I..`. – Marian Mar 31 '15 at 08:45
  • @Marian I think `-I` will fix my problem. Thanks. – ar2015 Mar 31 '15 at 08:51

1 Answers1

1

According to what you wrote you're searching for a way to move your sources around without worrying for hard-coded paths to your headers.

You didn't specify the compiler you're using so I'm not focusing on a specific one, anyway most C++ compilers have an option to specify one or more header search paths (on gcc you would just use something like -Ilib_parent_dir).

Once you've included your lib's parent path to the search list, you can move your sources around (as long as you don't move lib's headers) and have them include the right headers with something like #include <lib/crypt.hpp> (and keep include paths priority in mind)

This should be a cleaner and simpler way to achieve what you asked rather than using a macro.

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246