0

I have a pretty basic question. If I have a .cpp and .h file in a different location than my project, how can I reference it by saying #include " ".

I am trying to use wxMathPlot.cpp/.h and it references wxWidget cpp files. mathplot.cpp(19): fatal error C1083: Cannot open include file: 'wx/window.h': No such file or directory

So say my wxMathPlot.cpp is located in C:\Users\Owner\Desktop and my wx/window.h is in C:\Users\Owner\Documents

Zaffy
  • 16,801
  • 8
  • 50
  • 77
user2840470
  • 919
  • 1
  • 11
  • 23
  • [This SO question](http://stackoverflow.com/questions/12562807/include-absolute-path-syntax-in-c-c) has a nice answer. – Jashaszun Jul 08 '14 at 19:09
  • 4
    The keyword here is *library*. You need to learn about C++ libraries and how to use them in your particular development environment. You are wrong in assuming that this is just about referencing source files. You must compile the library's sources separately (or use a prebuilt binary), reference the headers by modifying the include path of your compiler and finally link against the binary. Nothing of this is trivial stuff for a beginner, and a complete guide on C++ libraries is out of scope for a single SO answer. – Christian Hackl Jul 08 '14 at 19:14
  • Always favor config for this, it is very unlikely to stay in the Desktop folder. Project + Properties, C/C++, General, Additional Include Directories setting. The linker has one too. – Hans Passant Jul 08 '14 at 19:27

3 Answers3

4
#include "../Documents/wxMathPlot.h"

Should work. To elaborate:

When you use an include such as #include "header.h" the same directory as the file is searched.

When you use an include such as #include <header.h> a specific directory is searched, chosen by your compiler, which is where you will find most standard library header files.

Greg Hilston
  • 2,397
  • 2
  • 24
  • 35
1

You can reference it by using its full path or by referencing through one or more ..'s in your path (that means "go up one level"), or you can specify the directory in which the header file resides in the 'header file search path' (the 'include path') and then just use the filename.

However, using the full path is not recommended because if you move the header file relative to the file it is being referenced from, then it will not work anymore.

Look at this SO question for a nice answer as well.

Community
  • 1
  • 1
Jashaszun
  • 9,207
  • 3
  • 29
  • 57
0

For CPP files you need add those files in your project. If you are using Visual Studio you can add the cpp file by right clicking on your working project and selecting add existing item. If you want to refer a .h file you need include this, e.g

#include "../Documents/wx/Windows.h"

It is always good to use relative path rather than using absolute path.

Naseef Chowdhury
  • 2,357
  • 3
  • 28
  • 52