I know that this is a very popular question with a lot of threads already about it, but unfortunately after trying some of the suggestions I didn't manage to solve my problem, so I am posting a new question about it. What I am interested in, is running a c++ program in terminal, the thing is, as I was always using eclipse to compile and run programs I am completely noob on this and also my ubuntu knowledge is limitated to very basic things. So what I am finding difficulties in is when I want to compile a program which needs to include some header files form different directories. What I want to do is to include this libraries without needing to add in the #include "..." the whole path of where the library is but just it's name. In Eclipse this could be done by adding paths through options, but in Ubuntu I am not really sure how to do so, following some instructions in other threads I tried to add the paths to my ~/.profile txt file but without any success. So I would like to ask how is this possible to be done, i.e. where and how to add the paths of the header files needed so that I want have all the time for very new project to provide the entire path in my includes.
1 Answer
You add the path with -I
arguments to g++
for example,
$ cat /home/efrisch/t/myheader.h
#include <iostream>
$ cat hello.cc
#include "myheader.h"
int main(int argc, char **argv) {
std::cout << "Hello, World!" << std::endl;
}
$ g++ -O -I/home/efrisch/t hello.cc -o hello
$ ./hello
Hello, World!
-
Thank you for the answer, but this wouldn't be of much help to me. I need a way to include for example the "/home/efrish/t/myheader.h" without having to write the path inside the programme this is because I have hundreds of header folders I need to include, so I just need a way to include the paths of where the header files are Mar 19, 2014 at 20:13
-
@user3111197 Yes. You pass them on the command line to g++ like I demonstrated. "/home/efrisch/t" is a different folder from where
hello.cc
is. Mar 19, 2014 at 20:16 -
Ok now got it, one more really stupid thing (I am not in my machine with ubuntu and I can't try them myself that's why I have to ask the following stupid thing as well) about the syntax for including more than one folders say "/home/efrisch/t" and "/home/efrisch/s" the correct syntax for it is: $ g++ -O -I/home/efrisch/t -I/home/efrisch/s hello.cc -o hello ? Mar 19, 2014 at 20:35
-
@user3111197 Yes! If you have the same include in both folders
t
ands
thent/whatever.h
will shadows/whatever.h
; so you might reverse the order. Mar 19, 2014 at 20:38 -
Ok, thanks a lot for the help I will try it tomorrow and see if it works. Mar 19, 2014 at 20:59