2

When using cs50 library, get_string function doesn't work properly.

uppercase.c:6:19: error: too few arguments to function 'get_string'
     string name = get_string("hey? ");
                   ^~~~~~~~~~
In file included from uppercase.c:2:0:
c:\mingw\include\cs50.c:78:8: note: declared here
 string get_string(va_list *args, const char *format, ...)
        ^~~~~~~~~~

How can I make this work?

  • `string s = get_string("Prompt: ");` is the example shown on the github page so probably something else. – Allan Wind Jun 05 '23 at 04:48
  • 1
    The function `get_string` as defined in `cs50.c` requires at least two function arguments. However, these function parameters are concealed by the macro `get_string` which only requires at least one function argument. Therefore, the line `string name = get_string("hey? ");` appears correct to me. I would have to see your entire code to see what is wrong. – Andreas Wenzel Jun 05 '23 at 04:54
  • @AndreasWenzel Heh. I didn't even know you could define a macro with the same name as the function. – Allan Wind Jun 05 '23 at 04:56
  • 2
    @AllanWind: You probably have never seen it because it is bad coding style. It [has already been suggested](https://github.com/cs50/libcs50/issues/176) that the CS50 library should implement `get_string` in a better way. – Andreas Wenzel Jun 05 '23 at 05:03
  • 1
    I agree with @AllanWind. `"c:\mingw\include\cs50.c"` certainly looks suspicious. Even more so as it is included as part of your `uppercase.c` file. The `cs50.c` source contains various `define` and `undef` statements that will effect how the compile goes if you include that file. Don't do it. Simply `#include "cs50.h"` and then compile and either include `cs50.c` as a source to compile against, or simply pass the library to the linker and link against a built version, e.g. `-lcs50`. – David C. Rankin Jun 05 '23 at 05:04

1 Answers1

3

The error message suggest that in uppercase.c you #include <cs50.c>. The correct usage is to include the header file #include <cs50.h> and link the implementation library -lcs50 when you build your program. The documentation says:

If, when compiling a program, you see /usr/bin/ld: cannot find -lcs50: Add export LIBRARY_PATH=/usr/local/lib to your .bashrc.

You normally specify the library search path with -L/usr/local/lib or whatever path you installed the library to.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • Worth a sentence about the difference between `#include ` for a standard include within the include search path, or `#include "cs50.h"` as a local header in the current project directory structure. (still must be found by the include search path) – David C. Rankin Jun 05 '23 at 05:07
  • 1
    @DavidC.Rankin Usually, yes, but I think it will just confuse op as it's documented as `<>`. I will add note about the library search path though. – Allan Wind Jun 05 '23 at 05:08
  • 1
    Maybe right -- if he installed the library properly, then `#include ` is correct. If he just dumped `cs50.c` and `cs50.h` into his current directory, then `#include "cs50.h"` would be the better form. – David C. Rankin Jun 05 '23 at 05:09
  • 1
    @DavidC.Rankin Let's see what op says and I will revise. If you want to write up an answer with this that is totally fine as well. – Allan Wind Jun 05 '23 at 05:10
  • 2
    Oh, no, your answer is fine. We have it in the comment and we can see if we get more feedback if the issue isn't resolved. – David C. Rankin Jun 05 '23 at 05:11