-2

I'm writing a program in c++, and this program has two parts, say part A and part B. These parts are in different directories. The codes in B use some classes in A. A's directory name is inconsistent, so whenever the directory has changed, I have to reset the included classes in B. What is the best idea to reduce the number of resets in part B?

For example,

Class One is in A.

/* some codes here */
class One{

/* definitions */

}

Classes Two and Three are in B

#include <path-to-class-One/One.hpp>
/* some codes here */
class Two{

/* definitions */

}
#include <path-to-class-One/One.hpp>

/* some codes here */
class Three{

/* definitions */

}

If <path-to-class-One> changes, I must change the path in classes Two and Three. I want to reduce these changes or, if possible, reduce it to ZERO times!

fva
  • 33
  • 4
  • On Linux, use symbolic links. – Sam Varshavchik Jul 15 '22 at 12:33
  • 3
    include relative paths `#include "One.hpp"` and add the paths to your include path, then you need to change it in only one place – 463035818_is_not_an_ai Jul 15 '22 at 12:40
  • editors have search-and-replace that requires a single click for many edits – 463035818_is_not_an_ai Jul 15 '22 at 12:42
  • The base of project A and the base of project B should be set as include path in your build toolchain. Include relative to these. If they move there's a single base to edit in your build settings – Jeffrey Jul 15 '22 at 12:43
  • 1. Use some build manger/generator (cmake for example). 2. Both parts should be a library 3. Library B should link library A 4. Library A should expose its public include path. After that if you move library A around (as a whole thing), nothing have o be updated since cmake will take care f it. – Marek R Jul 15 '22 at 12:45

1 Answers1

1

when using include you should not give a full or relative path to the h file, but just write the file name. in your example:

#include "class_a.h"

(notice an included file which is not from a library should use "" and not <>, you can read about it here"

now to make the compiler find the included file it depends how you compile your code.

  1. compile in terminal with gcc command: just add the -I flag

    gcc source_file_name.cpp -I <path_to_included file_directory> -o exe_name.out

  2. use makefile/cmake: add a flag that says where the included files are

robb218
  • 23
  • 4
  • "should only write the file name" is not the best recommendation. Organizing files in folders is pretty useful and not always you want to add all subfolders to the include path but only the top folders – 463035818_is_not_an_ai Jul 15 '22 at 12:54
  • I agree. Usually, there is one include directory for a project. if there are more sub directories, then a relative path from the main include directory should also be written. – robb218 Jul 15 '22 at 13:10