Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 11

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.

10 Replies
Highlighted
Ground Controller Lvl 1
Message 2 of 11

Re: get_int error

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

Highlighted
Ground Controller Lvl 1
Message 3 of 11

Re: get_int error

it works. 

Is there a reason why we should add "-lcs50"?

kindly advice

Highlighted
Commander Lvl 3
Message 4 of 11

Re: get_int error

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.

 

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!

Highlighted
Ground Controller Lvl 1
Message 5 of 11

Re: get_int error

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

Highlighted
Commander Lvl 3
Message 6 of 11

Re: get_int error

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”.

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!

Highlighted
Ground Controller Lvl 1
Message 7 of 11

Re: get_int error

Thanks so much! This really helped me.

Highlighted
Copilot Lvl 2
Message 8 of 11

Re: get_int error

thanks bro now it's working

Highlighted
Ground Controller Lvl 1
Message 9 of 11

Re: get_int error

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

Highlighted
Commander Lvl 3
Message 10 of 11

Re: get_int error

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.

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!