Home > Net >  command line prompt command (want to understand what its doing)
command line prompt command (want to understand what its doing)

Time:07-04

I am doing the CS50 class, I have installed the cs50.h. Based on the instructions I used the following command in terminal to compile my simple program and just want to make sure I understand everything im asking terminal to do. Line is:

gcc -g hello.c -o hello -lcs50 -lm

I know the following*: gcc =

  • gcc = gnu compiler for C
  • -g = generate source-level debug information
  • Hello.c = name of the file we want to compile
  • -o = write output file
  • hello = our output file name

Can anyone tell me what -lcs50 and -lm are? My guess is that its calling on the library lcs50 in (-lcs50) but again this is a guess and would like to know for sure.

Everything works as it should with no issues

Thanks,

CodePudding user response:

Mostly correct.

  • -o is not required to generate the output file, it's only needed to customize the name. (-o and the following name can only appear together).

  • -lcs50 means "link the library called cs50", not lcs50. It will try to find this file using several different name patterns, e.g. libcs50.so (on Linux), [lib]cs50.dll[.a] (on Windows), libcs50.a (on both), something else on Mac.

  • -lm links the standard math library, but I don't think you need to manually specify it on most modern GCC distributions.

CodePudding user response:

Yes. For -lm, it's for the maths library, which is not linked by default. This is explained well at Why do you need an explicit `-lm` compiler option.

  • Related