简体   繁体   中英

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 be done in C code using #pragma or something?

EDIT: I have changed -ml to -lm

简化用户(或实际上对于开发人员)的复杂性的通常方法是编写makefile。

首先,它是gcc -lm并且没有#pragma意味着给出链接指令

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM