why is it showing undefined reference to `add'? [duplicate] - c

This question already has answers here:
C error: undefined reference to function, but it IS defined
(6 answers)
Closed 9 months ago.
I have written a code that just basically add two numbers. file is the self made header file
named xoxo.h
extern int add(int r,int m);
This is my second file that contains the function defination of the function add.The name is
run.c
#include "xoxo.h"
int add (int i,int f) {
return (i+f);
}
This is my main file tester.c
#include "xoxo.h"
#include <stdio.h>
void main() {
printf("%d",add(1,2));
}
The error is shown as
PS C:\Users\HOME\Desktop\New folder> gcc tester.c
C:\Users\HOME\AppData\Local\Temp\ccBwWXFk.o:tester.c:(.text+0x1e):
undefined reference to `add' collect2.exe: error: ld returned 1 exit
status
plz help

When you compile the application, you need to provide run.c as well, otherwise the application cannot find the implementation of add.
Run gcc tester.c run.c instead.

Related

Compiling C programs with static files

I am trying to compile a c program with a static library and its not working .
This is the error :
undefined reference to `calculatearea'
collect2.exe: error: ld returned 1 exit status .
The static files were made with the gcc / g++ compilers .
This is the main code :
#include <stdio.h>
#include <stdint.h>
int calculatearea(int a , int b);
int main()
{
int c = calculatearea(2,4);
printf("%d",c);
getchar();
return 0;
}
edit :
: screenshot of compiler error
From the above code we can see that you have declared the function int calculatearea(int a , int b); but have not written any definition for the same. and you are calling this function in the main. compiler is not finding the definition for the function calculatearea and giving error.
To solve this:
1) Write the definition for function calculatearea in the same file.
2) Make use of extern specifier with this function declaration and make sure that definition is present with the link library at the time of compilation.
3) As mentioned in the picture if the area.o have the definition of function calculatearea, then compile as below, this will generate a.out in linux:
gcc filename.c area.o

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 >>>();
}

Cygwin showing error iostream.h is unable to locate [duplicate]

This question already has answers here:
fatal error: iostream.h no such file or directory [duplicate]
(3 answers)
Closed 8 years ago.
I wanted to run C programs on windows in order to achieve this I downloaded cygwin(Linux like environment for windows) made a program and kept it on a directory called ..\cygwin\home\Computer
Code goes here
#include<iostream.h>
void main(){
printf("Hai");
}
When i am trying to execute this program using command prompt.
$ g++ hai.c
Its throwing out an error
hai.c:1:21: fatal error: iostream.h: No such file or directory
#include<iostream.h>
^
compilation terminated.
What is going on any idea?
change to
#include <iostream>
#include <cstdio>
int main(){
printf("Hai");
}
or with g++ -x c hai.c or gcc hai.c
#include <stdio.h>
int main(){
printf("Hai");
}

library function error in C [duplicate]

This question already has answers here:
Undefined reference to `pow' and `floor'
(6 answers)
Closed 9 years ago.
I wrote this function
int *constructST(int arr[], int n)
{
// Allocate memory for segment tree
int x = (int)(ceil(log2(n))); //Height of segment tree
int max_size = 2*(int)pow(2, x) - 1; //Maximum size of segment tree
int *st = malloc(max_size*sizeof(int));
// Fill the allocated memory st
constructSTUtil(arr, 0, n-1, st, 0);
// Return the constructed segment tree
return st;
}
and I have included the following libraries math.h ,stdlib.h, stdio.h but I get the following error
/tmp/ccg4X72c.o: In function `constructST':
tree.c:(.text+0x3f4): undefined reference to `log2'
tree.c:(.text+0x40b): undefined reference to `ceil'
tree.c:(.text+0x433): undefined reference to `pow'
collect2: error: ld returned 1 exit status
Any help why I am getting this error though I have included the math.h .
Including <math.h> ensures that your program knows the function prototypes for these functions, so it can tell that you're calling them correctly, but it doesn't actually link the library code in to them. For that you will have to add the right linker flag when you build it, usually -lm:
gcc -o myprog myprog.c -lm

Linker error in C when using Postgres [duplicate]

This question already has answers here:
Error when connecting to Postgres database in C - using libpq-fe.h
(2 answers)
Closed 9 years ago.
I have the following code:
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
int main(int argc, char* argv[])
{
//Start connection
PGconn* connection = PQconnectdb("host=webcourse.cs.nuim.ie dbname=cs621 sslmode=require user=ggales password=1234");
if (PQstatus(connection) ==CONNECTION_BAD)
{
printf("Connection error\n");
PQfinish(connection);
return -1; //Execution of the program will stop here
}
printf("Connection ok\n");
//End connection
PQfinish(connection);
printf("Disconnected\n");
return 0;
}
When I run it, I get the following error:
/tmp/cc73kO0N.o: In function `main':
main.c:(.text+0x15): undefined reference to `PQconnectdb'
main.c:(.text+0x25): undefined reference to `PQstatus'
main.c:(.text+0x40): undefined reference to `PQfinish'
main.c:(.text+0x5d): undefined reference to `PQfinish'
collect2: error: ld returned 1 exit status
This is strange, as PQconnectdb etc are all functions that are defined in libpq-fe.h, which I have already included in the code.
Any help would be great thanks.
#include <libpq-fe.h> does not link to the library, it only includes information about the functions and data types that the library provides.
You must tell the linker where the references that are declared in libpq-fe.h can actually be found.
If you are using a Makefile to compile you code you should add -lpq to your LDFLAGS or linking command.
Post the command you are running to compile to give us more information.

Resources