You are not logged in.

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

sheomualjy
Member
Registered: 2011-12-23
Posts: 37

Geany - Automatically link libraries for C compilation

Hi

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?

Thanks

Offline

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

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,213
Website

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:

#!/bin/bash

FLAGS=""

[[ 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)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

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

jdarnold
Member
From: Medford MA USA
Registered: 2009-12-15
Posts: 462
Website

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.

Offline

Board footer

Powered by FluxBB