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,
Re: spreading source across folders
Quote:
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.
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.
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.