问题
I'm not new to programming but I'm also far from an expert. I'm taking CS50 from Harvard online and I'm trying to use the functions from the cs50 library that are supposed to work automatically inside the cs50 appliance (Fedora virtual machine version 19-2). My problem is that when I #include <cs50.h>
and compile like he does in the lectures, I get an error message.
Here's a simple program from a lecture slide.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// ask user for input
printf("Give me an integer: ");
int x = GetInt();
printf("Give me another integer: ");
int y = GetInt();
// do the math
printf("The sum of %i and %i is %i!\n", x, y, x + y);
}
This is the error message I get:
jharvard@appliance (~/Dropbox/pset-1): ls
adder.c even-odd.c hello
jharvard@appliance (~/Dropbox/pset-1): clang -o adder adder.c
/tmp/adder-iuV3am.o: In function `main':
adder.c:(.text+0x19): undefined reference to `GetInt'
adder.c:(.text+0x32): undefined reference to `GetInt'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
jharvard@appliance (~/Dropbox/pset-1):
My guess is it's not finding the library for some reason; does anyone have any ideas what needs to be done to get everything talking?
I searched some other question with answers in reference to using a cs50.c file, but I think those were from people trying to compile on their own machines and not in the all-in-one appliance.
回答1:
You got to tell the compiler to link in the library by running either
clang -lcs50 -o adder adder.c
or simply
make adder
since they have make configured for you already.
回答2:
you might as well create a Makefile that would automatically link cs50 library from now on.
to do that make a new file with no extensions and call it Makefile;
in the Makefile simply write down this line: LDLIBS += -lcs50
.
further more, when you need to link more libraries in the future , do the same, for example LDLIBS += -lm
to add the math library ,etc.
I hope I could help you with this
来源:https://stackoverflow.com/questions/23749103/cs50-library-wont-link-to-file-in-cs50-appliance