0

I have a class called Timer that contains, obviously, code to track running time. I want to include this class in another project but I can't figure out how.

I have tried using

#include "Timer.h"

as well as using the file path to the project with the timer class, i.e.

#include "/users/user/projects/TimerProject/timer.h"

But that hasn't worked either, it tells me the file can’t be found. Is there something I am missing here?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
HalfDecent
  • 19
  • 2
  • 5
  • 4
    What does "hasn't work" mean exactly, what is the file path structure here and how exactly is the compiler invoked? You will need to use the proper `#include` as well as provide the correct include and library search paths and library names to the compiler. –  Nov 18 '18 at 23:58
  • This is mostly a deployment issue. You can integrate your other project as a git submodule, or you can copy files over, or you can provide an os package for you other project, among other ways of doing it. – Jazzwave06 Nov 19 '18 at 00:00
  • 1
    You will need to expand on _but that hasn't worked_. Do you get a compiler error saying the file cannot be found, do you get a compiler error saying that a file in Timer.h cannot be found, do you get a linker error? Please [edit] your question with these details – Tas Nov 19 '18 at 00:05
  • It does tell me that the file can’t be found, editors my original post to reflect that. – HalfDecent Nov 19 '18 at 00:20
  • File not found for the second example is likely a user error. You need to figure out why the location you used in the include is wrong. For the first example you need to add an additional path to the paths your compiler uses for include paths. The answer to how may differ depending on the ide you use or how you are building your code. – drescherjm Nov 19 '18 at 00:23
  • Please provide your compiler and IDE or build system (if any), because you need to tell the compiler where to look for headers. This is usually done in the IDE or build system configuration (e.g. CMake). And please don't use absolute paths in #include paths., this is considered bad practice. – uceumern Nov 19 '18 at 14:21

1 Answers1

2

Yes. You need to tell your C++ compiler where to search for include files. For gcc or clang, this is the -I command-line switch. So, for example:

g++ -o foo foo.cpp -I/users/user/projects/TimerProject/

and this will allow you to use:

#include <Timer.h>

Using double-quotes around the include name tells the compiler: "Search the same directory as the including file first, then search the include folders the compiler knows about". So if you have a foo.h next to your foo.cpp, you could use:

#include "foo.h"

without adding anything to the include path for it.

Finally: Files are case-sensitive on many operating systems. In your example, you have Timer.h and timer.h - make sure you use the correct spelling!

See also:

What is the difference between #include <filename> and #include "filename"?

einpoklum
  • 118,144
  • 57
  • 340
  • 684