Why "undefined symbols" on gcc when main includes header file? [duplicate] - c

This question already has answers here:
undefined symbol for architecture x86_64 in compiling C program [duplicate]
(1 answer)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
Why does main.c fail to compile when including sum_divisibles.h?
I am sure I am missing something silly, but can't find it for the life of me.
Compiling on Mac OSX.
sum_divisibles.h
#ifndef SUM_DIVISIBLES
#define SUM_DIVISIBLES
int sum_divisibles(int limit);
#endif /* SUM_DIVISIBLES */
sum_divisibles.c
#include <stdlib.h>
#include "sum_divisibles.h"
int sum_divisibles(int limit)
/* Returns sum of multiples of 3 and 5 less than limit. */
{
int mult_3, mult_5, sum = 0;
for (int ndx=0; (mult_3 = ndx * 3 ) < limit; ndx++) {
if ( (mult_5 = ndx * 5) < limit && mult_5 % 3 != 0 ) {
sum += mult_5 ;
}
}
return sum; }
main.c
#include <stdio.h>
#include "sum_divisibles.h" // does not compile
//#include "sum_divisibles.c" // compiles
int main(void)
{
printf("%d\n", sum_divisibles(1000));
}
Error -- on gcc -Wall main.c
Undefined symbols for architecture x86_64:
"_sum_divisibles", referenced from:
_main in main-e4e97
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Related

clang: _rotl32 not found for architecture arm64

On Apple M1, I'm trying to compile the following code:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
inline uint32_t rotl32(uint32_t x, int32_t bits)
{
return x<<bits | x>>(32-bits); // C idiom
}
uint32_t bad_hash32(char const *input) {
uint32_t result = 0xC0FF117;
while (*input) {
result ^= *input++;
result = rotl32(result, 5);
}
return result;
}
int main(int argc, char* argv[])
{
char const* const input = argv[1];
printf("%08x\n", bad_hash32(input));
return 0;
}
Command:
gcc bad_hash.c -o bad_hash
It produces the following error:
Undefined symbols for architecture arm64:
"_rotl32", referenced from:
_bad_hash32 in bad_hash-4c8a24.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What's the issue here? I tried to upgrade gcc to the latest.
Clang version:
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin21.2.0
Thread model: posix
Removing the inline specifier fixes the issue.
Though I still don't know why this happens.

How to fix undefined reference to pow in C Lion in Linux?

I have already taken a look at the question :Undefined reference to `pow' and `floor' but I'd like to know if there is anyway I could fix this permanently in my IDE which is Jet brains C Lion. I am using it on Pop OS. And I am very new to C as well as programming in general, it'd be better if I could just build and compile the program from my IDE rather than having to go to terminal every time.
Edit #1 : This is the code I am trying to compile rn. Its trying to find the reverse of a number rn.
#include <stdio.h>
#include <math.h>
int how_long(int x)
{
int count = 0;
while (x>0)
{
x /= 10;
count++;
}
return count;
}
int main()
{
int n = 1;
int num ;
int reverse_num = 0;
printf("Enter a number : ");
scanf("%i",&num);
int j = how_long(num);
while (num>0)
{
reverse_num += (num % 10) * (int)pow(10,j) ;
num -= (num%10) * (int)pow(10,n-1);
++n;
--j;
}
printf("%i",reverse_num);
return 0;
}
And the error I get on C Lion is :
[1/1] Linking C executable 5_2
FAILED: 5_2
: && /usr/bin/cc -g CMakeFiles/5_2.dir/main.c.o -o 5_2 && :
/usr/bin/ld: CMakeFiles/5_2.dir/main.c.o: in function `main':
/home/koustubhjain/CLionProjects/Assignments/5-2/main.c:30: undefined reference to `pow'
/usr/bin/ld: /home/koustubhjain/CLionProjects/Assignments/5-2/main.c:31: undefined reference to `pow'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
Edit #2 :
I found this old forum post on C Lion's website : https://intellij-support.jetbrains.com/hc/en-us/community/posts/206607085-CLion-Enabling-math-h-for-C-projects
but idk if it still works, and if it works, how do I add that to my CMake file ?
Here is my CMakeLists.txt file for reference :
cmake_minimum_required(VERSION 3.21)
project(5_2 C)
set(CMAKE_C_STANDARD 99)
add_executable(5_2 main.c)
Edit #3 : Edit #2 seems to have fixed it

Testing Dominion Card Game in C

I've been asked to write test programs on 4 functions in the card game dominion. I've written one (extremely simple) just to make sure I can get it to pass as I'm pretty new to testing. However, I continually get a syntax error at runtime that I cannot figure out.
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
int main() {
int r = 0, j = 0;
int adventurer = 8;
int greathall = 17;
r = getCost(adventurer);
assert(r == 6);
j = getCost(greathall);
assert(j == 3);
return 0;
}
When I compile it, I do get some warnings:
Undefined symbols for architecture x86_64:
"_getCost", referenced from:
_main in unittest1-7d7bf2.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Which I'm not sure about either, but the base code that we are given from our instructor, as well as all of the other code from my classmates, has these warnings as well.
However, when running I get the following error:
./unittest1.c: line 8: syntax error near unexpected token `('
./unittest1.c: line 8: `int main() {'
I've tried rewriting it in a blank file thinking there was some invisible characters or something but I still get this error. Does anyone see something wrong in my code? Any help is appreciated.
**getCost is called in dominion_helpers

CUDA linking Error undefined reference [duplicate]

This question already has answers here:
Cuda C - Linker error - undefined reference
(2 answers)
Closed 7 years ago.
I'm new to CUDA programming hence running into issues with compiling/ linking files. I'm trying to compile .c and .cu files.
Here are the files:
p3.c:
#include <stdlib.h>
#include <stdio.h>
extern void load_scheduler(int k, int j);
int blocks, threads;
int main(int argc, char* argv[])
{
if (argc > 1)
{
blocks = atoi(argv[1]);
threads = atoi(argv[2]);
}
else
exit(1);
load_scheduler(blocks, threads);
}
And scheduler.cu file:
#include <stdlib.h>
#include <stdio.h>
__global__ void sched_func()
{
int j = 6*5*threadIdx.x;
printf("%d\n",j);
}
void load_scheduler(int b, int n)
{
sched_func<<< b,n >>>();
}
I compile these two files using nvcc -c scheduler.cu p3.c and it seems fine
However, when I try to link these two files using nvcc -o cuda_proj scheduler.o p3.o, I get an error:
p3.o: In function `main':
p3.c:(.text+0x58): undefined reference to `load_scheduler'
collect2: ld returned 1 exit status
I may not be using the right steps to get this working, so if there's any other way I should try out, suggestions are welcome. I am also new to making Makefiles so want to stick to using nvcc commands on terminal.
Just added : extern "c" before load_scheduler definition. NVCC could not recognize the function definition as it belonged to .cu file, therefore the error.
extern "C"
void load_scheduler(int b, int n)
{
sched_func<<< b,n >>>();
}

linker reports undefined reference but code compiles OK [duplicate]

This question already has answers here:
gcc will not properly include math.h
(2 answers)
Closed 8 years ago.
I'm trying to learn enough c to satisfy my occasional need to write simple programs that answer specific questions I have. I've been following a tutorial and using Geany for ease of use. Instead, I can't seem to get the simplest program to run. Here is my source code:
#include <stdio.h>
#include <math.h>
int main(int argc, char **argv)
{
int x, y;
double c, sqr_c;
for (x = 10; x <= 31; x++)
{
for (y = 10; y <= 31; y++)
{
c = 1000 * x * x + y * y;
sqr_c = sqrt(c);
printf ("%f\n", sqr_c);
}
}
return 0;
}
It compiles fine (gcc -c) but when I try to build an executable, I get:
gcc "concsqr.c" -Wall -o "concsqr" (in directory: /home/chip)
/tmp/cccSmdZS.o: In function `main':
concsqr.c:(.text+0x4b): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
Compilation failed.
I read something about making sure the linker can locate the library where sqrt() is defined, but I do not know how to do that, and wouldn't it be in a standard location anyway? Why doesn't the linker already know where it is? It's a standard library for c.
You must try to compile your program with the -lm flag as libm.so is the math library, and the -l flag adds a lib prefix and .a or .so suffix.
gcc concsqr.c -Wall -o concsqr -lm

Resources