how to use clang compile c file with math.h? [duplicate] - c

This question already has answers here:
Undefined reference to sqrt (or other mathematical functions)
(5 answers)
Closed 9 years ago.
#include <stdio.h>
#include <math.h>
int main()
{
printf("%.81f\n", 1+2*sqrt(3)/(5-0.1));
return 0;
}
output:
/tmp/a4-4oU730.o: In function main':
a4.c:(.text+0x4f): undefined reference tosqrt'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Try adding -lm for libm for math to your build command. That said, your code works fine for me using clang 4.1 on Mac OS.

Related

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

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.

Square Root Program in C not Compiling [duplicate]

This question already has answers here:
Why do you have to link the math library in C?
(14 answers)
Closed 4 years ago.
Just starting to learn C, and I found this example program on a C tutorial website, and it is giving an error upon compiling.
Here is the program, calculates the square root of a number based on user input:
#include <stdio.h>
#include <math.h>
int main()
{
double num, root;
/* Input a number from user */
printf("Enter any number to find square root: ");
scanf("%lf", &num);
/* Calculate square root of num */
root = sqrt(num);
/* Print the resultant value */
printf("Square root of %.2lf = %.2lf", num, root);
return 0;
}
I compile it using gcc in Ubuntu:
gcc -o square_root square_root.c
And here is the error:
/tmp/cc9Z3NCn.o: In function `main':
square_root.c:(.text+0x4e): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
What am I doing wrong? I can see that the math module is imported, so why the error?
Again, I just started studying C today, and I just want to figure out how to get programs to run. Thank you for your patience as it must be something obvious.
sqrt lives in the math library, so you need to tell your program to link to it with -lm:
gcc -o square_root square_root.c -lm
You need to compile it with -lm flag
gcc -o square_root square_root.c -lm

gcc: fatal error: regex.h: No such file or directory [duplicate]

This question already has an answer here:
Regex.h for windows
(1 answer)
Closed 4 years ago.
For my C programs, I am using gcc.
Ultimately, I want to write a program that does stuff with regular expressions. Right now, however, my program simply outputs Hello World:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <regex.h>
int main(void) {
printf("Hello World\n", regex);
return 0;
}
Here's how I compiled that program:
gcc -std=c99 -o helloWorld helloWorld.c
That produces this error:
fatal error: regex.h: No such file or directory
I'm a C newbie. Any help would be much appreciated.
See what happens if you drop .h from regex.h

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

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