开发者

Is it possible to link to the math library from inside the C source code in 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开发者_JAVA技巧 be done in C code using #pragma or something?

EDIT: I have changed -ml to -lm


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


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


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.


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


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.


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 )

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜