undefined symbol for architecture x86_64 in compiling C program [duplicate] - c

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I have a simple program as
demo_use.c
#include "libhello.h"
int main(void) {
hello();
return 0;
}
libhello.h
void hello(void);
libhello.c
#include <stdio.h>
void hello(void) {
printf("Hello, library world.\n");
}
I have used the command in terminal as
gcc demo_use.c -o test
error
Undefined symbols for architecture x86_64: "_hello",
referenced from: _main in ccZdSQP3.o
ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status

You need to compile both the source files together to generate the binary. use
gcc demo_use.c libhello.c -o test
Otherwise, the definition of hello() function will be absent. So, at linking time, linker will throw undefined symbol error.

Related

1 I am doing the cs50 course and decided to run the code from there on my MacBook [duplicate]

This question already has an answer here:
CS50 get_string error linker command failed with exit code 1
(1 answer)
Closed 13 days ago.
I have the following code:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int x = get_int("whats x? ");
int y = get_int("whats y? ");
if (x < y)
{
printf("x is less than y\n");
}
}
It keeps giving me this error
moutarrjallow#Moutarrs-MBP cclang % make ccompare
cc ccompare.c -o ccompare
Undefined symbols for architecture x86_64:
"_get_int", referenced from:
_main in ccompare-91f0e5.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: *** [ccompare] Error 1
You need to link with the cs50 library by adding the -lcs50 to above. In your Makefile you can probably do that by setting LDLIBS := -lcs50.

Can't use "get_int" in libcs50 for C

So I'm currently new to C right now, and I just downloaded the cs50 library. I tried to make an input by using get_int(), but it's not working. Here's my code:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int i = get_int("Input: ");
printf("Output: %i\n", i);
}
This is the error that I got
cc calculator.c -o calculator
Undefined symbols for architecture x86_64:
"_get_int", referenced from:
_main in calculator-225c65.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: *** [calculator] Error 1

I am trying to implement a code using 'extern' keyword, IDE: VS Code (using code runner...) [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 months ago.
extrn.c
#include <stdio.h>
extern int var;
int main()
{
printf("%d", var);
return 0;
}
var.c
int var = 5;
I go to file extrn.c and I run the code and I get this:
undefined reference to `var'
and this is what my output is looking like:
[Running] cd "/home/buff/Documents/Coding/C/C_programming_NESO/" && gcc extrn.c -o extrn && "/home/buff/Documents/Coding/C/C_programming_NESO/"
/usr/bin/ld: /tmp/ccoKgi02.o: in function `main':
extrn.c:(.text+0xa): undefined reference to `var'
collect2: error: ld returned 1 exit status
[Done] exited with code=1 in 0.093 seconds
Compile your both C files together to fix this undefined reference error.
For GCC
gcc extrn.c var.c -o main
For clang
clang extrn.c var.c -o main

Compiling C program on Mac results in `Undefined symbols for architecture x86_64` error [duplicate]

This question already has answers here:
undefined symbol for architecture x86_64 in compiling C program [duplicate]
(1 answer)
Make Error: Undefined symbols for architecture x86_64
(1 answer)
Closed 7 years ago.
I am trying to compile a C program on my MacBook Pro, so far the files I have look something like this:
main.c
#include <stdio.h>
#include "blah.h"
int main(int argc, char *argv[])
{
//do stuff
return 0;
}
blah.h
extern void method1()
extern void method2()
extern void method3()
blah.c
#include <stdio.h>
#include <stdlib.h>
#include "blah.h"
void method1()
{
//do stuff
}
void method2()
{
//do stuff
}
void method3()
{
//do stuff
}
However, when I try to compile, I get an error like:
$ gcc main.c -o main
Undefined symbols for architecture x86_64:
"_method1", referenced from:
_main in ccVPIYad.o
"_method2", referenced from:
_main in ccVPIYad.o
"_method3", referenced from:
_main in ccVPIYad.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
I think this is because Mac compiler adds an underscore in front of every function name, but I'm not sure how to fix this. I've tried changing blah.h to this:
extern void method1() asm ("create_list");
extern void method2() asm ("print");
extern void method3() asm ("insert_front");
but it is not fixing the problem.
The actual function definitions are in another source file which you are not compiling with.
Do:
gcc blah.c main.c -o main
What #l3x wrote
Remove extern and add semicolons
Updated blah.h:
void method1();
void method2();
void method3();

Using nan() on solaris-sparc

#include <math.h>
int main() {
nan("");
}
The above code works on Linux with gcc fn.c -lm. It doesn't on solaris-sparc. How do I fix this?
The error message is:
Undefined symbol nan, first referenced in file /var/tmp//ccsdneUZ.o ld: fatal: Symbol referencing errors. No output written to test collect2: ld returned 1 exit status.

Resources