0

I need to run a demo program, ba_demo.cpp, which is in a folder structure, "C:/root/demo_parent1/demo_parent2/demo.cpp", and this file uses the following code in it.

#include "root/sub1/sub2/header_file.h".

The file is at

'C:/root/sub1/sub2/header_file.h'

When I try to compile the demo program by the command

C:\root\demo_parent1\demo_parent2> gcc demo.cpp

it is not finding the header file and is throwing an error. What changes should I make to my command in order to run the demo program successfully?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
User1234321232
  • 517
  • 10
  • 25

2 Answers2

3

First you must inform your compiler where to look for root directory by adding flag -IC: so your command will look like this:

C:\root\demo_parent1\demo_parent2> g++ demo.cpp -IC:

But that's not practical. Better change include to

#include header_file.h

and add only /root/sub1/sub2/ directory, which will look like this:

C:\root\demo_parent1\demo_parent2> g++ demo.cpp -IC:\root\sub1\sub2\

Now you have your program compiled to a.out file. You execute it by

C:\root\demo_parent1\demo_parent2>./a.out

You can change output name to demo.exe like this:

C:\root\demo_parent1\demo_parent2> g++ demo.cpp -IC:\root\sub1\sub2\ -o demo.exe

Please note that since you work on Windows under some &nix simulator the slashes in my examples might be wrong but I can't check them now. Use google to check how to handle them properly.

EDIT: Also, don't forget to take Lukas Holt's advice and use g++! It is very important to use proper compiler for proper language.

Darth Hunterix
  • 1,484
  • 5
  • 27
  • 31
  • If the slashes are a problem, usually syntax like this works /c/root/sub1/sub2/ in cygwin bash shells and things. – Lucas Holt Jul 01 '14 at 22:48
  • 1
    I have never worked under Cygwin, and last time I worked under MinGW I had slahes totally mixed up and everything worked anyway. – Darth Hunterix Jul 01 '14 at 22:50
1

Your .cpp file is in: C:/root/demo_parent1/demo_parent2/demo.cpp

You include:

#include "root/sub1/sub2/header_file.h"

This is a relative path. You need either to specify an include directory with the -I flag, or to move header_file.h to C:/root/demo_parent1/demo_parent2/root/sub1/sub2/header_file.h. The most simple solution would be to just #include "header_file.h" and to move it into the same directory as demo.cpp.

Then you will be able to compile it with gcc, and to execute the generated file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MayeulC
  • 1,628
  • 17
  • 24