absolute directory path for include "xxx.h"

Hi guys:

This thread is related to c++ coding under linux as well as netbeans ide under ubuntu.

First, I know that
#include <xxx.h> will get the compiler to look for the file xxx.h in a system folder and #include "yyy.h" will get the compiler to look into the local project folder for yyy.h

Look at a typical unix/linux project, which consists of:
1. a project root folder called "myproject"
2. within "myproject", there's a Makefile, a README, a subfolder called "src"
3. within "src", we have all .cc files and all necessary user-defined header files.


Okay, so far so good. If I edit the .cc files in vim and try to include header files, there's no question at all. Assume we have a "main.cc" source file in "src" folder, then a line of code in "main.cc" is going to be:
#include "src/user_defined_header_1.h"

It compiles and works just fine.

Here comes the questions:
Question 1: when I use Netbeans (I'm using Ubuntu 9.10, by the way) to import "myproject", I have a netbeans project. The weird thing is, netbeans tells me "Cannot find src/user_defined_header_1.h" although the file is out there. When I change the line of code to #include "user_defined_header_1.h" , no complaints are given by netbeans. So what causes the problem? How can I configure Netbeans to let it know I'm using the absolute path of the project?

Question 2: Is this really a problem? I mean, I used to live without IDEs, but now want to make a change and decided to start with Netbeans. Most of my projects are given in the non-ide form, meaning that most of my partners don't use IDEs. I don't want to change the absolute path into relative path for sake of Netbeans every time I edit the code, then change the relative path back to absolute path for consistency in style with my partners. If you have any suggestions or comments, please share with me.


Cheers, and I'm looking forward to any potential input.
Okay, so far so good. If I edit the .cc files in vim and try to include header files, there's no question at all. Assume we have a "main.cc" source file in "src" folder, then a line of code in "main.cc" is going to be:
#include "src/user_defined_header_1.h"
I don't understand how do you compile that.
If main is inside src, then the path is just "./user_defined_header_1.h". But you are saying, go to src/src/
In general for C/C++ header file inclusion there is a technique some developers adopt and I personally like it.

That is in all your C/C++ program you always reference "current" directory

E.g
#include "A.h"
#include "B.h"
#include "C.h"

Please note all above .h files may not reside in this SAME directory so if you compile your program you will get an error. Now the technique is usually we have Makefile for our C/C++ program. Inside this Makefile you put in commands to do a symbolic link of those .h files to our current directory before doing the actual compilation. In this way, A.h B.h C.h will FIRST appear in our current directory before we do compilation for our programs.

Since it is a link, if there are changes in A.h B.h C.h in their own directory, our Makefile will automatically detect the latest before compiling our program. A simple strategy and I quite like it's simplicity.
@ne555:
Sorry about the confusion. In the Makefile, I use the option -I. Example:
1
2
main.o:	src/main.cc
	$(CXX) $(CFLAGS) -I. -c -o $@ $<


I see where your confusion is. In this particular example, I just need #include "header_1.h" if without the -I option. There's no need to use "absolute" path (not really system level absolute; relative to the project root directory). The example could be more clear if : we have two subfolders --- "src" for .cc files and "includes" for user defined header files. Then I guess it becomes necessary to use "absolute" path if we don't want crap like #include "../includes/header_1.h"

Sorry again. My bad.


@sohguanh:
It's a good idea. I like it as well. But the thing is, I'm usually responsible for a part of the project. The typical scenario is, I get a package, which contains work of other people. I can't impose my personal habit or preference on it in order to avoid inconsistency in style. My team leader will spit on me if I do so :).

My major question is: how can I make Netbeans understand that the project is using the "absolute" path relative to the project root directory?

Thanks.
Topic archived. No new replies allowed.