1

I am wondering how would I reference a file from another project in C++.

For example, I have moved avocado.h from my Vegetable project to Fruit project. In fruit.cpp, I am trying to include avocado.h, but the compiler cannot find avocado.h.

Original recipie.cpp:
    #include "avocado.h" //error c1083: Cannot open included file: 'avocado.h':No such file or directory

My existing solution is to use namespaces, but I am wondering if it will be much more efficient for me to somehow tell #include exactly where to look?

Current recipie.cpp:
namespace fruit {
    class avocado;
}

EDIT - Sorry, I should mention that I am working in a Solution (or library) where both Fruit and Vegetable projects are under the same solution.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
Jon
  • 8,205
  • 25
  • 87
  • 146
  • If you want to create two or more projects that share a file, consider creating a library that the projects can use. –  Mar 24 '17 at 22:54
  • 1
    There are ways to set the include directories (depending on your compiler) which are the directories the compiler looks for the header files. Additionally you can use relative paths such as `#include "../Vegetable/avocado.h"`. – nwp Mar 24 '17 at 22:56
  • @nwp Is there a way to use an absolute path? – Jon Mar 24 '17 at 22:57
  • The compiler will have flags to tell it where to look for headers. Like `GCC` does this `g++ -I/path/to/header/dir ...` – Galik Mar 24 '17 at 23:00
  • 1
    Apparently [you can use absolute paths](https://stackoverflow.com/questions/12562807), but it is a really bad idea because it locks the project into essentially only working on your computer. – nwp Mar 24 '17 at 23:08

1 Answers1

2

Since you've moved the header from one project to another, you should probably move it physically, as well, in same directory as recipie.cpp. Alternatively, you can change the Fruit project properties: Configuration Properties \ VC++ Directories \ Include Directories to include the path to avocado.h

GreatDane
  • 683
  • 1
  • 9
  • 31