2

I'm trying to figure out how to add header and object files to the standard library so that I can use my custom files as easy as the standard library.

Currently I have to type the path to the header file in the .c file and then link the object file path when compiling.

I would like to just be able to add:

#include <mystdlib.h>

and not worry about linking the object file like I do when I reference the stdio.h header file.

I have searched around, but I fear I'm not using the proper terminology as I don't seem to find the results I need. Am I the first to want to do this, or it is just impossible, and therefore people don't even try?

Deanie
  • 2,316
  • 2
  • 19
  • 35
  • If your header file is in the same project folder as the `.c` file you can use: `#include "mystdlib.h"` – Rizier123 Nov 23 '14 at 00:55
  • If the header is NOT in a standard location, and not in your current directory, then use the "-I" argument to specify the directory that contains it. for example: `gcc -I/home/mysql/include xx.c -o xx` – TonyB Nov 23 '14 at 01:11
  • 2
    Using a tool like http://www.gnu.org/software/make/ will greatly simplify your build process. Generally you don't want to manually invoke gcc because compiling and linking everything in non-trivial projects is both time consuming and error prone. – engineerC Nov 23 '14 at 01:19
  • @CaptainMurphy Proper makefiles are often notoriously hard to get right. And if they're going to use `Make` and not something like cmake or automake (or whatever your favorite build system is), they still need to know the basics (or just spend a day reading `info gcc`). –  Nov 23 '14 at 01:21
  • If the header is in the same directory as your source file you can use `#include "header.h"` and you don't need to use `-I` to tell the compiler where to find it. If it's not in the same directory use a makefile (I disagree with the comment above, simple makefiles are very simple, e.g. as little as `CPPFLAGS = -I ~/path/to/headers` is a valid makefile) – Jonathan Wakely Nov 23 '14 at 15:39

1 Answers1

3

gcc uses environment variables C_INCLUDE_PATH and LIBRARY_PATH to look for header and library files. Setting them somewhere (eg., your bash_profile) should achieve what you describe:

export C_INCLUDE_PATH="$HOME/include"
export LIBRARY_PATH="$HOME/lib"

Alternatively, the -I and -L flags add directories to the list of directories to be searched for header files and library files, respectively.

edit: As noted by @ChrisStratton below, the library name or object file needs to be explicitly specified. AFAIK, there is no way to make gcc always link against a library (like with libc).

sources:

https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html

MBlanc
  • 1,773
  • 15
  • 31
  • 1
    This will take care of the searching, but the library name or object file implementing the functionality would still have to be explicitly specified. – Chris Stratton Nov 23 '14 at 01:20
  • An alternative to exporting LIBRARY_PATH allowing the executable to load your custom lib, you can also specify `rpath` which will be included in the executable itself (e.g. `-Wl,-rpath=/path/to/your/lib`) Note, this means that your library must be in the same place on other systems for your executable to run on other boxes. – David C. Rankin Nov 23 '14 at 02:44
  • @ChrisStratton Thanks! I've incorporated your comment to the answer. I'm curious if it's possible to make gcc automatically link against something. – MBlanc Nov 23 '14 at 14:49
  • @DavidC.Rankin Good point. For dynamic libs `rpath` can be used in lieu of `LD_LIBRARY_PATH`, but `LIBRARY_PATH` _must_ be set either way (http://stackoverflow.com/questions/4250624/ld-library-path-vs-library-path) – MBlanc Nov 23 '14 at 15:10
  • @MBlanc, I don't know what you mean about `LIBRARY_PATH` _must_ be set ... most people use `-L` and don't use `LIBRARY_PATH` at all. – Jonathan Wakely Nov 23 '14 at 15:35