-5

Specifically, what does the "-lm" mean, and is it required to include that? Is there a "dictionary" online that will explain all of these command abbreviations like "-lm"?

FriskySaga
  • 409
  • 1
  • 8
  • 19
  • `-lstuff` means 'link the executable with a library called "stuff"'. – ForceBru Mar 28 '17 at 16:40
  • 1
    This is called documentation or manual. Like in RTFM. – too honest for this site Mar 28 '17 at 16:40
  • @Olaf My professor required that all students use "gcc -lm -o" to compile our C programs. I don't really know what the "-lm" means, though. – FriskySaga Mar 28 '17 at 16:41
  • And instead of reading the docs, want it spoon-fed? That's hardly what your prof wants you to do. – too honest for this site Mar 28 '17 at 16:42
  • What documents? I have literally zero idea of what documents you are referring to. – FriskySaga Mar 28 '17 at 16:43
  • 1
    Those are called _flags_. Google 'GCC compiler flags' and you'll [get your 'dictionary'](https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html) – Spikatrix Mar 28 '17 at 16:46
  • @Olaf I guess you were referring to the documentation that Attie linked. The problem is that I didn't know what to Google. If I typed into Google, "What does 'gcc -lm -o' mean?", I don't get any tangible results. Feel free to try it yourself. All I received was an assignment PDF from my professor. The first assignment only required students to compile with "gcc -o". Now, this assignment requires students to compile with "gcc -lm -o", and so I had no idea where the "-lm" came from. – FriskySaga Mar 28 '17 at 16:47
  • 1
    You are using `gcc`, right? So it is pretty obvious that you need `gcc` documentation. – Eugene Sh. Mar 28 '17 at 16:48
  • @CoolGuy Thank you so much. I didn't know they were called "flags". That's what I called them "abbreviations" in my original question. It really helps to know the terminology. – FriskySaga Mar 28 '17 at 16:49
  • 1
    @FridaySky FYI, if you Google 'Porsche - Carrera', you'll get search results having 'Porsche' that does not have 'Carrera' in it. For Google, `-` is a special character which excludes stuff. So, if you search 'GCC -lm', you'll get search results having GCC without 'lm'. To avoid this, Google either `GCC lm` or `GCC "-lm"`. – Spikatrix Mar 28 '17 at 16:51
  • 1
    Google "gcc documentation" would be the obvious. And these **options** (they are not flags!) are standard for command line arguments. That is basic computer knowledge. If in doubt, you should ask your prof to add this information. Just don't eat everything he tells you without asking if something is unclear. – too honest for this site Mar 28 '17 at 17:00
  • @Olaf It's not entirely obvious for me since I wouldn't use the word "documentation". I probably would type in "gcc compiler" and "gcc commands" and "command prompt commands". – FriskySaga Mar 28 '17 at 17:05
  • Apparently you did not even try these! – too honest for this site Mar 28 '17 at 17:06
  • @Olaf I can definitely tell you about data structures like linked lists, stacks, and queues, but I've never been exposed to the nitty-gritty like gcc documentation, options/flags, etc. I didn't try them because "what does gcc -lm -o mean" didn't work for me. – FriskySaga Mar 28 '17 at 17:07
  • The canonical may be *[Why do you have to link the math library in C?](https://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c)* (18 answers. 314 upvotes. 2009.) – Peter Mortensen Oct 27 '22 at 23:48
  • Which is a duplicate of *[GCC -lm -lz -lrt options - what are they about?](https://stackoverflow.com/questions/5663097/gcc-lm-lz-lrt-options-what-are-they-about)* (2011). – Peter Mortensen Oct 27 '22 at 23:59
  • @PeterMortensen why are you bringing this up now? As someone said above: "For Google, - is a special character which excludes stuff. So, if you search 'GCC -lm', you'll get search results having GCC without 'lm'. " – FriskySaga Oct 28 '22 at 03:45

2 Answers2

3

This is linking against the m library... Which is used for math functions.

The -l<library name> parameter to gcc means 'link with this library'.

The m is the library to link with (e.g: libm.so or libm.a).

See the GCC man page (run man gcc), and functions like sin(), sqrt(), pow(), etc.

Note that in these man pages, it states:

Link with -lm.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Attie
  • 6,690
  • 2
  • 24
  • 34
  • 1
    Don't answer off-topic questions. – too honest for this site Mar 28 '17 at 16:40
  • This helped so much. Thank you for the link. I'll mark Best Answer asap. – FriskySaga Mar 28 '17 at 16:44
  • 3
    No problem. Hopefully your professor could have answered that as well - don't be afraid to ask for help, how else will you learn? – Attie Mar 28 '17 at 16:46
  • @Attie google it? :) – Chris Turner Mar 28 '17 at 16:46
  • 2
    @ChrisTurner In general, yes I agree. But for something so teeny-wee, Googling is tricky to do correctly, especially for beginners... you think `what does gcc -lm mean` might work? Not likely... it'll exclude `lm` from the search terms (yay). It's a big world, and we all had to start somewhere. – Attie Mar 28 '17 at 16:49
  • 1
    @Attie works just fine if you put quotes around the -lm and gives several useful results *shrugs* – Chris Turner Mar 28 '17 at 16:51
  • 2
    @ChrisTurner Yeah, I know... not my point. How did you *know* to quote it? Magic? – Attie Mar 28 '17 at 16:52
  • 2
    @ChrisTurner I actually typed in exactly what Attie said: "what does gcc -lm -o mean". When that didn't work on Google, I went to Bing. When that didn't work, I was going to send my TA an email but he specified that he only responds on Fridays. So, then I looked it up on Stack Overflow because maybe Google missed those results. I didn't find anything either, so I asked a question. Until you mentioned it, Chris, I didn't know putting quotes around a word was a good trick to Googling effectively. – FriskySaga Mar 28 '17 at 16:58
2

-l stands for library and here by using -lm, you are telling GCC to use the math library. If your program is using functions like pow, sqrt, floor or others from the math library, you need to tell the compiler to link it with the math library and that’s what it’s doing.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Swati
  • 57
  • 3
  • 1
    Quick question (and it's fine if you don't want to answer it): But why won't #include do the trick anyway? Or would I need to "gcc -lm" in conjunction with #include ? Sorry, I used to compile my programs on CodeBlocks for my Intro to C course, so compiling through the command line is kind of weird for me. – FriskySaga Mar 28 '17 at 16:55
  • 2
    There are two parts to using a library - the function 'prototypes' (aka API), and the actual executable implementation. You need to link with both. There are also multiple steps to 'compiling' an application - 'compiling', and 'linking'. Worth reading up on. – Attie Mar 28 '17 at 16:57