tags:

views:

116

answers:

6

I am new with using gcc and so I have a couple of questions.

What do the following switches accomplish:

gcc -v -lm -lfftw3 code.c

I know that lfftw3 is an .h file used with code.c but why is it part of the command?

I couldn't find out what -lm does in my search. What does it do?

I think I found out -v causes gcc to display programs invoked by it.

+1  A: 

-lm links your program with the math library. -v is the verbose (extra ouput) flag for the compiler. -lfftw3 links your program with fftw3 library.

You just include headers by using #include "fftw3.h". If you want to actually include the code associated to it, you need to link it. -l is for that. Linking with libraries.

Pablo Santa Cruz
+9  A: 

-l specifies a library to include. In this case, you're including the math library (-lm) and the fftw3 library (-lffw3). The library will be somewhere in your library path, possibly /usr/lib, and will be named something like libffw3.so

Paul Tomblin
To expand a bit, `-l` is the switch and text immediately following it is the library name. One can write `-l m` as well. Spaces are optional.
atzz
no wonder I couldn't find -lm in my search...
DemiSheep
A: 

arguments starting with -l specify a library which is linked into the program. Like Pablo Santa Cruz said, -lm is the standard math library, -lfftw3 is a library for fourier transformation.

fschmitt
+4  A: 

From GCC's man page:

 -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 is the library that math.h uses, so -lm includes that library. You might want to get a better grasp of the concept of linking. Basically, that switch adds a bunch of compiled code to your program.

Nathon
A: 

Try man when you're trying to learn about a command.

From man gcc

-v Print (on standard error output) the commands executed to run the stages of compilation. Also print the version number of the com- piler driver program and of the preprocessor and the compiler proper.

As Pablo stated, -lm links your math library.

-lfftw3 links in a library used for Fourier transforms. The project page, with more info can be found here:
http://www.fftw.org/

The net gist of all these statements is that they compile your code file into a program, which will be named the default (a.out) and is dependent on function calls from the math and fourier transform libs. The -v statement just helps you keep track of the compilation process and diagnose errors should occur.

Jason R. Mick
I did try man gcc and was trying to search for -v but I couldn't figure out how to show only "-v" with nothing else I came up with hundreds of other -version type things.
DemiSheep
You just have to keep scrolling down till you get to the letter flags section. A program like gcc has a lot of options, so the man page can be pretty intense, but its worth digging into.
Jason R. Mick
A: 

In addition to man gcc which should be the first stop for questions about any command, you can also try the almost standard --help option. Even for commands that don't support it, an unsupported option usually causes it to print an error containing usage information that should hint at a similar option. In this case, gcc will display a terse (for gcc, its only about 50 lines long) help summary listing the small number of options that are understood by the gcc program itself rather than passed on to its component programs. After the description of the --help option itself, it lists --target-help and -v --help as ways to get more information about the target architecture and the component programs.

My MinGW GCC 3.4.5 installation generates more than 1200 lines of output from gcc -v --help on Windows XP. I'm pretty sure that doesn't get much smaller in other installations.

It would also be a good idea to read the official manual for GCC. It is also helpful to read the documentation for the linker (ld) and assembler (often gas or just as, but it may be some platform specific assembler as well); aside from a platform-specific assembler, these are documented as part of the binutils collection.

General familiarity with the command line style of Unix tools is also helpful. The idea that a single-character option's value might not be delimited from the option name is a convention that goes back essentially as far as Unix does. The modern convention (promulgated by GNU) that multiple-character option names are introduced by -- instead of just - implies that -lm might be a synonym for -l m (or the pair of options -l -m in some conventions but that happens not to be the case for gcc) but it is probably not a single option named -lm. You will see a similar pattern with the -f options that control specific optimizations or the -W options that control warnings, for example.

RBerteig