mjhalwa
mjhalwa

Reputation: 533

How to store .c and .h files in an includepath, separate from source code of current project

I tried to shorten down my question (the Old question can still be found below)

My current directory structure looks like this

C:\users\documents\projects 
    |
    +----- utility
    |         |
    |         +----- include (files not shown)
    |         +----- src
    |                 |
    |                 +----file1.c (and other files not shown)
    |
    +----- proj1
             |
             +----- include (files not shown)
             +----- src
                     |
                     +----- proj_file1.c (and other files not shown)

I can include the .h files from ..\utility\include with #include <file.h> to proj1, if I add this directory as include path to my IDE (in proj1). Is there an aequivalent solution for the ..\utility\src files? I am using LPCXpresso IDE on Windows 7. I suppose there is the same solution on any IDE, so I just want to know how this whatever path (where .c files will be searched, if not found in the .\src directory) is generally called to find it in my project settings.

Thanks in advance and thanks for the answers up to now


Old Version of my question:

Motivation: Imagine, you write a program in C/C++ in some IDE and have .c and .h source code files as usual. In addition you have a helper.c and helper.h file, were you defined some useful, not project related functions (which can be used in several projects). You want to include these files, but don't want to have them were you store your project related source code.

As far as I know .h files can be stored in a separate folder, which is pointed to by the includepath. This path can be set in every IDE. Further it changes the

#include "helper.h"

statement to

#include <helper.h>

If I put the .c files in the same folder and not include them separately, the compiler will not find them. If I include them as well with

#include <helper.c>

a multiple inclusion will lead to multiple function deklaration and therefore to a compiler-error. Only solution may be an

#ifndef helper_c_
//content of helper.c file
#endif

, which is kind of impractical and will always need inclusion of the .h and the .c file. But i only need to have them stored once, with no copies and if i need to change something, it will change in all projects, as they are all pointing to that folder

I also now about library files, where you have an .lib and a .dll file, where the .lib file needs to be pointed at by the library-path and the .dll file needs to be in the same folder as the .exe file afterwards. But that is not what i want.

My Question: Is there a possibility to store the .h and .c file (in my current case there are 10 file-pairs) in a separate folder and point at them via an include path or so? I tried googling around, but I think I am not quite sure what i shall look for.

Thanks for help

EDIT: I forgot to mention: I use Windows 7, and my current IDE is the LPCXpresso-IDE

Upvotes: 0

Views: 1512

Answers (4)

thurizas
thurizas

Reputation: 2508

OK, suppose you have this directory structure:

C:\users\documents\projects 
        |
        +----- utility
        |         |
        |         +----- include (files not shown)
        |         +----- src
        |                 |
        |                 +----file1.c (and other files not shown)
        |
        +----- proj1
                 |
                 +----- include (files not shown)
                 +----- src
                         |
                         +----- proj_file1.c (and other files not shown)

And also assume, that the current directory for compilation is in the proj1/src directory. I see at least three solutions to your question:

  1. if you really want to #include the source files, which I do not recommend doing, just use a relative path to the files i.e.

    #include "..\..\utility\src\file1.c"
    

Now in addition to the issues with including source files, this tends to be very fragile in that if you change the directory structure (or change a name of a directory) everything breaks. You would need to go back into your source and fix every line of code.

  1. As iharob suggested, use a make file to handle this. In this case, you would have a compile line that looked like this (assuming you are using Microsoft's tool change);

    cl /I..\..\utility\include ..\..\utility\src\file1.c  /o util_file1.o
    

This causes the result of the compilation to be dropped in the current working directory and the linker will be able to find all the object files and combine them together into one executable. We still are dealing with relative paths here, but all the changes would be in a single file, and by using make variables the changes would be on a single line or two.

  1. If the functions in the utility directory are going to be used in multiple projects, my personal favorite solution is to make a utility project that produces a dynamic library (a dll under windows) and link subsequent projects with that library. You still have to deal with locating where the include files are going to be (maybe a directory at the top level of where all your project folders are?), but to me a library seems cleaner. It also has the added advantage that if you want to modify the code in the utility project, just recompile the library and the rest of you project will 'see' the modifications with out recompilation of them (assuming you do not modify the interface of the library).

Hope this helps, T

Upvotes: 2

mjhalwa
mjhalwa

Reputation: 533

After looking around through all the settings and options I found the following satisfying solution: creating a Link to a source file folder

This answer applies to the LPCXpresso IDE

  • imagine the folder structure shown in my question
  • inside the LPCXpresso IDE -> rightclick on the project -> properties
  • navigate to C/C++ General>Paths and Symbols>Source Loacation
  • click "Link Folder..."
  • in the opened dialog tag the checkbox Link to folder in the file system
  • click Browse... or enter C:\users\documents\projects\utility\src
  • click OK
  • click Apply
  • recompile and be happy :)

Upvotes: -1

Tufak
Tufak

Reputation: 36

A word of warning: it is generally not a good idea to #include source (.c) files. The source files are meant for compilation, not inclusion -- including them may result in strange errors (most prominently, apparent re-definitions of a function).

Here is what I would do (for each project that needs the helper code):

  • Add your utility .c files to the project. Look for Add existing file... or a similar IDE feature; this ensures that your utility source files, i.e. helper.c, get compiled along with your project.
  • As for the .h file, include it with #include <helper.h>, enabling you to use your utility declarations.
  • Finally, find a compiler option called Include paths, a.k.a. -I, and set it to the folder that contains helper.h. This is usually found in Project options/settings.

Upvotes: 0

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

Yes of course there is, depending on what compiler you are using there will be a switch to tell the compiler where to search for headers, for gcc and some others AFAIK, it's -I, so for example suppose that your headers are in the myproject/headers directory, the compiler should be invoked like this

gcc -I myproject/header ${OTHER_OPTIONS} ${SOURCE_OR_OBJECT_FILES} -o ${OUTPUT_FILE}

The usual way to build a project with the .c files in different directories is to use a Makefile and a program that can parse the Makefile and invoke the neecessary commands to build the project.

Upvotes: 0

Related Questions