spreading source across folders
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: spreading source across folders

  1. #1
    Join Date
    Jun 2009
    Posts
    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,

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,013

    Re: spreading source across folders

    Quote Originally Posted by rowdy View Post
    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

  3. #3
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,109

    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.

  4. #4
    Join Date
    Feb 2009
    Posts
    326

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center