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?

5
  • 1
    First off, you aren't executing anything with gcc demo.cpp, that would compile it (creating an executable program). Then you'd need to run it. That said, do you have a file header_file.h in `c:\root\demo_parent1\demo_parent2\root\sub\sub2`? Commented Jul 1, 2014 at 22:39
  • @nerdwaller yes..i do have a header_file.h..but in C:/root/sub1/sub2 Commented Jul 1, 2014 at 22:40
  • 1
    Where is your header file on the path though Commented Jul 1, 2014 at 22:41
  • @LucasHolt how can i set the path? Commented Jul 1, 2014 at 22:43
  • 2
    You could try using the -I flag to gcc to specify the path to look for header files on. Also, since this is a C++ program you probably want to use g++ rather than gcc. Commented Jul 1, 2014 at 22:43

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If the slashes are a problem, usually syntax like this works /c/root/sub1/sub2/ in cygwin bash shells and things.
I have never worked under Cygwin, and last time I worked under MinGW I had slahes totally mixed up and everything worked anyway.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.