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

1

13

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.

Kolnos answered 24/8, 2012 at 13:19 Comment(2)
Are you passing -lcs50 to your compiler?Lorislorita
And also an appropriate -L<some_directory> to tell the compiler where to search for -lcs50?Wehrle
U
19

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.

Udometer answered 24/8, 2012 at 14:44 Comment(1)
Thank you so much, sorry but I (wrongly) thought compiler knew where to look for. Now on, I will pay more attention on linking. Thank you again!Kolnos

© 2022 - 2024 — McMap. All rights reserved.