Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ How to use a class from another project

I've been searching for how to do this for nearly an entire day.

At first, I thought that this can be done by... Right click the project name -> "Add files" -> choosing a .cpp file which contains the class you need and the corresponding header file.

Then, the .cpp file and header file appeared with its original folder. After this, I wrote #include"random.h" on the project which needed to use random.h and its functions.

However, this produces an error, indicating that fatal error: random.h: No such file or directory. The compiler apparently can't find the file (even though I can).

I add a picture of this.

enter image description here

Also, I've been looking for how to add .cpp & header files without its folder. (In the picture above, for example, you'll see that random.cpp inside Using_a_class_test is included in a folder named Random. To my shame, I haven't found how to eliminate such a folder.)

I'd appreciate if you'd offer any insight.

like image 780
Hiroki Avatar asked Mar 17 '23 18:03

Hiroki


1 Answers

Unfortunately, what you did is not enough. When you try to compile

#include "random.h"

The compiler needs to know where the random.h file is, and for that it uses the include path info, which is unrelated to the files you included in the project.

A couple of solutions:

  1. You modify all occurrences of #include "random.h" to be #include "/path/to/random.h"
  2. You modify the include path information for your project. Go to Project >> Build options, select the tab "Search directories" and add all the paths to your .h files there.

Hope this helps.

like image 117
Baltasarq Avatar answered Mar 24 '23 13:03

Baltasarq