0

In pset3/find/Makefile, if somebody has noticed find has "-lcs50" flag to link the libcs50.a file but generate doesn't though generate.c includes cs50.h. Why?

2 Answers 2

1

You don't really need to use any of the CS50 library functions in order to complete the implementation of generate.c, so no need to link the library's binary file (where the object code of the functions lies).

However, you still see the data type string appearing in main's parameter list. As you probably know, the type string is defined in cs50.h and that's why you need to include cs50.h.

You could replace string with char * in which case you'd no longer need to include cs50.h.

As a side note: the option -lcs50 links the cs50's library binary file called libcs50.a which is located under /usr/lib/ in the appliance.

4
  • Hello, Sir. You said, "You could replace string with char * in which case you'd no longer need to include cs50.h" but lest we are using 'string' not "char *" we have to include cs50.h. And if, we are including cs50.h, we have to link "libcs50.a" using flag -lcs50 while compiling which we are not in case of "generate.c". Why? Commented May 25, 2015 at 7:18
  • @UtkarshGupta I answered that already. Please read the answer carefully (first paragraph)!
    – kzidane
    Commented May 25, 2015 at 11:00
  • Yes, sir, I do understand that we don't need cs50 library in generate.c but if we are using string instead of char*, which we are, in the file generate.c provided by cs50, we have to include cs50.h (as told by you), and if we are, we must link libcs50.a. Mustn't we? Commented May 27, 2015 at 5:52
  • @UtkarshGupta, no you don't have to link the cs50 library because you include cs50.h. these are 2 separate things that are resolved in different compilation phases.
    – kzidane
    Commented May 27, 2015 at 13:22
0

generate.c uses "string" not "char*" as its data type. But string is not actually a data type, char is. In generate.c, though we are including cs50.h, all it has to do is to translate that "string" to "char*" as cs50.h only has that typedef. We don't need definitions of other functions that are declared in cs50.h inasmuch as we are not using them in "generate.c". That is why we don't need to provide "-lcs50" to link "libcs50.a".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .