Search code examples
c++cideinclude-path

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.

  • I try to avoid using libraries (.lib, .dll)
  • I don't want to copy the .c files in each project (proj1, proj2, ..., projn)
  • I want to be able to simply edit the .c and .h files and if recompiling proj1 and so on the changes will be applied, as they will for all other projects
  • Generating an own makefile may be a solution (but shouldn't there be an Option to add a source-file-path in IDEs?)
  • #include <..\utility\src> is a non-desired solution as changes to the directory will fource to add this line in each single file, where changing a path in the Options are only some clicks.

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


Solution

  • 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 :)