代码之家  ›  专栏  ›  技术社区  ›  DemiSheep

gcc开关-它们做什么?

  •  2
  • DemiSheep  · 技术社区  · 14 年前

    我是使用gcc的新手,所以我有几个问题。

    gcc -v -lm -lfftw3 code.c
    

    我知道 lfftw3 是一个 .h 与一起使用的文件 code.c 但为什么这是命令的一部分?

    我不知道是什么 -lm 在我的搜索中。它有什么作用?

    我想我发现了 -v 使gcc显示它调用的程序。

    6 回复  |  直到 14 年前
        1
  •  11
  •   Paul Tomblin    14 年前

    -l 指定要包含的库。在本例中,您将包括数学库( -lm )以及fftw3图书馆 (-lffw3 ). 库将位于库路径的某个地方,可能是/usr/lib,并将命名为 libffw3.so

        2
  •  4
  •   nmichaels    14 年前

    从GCC手册页:

     -v  Print (on standard error output) the commands executed to run the
               stages of compilation.  Also print the version number of the
               compiler driver program and of the preprocessor and the compiler
               proper.
    
    -l library
               Search the library named library when linking.  (The second
               alternative with the library as a separate argument is only for
               POSIX compliance and is not recommended.)
    
               It makes a difference where in the command you write this option;
               the linker searches and processes libraries and object files in the
               order they are specified.  Thus, foo.o -lz bar.o searches library z
               after file foo.o but before bar.o.  If bar.o refers to functions in
               z, those functions may not be loaded.
    
               The linker searches a standard list of directories for the library,
               which is actually a file named liblibrary.a.  The linker then uses
               this file as if it had been specified precisely by name.
    
               The directories searched include several standard system
               directories plus any that you specify with -L.
    
               Normally the files found this way are library files---archive files
               whose members are object files.  The linker handles an archive file
               by scanning through it for members which define symbols that have
               so far been referenced but not defined.  But if the file that is
               found is an ordinary object file, it is linked in the usual
               fashion.  The only difference between using an -l option and
               specifying a file name is that -l surrounds library with lib and .a
               and searches several directories.
    

    libm 是那个图书馆吗 math.h linking . 基本上,这个开关会向程序中添加一堆已编译的代码。

        3
  •  1
  •   Pablo Santa Cruz    14 年前

    -lm 将程序与数学库链接。 -v 编译器的详细(额外输出)标志。 -lfftw3

    您只需使用 #include "fftw3.h" -l 就是为了这个。与图书馆链接。

        4
  •  0
  •   fschmitt    14 年前

    以-l开头的参数指定链接到程序的库。正如pablosantacruz所说,-lm是标准的数学库,-lfftw3是傅里叶变换库。

        5
  •  0
  •   Jason R. Mick    14 年前

    尝试 man 当你试图学习命令的时候。

    man gcc

    -v打印(在标准错误输出上)运行 通信- piler驱动程序及预处理器和编译器的设计 适当的。

    -lm 链接你的数学库。

    -lfftw3
    http://www.fftw.org/

    所有这些语句的要点是它们将您的代码文件编译成一个程序,该程序将被命名为default(a.out),并且依赖于来自math和fourier transform libs的函数调用。v语句只是帮助您跟踪编译过程并诊断应该发生的错误。

        6
  •  0
  •   RBerteig Keith Adler    14 年前

    除了 man gcc --help 选项。即使对于不支持它的命令,不受支持的选项通常也会导致它打印包含使用信息的错误,这些信息应该会提示类似的选项。在这种情况下,gcc将显示一个简短的帮助摘要(对于gcc,它只有大约50行长),列出gcc程序本身可以理解的少量选项,而不是传递给它的组件程序。在描述 选项本身,它列出 --target-help -v --help

    我的mingwgcc3.4.5安装从 gcc -v --help

    这也是一个好主意,阅读 official manual for GCC . 阅读链接器的文档也很有帮助( ld gas 或者只是 as ,但也可能是某些特定于平台的汇编器);除了特定于平台的汇编器之外,这些都作为 binutils

    熟悉Unix工具的命令行样式也很有帮助。单个字符选项的值不能从选项名中分隔的想法基本上可以追溯到Unix。现代惯例(由GNU颁布),多字符选项名称由 -- 而不仅仅是 - -lm 可能是 -l m (或一对选项) -l -m 在某些公约中,但这恰好不是 gcc )但这可能不是一个单独的选项 -流光溢彩 . 您将看到与 -f 控制特定优化或 -W 例如,控制警告的选项。