C - Undefined symbols for architecture x86_64 when compiling on Mac OSX Lion

Pillo picture Pillo · Aug 24, 2012 · Viewed 41.7k times · Source

I'm getting some problems on compiling a very very simple name.c file on Mac OSX Lion.

Now, I started following Harvard CS50 course on cs50.net. I'm not totally new to programming but I was curious on how this course has been taught.

This is the source of name.c:

#include <stdio.h>
#include <cs50.h>

int
main(void)
{
    printf("State your name:\n");
    string name = GetString();
    printf("O hai, %s!\n", name);
    return 0;
}

As you can see, it requires this library: https://manual.cs50.net/CS50_Library.

Now, when I compile it, this happens:

Undefined symbols for architecture x86_64:
  "_GetString", referenced from:
      _main in name-vAxcar.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [name] Error 1

If I use the same GetString() cs50.c function inside my source file, it works perfectly:

#include <stdio.h>
#include <string.h>
#include <float.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>

typedef char *string;

string GetString(void);

int
main(void)
{
    printf("State your name:\n");
    string name = GetString();
    printf("O hai, %s!\n", name);
 }

string
GetString(void)
{
    // CODE
}

Why does this happen? I installed the library as it says on the link above; I checked and both cs50.h and libcs50.a are respectively in /usr/local/include and /usr/local/lib.

Thank you in advance for your help.

Answer

user1071136 picture user1071136 · Aug 24, 2012

The problem you encounter is in the linking stage, not compiling. You did not provide the implementation of GetString, only its declaration (through the .h file you #include).

To provide the implementation itself, you usually need to link against the library which includes it; this is usually done by the -l flag to g++. For example,

g++ file.cpp -lcs50

Your second sample code does link, because you manually (and explicitly) provide an implementation for GetString, though an empty one.