简体   繁体   English

是否可以从gcc中的C源代码内部链接到数学库?

[英]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 当我试图包含<math.h>我发现我需要使用命令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? gcc -lm可以使用#pragma或其他东西在C代码中完成吗?

EDIT: I have changed -ml to -lm 编辑:我已将-ml更改为-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: 你没有说你正在使用哪个UNIX shell,但如果这只是为了方便,只需编写一个shell函数:

gcm() {
  gcc -lm $*
}

Put that in your shell's startup file and you can compile and link with the maths library with: 将它放在shell的启动文件中,您可以使用以下代码编译和链接数学库:

gcm mycode.c

Using -lm is the only option. 使用-lm是唯一的选择。 Additionally, using #pragma for that is microsoft-specific and rather dirty. 另外,使用#pragma是微软特定的而且非常脏。 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. 想象一下,有一个新的超高效数学库需要-lsupermath而不是-lm - 那么你必须修改代码而不是修改makefile或make配置文件。

No, gcc has no pragmas for linking to libraries. 不,gcc没有用于链接到库的编译指示。 You have to link to the math library with command line options (it's -lm not -ml ) 你必须使用命令行选项链接到数学库(它是-lm不是-ml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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