-
June 18th, 2009, 06:38 AM
#1
spreading source across folders
i am having trouble with getting source files to compile when they include headers in different folders.
eg.
src/folderA/constants.h:
Code:
#define constant1 51
src/folderB/program.c
Code:
#include "constants.h"
#include <iostream>
int main () {
cout << constant1;
return 0;
}
src/makefile:
Code:
program: program.o
$(CC) program.o -o program
program.o: src/folderB/program.c
$(CC) -c src/folderB/program.c -o program.o
This won't compile since program.c doesn't know where constants.h is,
-
June 18th, 2009, 06:56 AM
#2
Re: spreading source across folders
Originally Posted by rowdy
src/folderA/constants.h:
Code:
#define constant1 51
src/folderB/program.c
Code:
#include "constants.h"
//...
This won't work, because you're telling the compiler to look for the constants.h file in the same location as the program.c file. Either use a relative path to the header file or use #include <> and set 'folderA' as an include directory.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
June 18th, 2009, 07:52 AM
#3
Re: spreading source across folders
Nope.
#include <>
should be used for system files (such as time.h etc)
What needs to be done is simply
Code:
INC_DIRS = src/folderA
program: program.o
$(CC) program.o -o program
program.o: src/folderB/program.c
$(CC) -c src/folderB/program.c -I$(INC_DIRS) -o program.o
using "" or <> for include files works more or less equivalent, except that the order in that direcories are searched for the files differs. I have read the recommendation to use "" for own files and <> for system files.
-
June 18th, 2009, 08:01 AM
#4
Re: spreading source across folders
Richard.J is spot on.
Use the -I for every new directory to be included.
Below is applicable only for templates:
Just be a little careful while using templates, you would need implementation of a function to compile the application file.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|