tags:

views:

405

answers:

3

when i compile lex.yy.c with lfl gcc recognizes that some .a file of the flex library might be needed to be linked with my code. similarly for yacc we specify the -ly compiler option.

in other words if i create a library, abc.a i want gcc to recognize that whenever a program is compiled with -labc it should link with the library abc.a. what configuration changes need to be done?

+8  A: 

The yacc library is named liby.so, and lives in something like /usr/lib, which is a directory that ld knows about.

Your abc library should be named libabc.so (or ".a" for a static lib), and should be placed in a directory that is searched by ld.

To add /home/foo/libs to the list of directories searched, add -L/home/foo/libs to the ld command.

gnud
OK cool, or add the folder to LD_LIBRARY_PATH as $LD_LIBRARY_PATH:./libs
iamrohitbanga
`$LD_LIBRARY_PATH` is mainly used as a hack at runtime, to use shared libraries in directories that the runtime linker doesn't know about. I don't know if it has any effect on `ld`.
gnud
LD_LIBRARY_PATH does not affect the linker.
JesperE
LD_LIBRARY_PATH is for dynamic libraries. right?
iamrohitbanga
Yes, see my comment above, and imagine I said "dynamic, shared libraries".
gnud
A: 

Alternatively, if you want GCC to recognise the library abc and link it via -labc, assuming abc is a static library, make sure your library file/archive abc is named libabc.a, and it is either located in one of the directories GCC searches for .a files, or you add a -L flag where the parameter is the directory where libabc.a is located in.

blwy10
+1  A: 

You don't need to configure anything. Call your library libabc.a, then use the command line:

gcc ... -L<path-to-libabc.a> -labc
PiedPiper