0

I get the following error

cc     string.c   -o string
string.c:7:16: warning: implicit declaration of function 'get_string' is invalid in C99
      [-Wimplicit-function-declaration]
        string name = get_string();
                      ^
string.c:7:9: warning: incompatible integer to pointer conversion initializing 'string' (aka 'char *')
      with an expression of type 'int' [-Wint-conversion]
        string name = get_string();
               ^      ~~~~~~~~~~~~
2 warnings generated.
ld: can't write output file: string for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [string] Error 1

when I try to

make string

the code of string.c:

  1 #include <stdio.h>
  2 #include <cs50.h>
  3 
  4 int main(void)
  5 {
  6         printf("Name: ");
  7         string name = get_string();
  8         printf("hello, %s\n", name);
  9 
 10 }

The code is identical to the course on Week1.

Bare in mind that i have downloaded the cs50.h and installed it as per instructions from the CS50 library manual for OSX system.

jmd_dk
  • 12,125
  • 9
  • 63
  • 94
cl0wn cl0wn
  • 31
  • 1
  • 1
  • 2
  • 1
    It is a different version because I just found `string get_string(void);` in a `CS_50.h` reference (slightly different name from `cs50.h`). The reason for the error is because somewhere in the code *before* it is referenced, you need a declaration of what `get_string()` is, with either a function prototype or the function definition (implementation). When absent, compilers used to make implicit assumptions about the function but they no longer do so. – Weather Vane Jan 09 '17 at 18:28
  • You need to declare the function in the header. Perhaps the declaration and definition is already present and would be linked but the compiler doesn't know this. Add string get_string(); to header and see if linker correctly links it and if it performs with no definition. – marshal craft Jan 09 '17 at 18:30
  • sorry if being silly you mean that i have to edit the CS50.h that is located in /usr/local/include ? (i think the installation put it there) – cl0wn cl0wn Jan 09 '17 at 18:49
  • It is unlikely that editing a third-party library header is the right thing to do. Have a read through the header file and see if you can find the function, or similar, so you know what function to use. – Weather Vane Jan 09 '17 at 18:52
  • Supposing that there many different ways to get a string from user should I just skip this part in my studies or is a major aspect of making it work? kind thanks – cl0wn cl0wn Jan 09 '17 at 18:55
  • 2
    Belongs on cs50.stackexchange.com – M.M Jan 09 '17 at 21:44
  • Figured afterwards about the cs50 section I'm still rookie to move it :D this thing drives me crazy – cl0wn cl0wn Jan 09 '17 at 21:46

1 Answers1

4

According to the cs50 manual, the function name is GetString() not get_string(). So, outdated implicit the function declaration kicks in (which is not valid since C99), resulting in compiler assuming get_string() returns an int. But since there's no definition for get_string() eventually the linking fails.

Change:

string name = get_string();

to:

string name = GetString();
Community
  • 1
  • 1
P.P
  • 117,907
  • 20
  • 175
  • 238
  • Did as you said and the result of make string is Undefined symbols for architecture x86_64: "_GetString", referenced from: _main in string-cbacbf.o – cl0wn cl0wn Jan 09 '17 at 18:47
  • You probably haven't installed cs50 correctly. See: https://manual.cs50.net/library/#mac_os The error says the function `GetString` couldn't be found by the linker. That means you haven't compiled the cs50.c and/or linked it in your executable. – P.P Jan 09 '17 at 19:06
  • Do I need to do something more than what the manual says? I have done it twice step by step and still get the same error... thats frustrating – cl0wn cl0wn Jan 09 '17 at 19:18
  • 1
    @cl0wncl0wn: Does your linking (compiling) line include `-L /opt/cs50/lib` and `-lcs50` — where you need to specify the directory where you installed the CS50 library (unless, perhaps, it is in `/usr/local/lib` — in which case you may be able to omit the `-L /usr/local/lib`), and `-lcs50` instructs the linker to look for `libcs50.so` or `libcs50.a` (or equivalent names; `libcs50.dylib` on macOS, for example). – Jonathan Leffler Jan 09 '17 at 22:19
  • Default installation paths are the /usr/local/include for cs50.h and /usr/local/lib for the libcs50.so ----I played around the compiling process. eventually I did run manually cc string.c -o string -lcs50 as specified by lib manual it did run with no errors but the problem is that when i execute the string program i get the Name: as output and then Segmentation Fault 11 that seems to occur on that line that uses cs50.h i don't actually have clue what is that – cl0wn cl0wn Jan 09 '17 at 22:27
  • Fixed the Segmentation fault 11 which was coming from wrong function GetString() as it was defined on the updated library as get_string(). @JonathanLeffler can you please advise if there is a way to force make when working with cs50.h to compile with -L /usr/local/lib and -lcs50? Kind thanks – cl0wn cl0wn Jan 10 '17 at 13:57