Reputation: 533
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.
#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
Upvotes: 0
Views: 1512
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:
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.
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.
Hope this helps, T
Upvotes: 2
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
Upvotes: -1
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):
helper.c
, get compiled along with your project.#include <helper.h>
, enabling you to use your utility declarations.helper.h
. This is usually found in Project options/settings.Upvotes: 0
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