0
votes

Introduction to GCC has an example:

$ gcc -Wall -L. main.c -lhello -o hello 

The option ‘-L.’ is needed to add the current directory to the library search path.

Does it mean that the current directory is not a default search path for static library files, so needs to be added to the library search path by -L.?

Similar question for dynamic library file search. Is the current directory is a default search path for dynamic library files, or do we need to add the current directory to the dynamic library search paths by --rpath .?

Similar question for header file search. Is the current directory is a default search path for header files , or do we need to add the current directory to the header search paths by -I.? Does the following example imply that the current directory is a default search path for header files , and we don't need to add the current directory to the header search paths by -I.?

$ ls main.c hello.h
hello.h  main.c

$ cat main.c
#include "hello.h"
int
main (void)
{
  hello ("world");
  return 0;
}

$ gcc -c main.c
$
1

1 Answers

1
votes

The current working directory is not part of the compiler's default library or header search paths.

However, includes of the form #include "file" (with quotation marks) will always search the current working directory, regardless of whether it is on the header search path or not. As such, -I. is only necessary if you are including files in the project directory using #include <file> (which you really shouldn't do, because it'll confuse anyone trying to read your code).