CS50 get_string error linker command failed with exit code 1 - c

I am doing Harvard CS50 c course. When I type make file in the terminal it throws me this error:
Undefined symbols for architecture arm64:
"_get_string", referenced from:
_main in hello-c42e93.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [hello] Error 1
hello.c:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string answer = get_string("What's your name? ");
printf("hello, answer\n");
}
I installed cs50.h library through this steps https://github.com/cs50/libcs50
I am using macOS Ventura 13.1
edit: here is the link of a cs50 library https://github.com/cs50/libcs50/tree/main/src

As #LHLaurini mentioned above you need to link with the cs50 library which contains the definition of the function get_string():
clang hello.c -o hello -lcs50
You didn't supply the Makefile but usually you can set this variable which is used in a implicit link rule:
LDLIBS := -lcs50

Related

Why do I keep getting a linker error even after compiling my header file?

I keep getting this error when compiling!
Undefined symbols for architecture x86_64:
"_lex", referenced from:
_main in main-7a45f7.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I am compiling a file main.c. Here are the first few lines of the program:
#include <stdio.h>
#include "lex.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
I thought compiling the header file "lex.h" might have been the issue, but I was apparently wrong. I ran the command
gcc -c lex.c lex.o
and received the following warning (but no errors):
clang: warning: lex.o: 'linker' input unused [-Wunused-command-line-argument]
I think I have included all the information needed, but these are the files in my current directory (as per ls):
lex.c lex.h lex.o main.c
Any help would be appreciated, thanks!
Thanks to Tom Karzes and Itagaki Fumihiko for help. The fatal flaw in my compilation was not linking main.o and lex.o. To compile the files properly, this is what you should actually do:
gcc -c lex.c
gcc -c main.c
gcc main.o lex.o

'make' command is not linking files/library properly in C

I am a beginner and trying to learn C.
I am looking at the Harvard edX course, but I am running into an issue. Whenever I run make test, it is resulting in the following error:
cc test.c -o test
Undefined symbols for architecture arm64:
"_get_string", referenced from:
_main in test-d904ae.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Error 1
Here is my code:
#include <stdio.h>
#include <cs50.h>
int main(void)
{
string name = get_string("enter: ");
printf("%s\n", name);
}
The get_string function is from the cs50.h. I know that there is way around this my using clang test.c -o test -lc50. However, it is being said that make does all of that, and I would like to know what is wrong so that I can use make instead of clang test.c -o test -lcs50.

Error on M1 Mac through GCC Compile in vim

I try to compile C files in vim environment but It occurs on error that
Undefined symbols for architecture arm64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
My command is
gcc [filename.c] -o [filename]
and my file is
#include <unistd.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}
It is just simple code for checking compile.
But it didn't work.
So I wondering that what is the critical problem that occurs problem.
My command or M1 mac?
Of course I install gcc through 'HOMEBREW'.
Thank you.

Eclipse C compiler throwing errors

I was trying to compile helloworld.c in Eclipse Oxygen.1 on Mac 10.12.6
#include <stdio.h>
#include <stdlib.h>
int main(){
printf("Hello World!");
return 0;
}
and it throws such an error:
20:41:02 **** Incremental Build of configuration Debug for project HelloWorld ****
make all
Building target: HelloWorld
Invoking: Cross GCC Linker
gcc -o "HelloWorld" ./helloworld.o
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [HelloWorld] Error 1
20:41:03 Build Finished (took 938ms)
I am quite sure that the problem isn't the code itself, but what?

ld: symbol(s) not found for architecture x86_64 (libusb)

I'm trying to compile the following libusb snippet on my Mac:
#include <stdio.h>
#include <stdlib.h>
#include <libusb.h>
int main(void) {
libusb_device **devices;
ssize_t device_count = 0;
device_count = libusb_get_device_list(NULL, &devices);
printf("%d devices found\n", (int)device_count);
return EXIT_SUCCESS;
}
I have libusb installed via Homebrew.
I'm getting the following error during compilation:
ld: symbol(s) not found for architecture x86_64
The full compiler output is as follows:
22:28:24 **** Incremental Build of configuration Debug for project libusb ****
make all
Building file: ../src/libusb.c
Invoking: Cross GCC Compiler
gcc -I/usr/local/Cellar/libusb/1.0.9/include/libusb-1.0/ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/libusb.d" -MT"src/libusb.d" -o "src/libusb.o" "../src/libusb.c"
Finished building: ../src/libusb.c
Building target: libusb
Invoking: Cross GCC Linker
gcc -o "libusb" ./src/libusb.o
Undefined symbols for architecture x86_64:
"_libusb_get_device_list", referenced from:
_main in libusb.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: *** [libusb] Error 1
22:28:24 Build Finished (took 119ms)
I understand the problem is to do with the linker not finding the libusb library, right? How do I tell the compiler where that is in Eclipse CDT?
As we worked out in all the comments the link command that worked is
gcc -L/usr/local/Cellar/libusb/1.0.9/lib -o "libusb_example" ./src/libusb_example.o -lusb1.0

Resources