'make' command is not linking files/library properly in C - 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.

Related

CS50 get_string error linker command failed with exit code 1

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

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

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?

Make Error: Undefined symbols for architecture x86_64:

I've just started programming in C and decided to use make to build my sample app. Compiling the files using the GCC command works fine, however, using make fails with an error. If make just executes the gcc command under the hood, I don't see why this is even possible.
Here are the list of files:
makefile.make:
app.o: app.c helper.h
gcc -c app.c
helper.o: helper.h helper.c
gcc -c helper.c
app: app.o helper.o
gcc app.o helper.o -o app
app.c
/*
* file: app.c
*/
#include "helper.h"
int main() {
do_something();
return 0;
}
helper.h
/*
* helper.h
*/
void do_something();
helper.c
/*
* file: helper.c
*/
#include <stdio.h>
#include "helper.h"
void do_something() {
printf("Test\n");
printf("Testsss\n");
}
The Problem: Running "make app" throws the error message
Undefined symbols for architecture x86_64:
"_do_something", referenced from:
_main in app.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: *** [app] Error 1
Things Ive tried that works:
Compiling and Linking manually using gcc (using the commands from
make executed serially)
Using "make app.o" and "make helper.o" then running "gcc app.o helper.o -o app" compiles and links the app correctly
rename "makefile.make" to "makefile" or "Makefile". "app.o" and "helper.o" seems to work but i think thats just make doing its default instructions.

Resources