get_int error

Hello im taking the harvard intro to cs course and am having the following error

~/workspace/pset1/mario/less/ $ clang mario.c
/tmp/mario-69d749.o: In function main': mario.c:(.text+0x1c): undefined reference to get_int’
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Kindof lost as ive had so many issues with cloud9…anyhow here is my code

#include <stdio.h>
#include <cs50.h>
int main(void)

{
int n;
do
{
int n = get_int("Height: ");

} while (n > 0 || n > 23);

for (int i = 0; i < n; i++)
{
for (int j = 0; j < n + 1; j++)
{
if (i + j >= n - 1)
printf("#");
else
printf(" “);
}
printf(”\n");
}
}

Im honestly scared to try anymore solutions i have looked up as another non-working solution wil involve me restarting the workspace at best…or restarting my pc at worst…ive tried to use GetInt but that didnt work either. any explanation why my code isnt working would be appreciated.

1 Like

add  clang -lcs50 before running ur code e.g   clang -lcs50  mario.c

7 Likes

Hi @nickbabington , did the kind reply from @olusanyajohn90 help you out? If so, please give him some kudos and mark his answer as the accepted answer. If not, feel free to post back!

1 Like

it works. 

Is there a reason why we should add “-lcs50”?

kindly advice

2 Likes

In this situation, the get_int function is an external function defined in an external library. The program cannot be built unless you tell clang the name of the external library. The “-lcs50” command line option is telling clang to use the external library “cs50”. I’m assuming the external library “cs50” is provided by the professor for the class to use.

2 Likes

Can you please help  me?

I downloaded cs50.h and run it on DevCC app

But when i compile it, it says “undefined reference to get int”

The app recognizes the cs50.h library, did I miss something? 

Thanks

The cs50.h file is not the library. It is the “definitions” file. As previously posted, you need to have “-lcs50” in your command line to link against the library. You also need a copy of the cs50.lib file, which is the actual library.

You will want to review the course notes, especially this section:

So we can include another file, cs50.h, which also includes the function get_string, among others.
#include <cs50.h>
#include <stdio.h>

int main(void)
{
    string name = get_string("What's your name?\n");
    printf("hello, %s\n", name);
}
Now, when we try to compile our program, we have just one error:
$ clang -o string string.c
/tmp/string-aca94d.o: In function `main':
string.c:(.text+0x19): undefined reference to `get_string'
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
It turns out that we also have to tell our compiler to add our special CS50 library
file, with clang -o string string.c -lcs50, with -l for “link”.
1 Like

Thanks so much! This really helped me.

thanks bro now it’s working

this command is generating “a.out” named file, nothing else??

clang hello.c

will generate a file called “a.out”. To run it, you’d type:

./a.out

clang -c hello hello.c

creates a file called “hello”, executed as:

./hello

Slides 35 and 36 of Week 1 of CS50.