You are not logged in.

#1 2012-10-09 06:19:07

Registered: 2011-12-23
Posts: 37

Geany - Automatically link libraries for C compilation


I am learning C and have recently switched from using Code::Blocks to using Geany as an IDE. One thing that I have come to miss is the automatic linking of libraries when compiling.

Say, I write a program that uses the <math.h> header. Code::blocks detects that and automatically links in the appropriate math library when compiling. In Geany however, I need to manually put in the -lm flag in the build command, and when I am not using the math library, I need to manually take it out (otherwise the executables are bigger than necessary).

Does anyone know a way that I can tell Geany to automatically link in the appropriate libraries?



#2 2012-10-09 11:33:19

Inspector Parrot
Registered: 2011-11-29
Posts: 19,085

Re: Geany - Automatically link libraries for C compilation

I don't use geany so I can't be sure, but there may not be any built in autodetection of libraries.

I'm sure if you looked you could find some compiler option that would do this autodetection, or you could make a simple one yourself for the libs you commonly use, something like the following (untested) script:



[[ grep "#include <math.h" $1 ]] && FLAGS+=" -lm"
[[ grep "#include <X11/Xlib.h>" $1 ]] && FLAGS+=" -lX11"
# more of the same here

gcc -o ${1%.*} $1 $FLAGS

Then you could use that script in place of the compiler option in geany.

Alternately, you could start using Makefiles.  While the former may be easer and more familiar, I'd suggest getting used to Makefiles if you're going to be working much in C.

Last edited by Trilby (2012-10-09 11:34:11)

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman


#3 2012-10-09 14:25:52

From: Medford MA USA
Registered: 2009-12-15
Posts: 478

Re: Geany - Automatically link libraries for C compilation

Don't worry about extra libs that you don't use - they don't make the executable bigger. The linker only pulls in routines that you actually use from libraries you link against. The only thing would be a probably undetectable extra amount of time it would take to search it.

Personally, I don't like the idea of too much hand holding. You should know what libraries you need and add them yourself, says my internal curmudgeon programmer.


Board footer

Powered by FluxBB