views:

119

answers:

6
  • I am writing a program on linux gcc...
  • When I tried to include <math.h> I found that I need to link math library by using command gcc -lm
  • But I am searching for another way to link the math library 'in code', that does not require the user to compile using any options..

Can gcc -lm be done in c code using #pragma or something?

EDIT: I have changed -ml to -lm

+3  A: 

First, it's gcc -lm and no there is no #pragma meant to give linking directives

Gregory Pakosz
+3  A: 

No, you need to tell the linker to link the library in order to link the library.

The linker doesn't know about the code, only the compiled object files. It won't see a language specific pragma.

Pete Kirkham
+2  A: 

No, gcc has no pragmas for linking to libraries. You have to link to the math library with command line options (it's -lm not -ml )

nos
+2  A: 

Using -lm is the only option. Additionally, using #pragma for that is microsoft-specific and rather dirty. Imagine there is a new super-efficient math library which requires -lsupermath instead of -lm - then you'd have to modify your code instead of modifying a makefile or a make config file.

ThiefMaster
+3  A: 

You don't say which UNIX shell you are using, but if this is just for conveniance, simply write a shell function:

gcm() {
  gcc -lm $*
}

Put that in your shell's startup file and you can compile and link with the maths library with:

gcm mycode.c
anon
i doubt that's useful in the general case
Gregory Pakosz
+4  A: 

The usual way to simplify complication for the user (or indeed for the developer) is to write a makefile.

Andrew McGregor
however i fail to see how it answers the question about pragmas ;)
Gregory Pakosz