Undefined reference to 'function' yet the header is included [duplicate] - c

This question already has answers here:
C program cannot find function which included in header file
(2 answers)
Closed 7 years ago.
I am playing around with the ci20 and flowcloud. I have downloaded their c library/sdk and have included the header.
The program is simply:
#include <stdio.h>
#include <stdbool.h>
#include <flow/flowcore.h>
int main (int argc, char*argv[])
{
printf("hello");
if(FlowCore_Initialise()) printf("init");
return (0);
}
but on compilation gcc -Wall test.c -o hello I get this error:
/tmp/cch4kocL.o: In function `main':
test.c:(.text+0x40): undefined reference to `FlowCore_Initialise'
collect2: error: ld returned 1 exit status
I am not 100% sure what is going on here.

You are getting a linker error. When compiling with gcc, you need to also specifiy the library you want to link to in addition to just using the proper #include 's.
The syntax for compiling with gcc is
$ gcc [options] [source files] [object files] [-Ldir] -llibname [-o outfile]
As you can see you can link the library by adding -l<name> to your command string.

Related

GCC error undefined reference to `sqrt' on changing command option order [duplicate]

This question already has answers here:
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Why does the order of '-l' option in gcc matter? [duplicate]
(3 answers)
Closed 1 year ago.
Why I am getting the following error
/tmp/ccuWdVB3.o: In function `test':
MyCode.c:(.text+0x1c): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
MyCode.c
#include<stdio.h>
#include<math.h>
int test(int input1)
{
int x = 8;
int z = sqrt(x);
}
int main(int argc, char * a[]) {
}
when running with command:
gcc -o a.out -lm MyCode.c
But when I am running the command
gcc MyCode.c -o a.out -lm
everything works fine. Why the order of "MyCode.c" cli option is important here ?
Or Am i doing something wrong?
Libraries are searched only once during the linking (they may contain millions of symbols and objects) and that is the reason why they should be specified as the last ones when all objects linker has to search for are already known. Searching the giant library for the symbols after every object file in the project would be extremely inefficient.

Program using ceil() in C [duplicate]

This question already has answers here:
C Failing to compile: Can't find math.h functions [duplicate]
(2 answers)
Closed 3 years ago.
I did the following code using ceil()
#include<stdio.h>
#include<math.h>
int main()
{
float val;
float cVal;
val=23.4;
cVal =ceil(val);
printf("ceil value:%f\n",cVal);
return 0;
}
I am getting th following error
In function main':
test1.c:(.text+0x1b): undefined reference toceil'
collect2: error: ld returned 1 exit status
What is wrong in this code??Please help!
I compiled it using makefile
>>cmake .
>>make
>>./hello.out
From https://askubuntu.com/a/745199/513302
If you are going to compile a C program with math.h library in Linux using gcc you will have to use –lm option in the compiler command-line
gcc xyz.c -o xyz -lm
The -lm option is actually -l (for "link a module") and m is shorthand for the built-in math library.

undefined reference errors: Compiling HPL Benchmark [duplicate]

This question already has answers here:
Undefined reference to 'pow' even though -lm is a compile flag. [C]
(2 answers)
Closed 3 years ago.
I noticed that when I use sin inside function the compiler don't recognize it, here is an example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
float sinus(float a){
return sin(a);}
int main(int argc, char **argv)
{
double a = sinus(2);
printf("%f \n", sin(2));
printf("%f", a);
return 0;
}
If I use it directly in main it works fine, but inside a user defined function it gives me this error undefined reference to sin.
For compiling I use gcc -Wall -lm -lc -lgcc -o "%e" "%f".
References to libraries typically go to the end of the command line, in particular after the sources have been specified:
gcc -Wall -o "%e" "%f" -lm
(specifing the C lib is not necessary, it is linked implicilty)
From the documentation:
-l library
[...]
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.

Link error with OpenSSL [duplicate]

This question already has answers here:
Can't link OpenSSL code
(3 answers)
Closed 8 years ago.
I have installed OpenSSL . I just want to run a program using OpenSSL.
Here is my program, taken from here .
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "openssl/aes.h"
int main(int argc, char* argv[])
{
AES_KEY aesKey_;
unsigned char userKey_[16];
unsigned char in_[16];
unsigned char out_[16];
strcpy(userKey_,"0123456789123456");
strcpy(in_,"0123456789123456");
fprintf(stdout,"Original message: %s", in_);
AES_set_encrypt_key(userKey_, 128, &aesKey_);
AES_encrypt(in_, out_, &aesKey_);
AES_set_decrypt_key(userKey_, 128, &aesKey_);
AES_decrypt(out_, in_,&aesKey_);
fprintf(stdout,"Recovered Original message: %s", in_);
return 0;
}
While compiling the program I got the same error messages as there, but the solution provided there is not working for me.
I am still getting compile error.
$ gcc -I/home/bholanath/Sources/openssl-1.0.1e/include/ op.c -lcrypt
/tmp/ccvHr9Jr.o: In function `main':
op.c:(.text+0x9c): undefined reference to `AES_set_encrypt_key'
op.c:(.text+0xbc): undefined reference to `AES_encrypt'
op.c:(.text+0xd7): undefined reference to `AES_set_decrypt_key'
op.c:(.text+0xf7): undefined reference to `AES_decrypt'
collect2: error: ld returned 1 exit status
$ gcc op.c -lcrypt
/tmp/ccDEZMog.o: In function `main':
op.c:(.text+0x9c): undefined reference to `AES_set_encrypt_key'
op.c:(.text+0xbc): undefined reference to `AES_encrypt'
op.c:(.text+0xd7): undefined reference to `AES_set_decrypt_key'
op.c:(.text+0xf7): undefined reference to `AES_decrypt'
collect2: error: ld returned 1 exit status
Any help to remove compilation error and run my program will be great.
I am using GCC under Fedora linux.
The OpenSSL library names are libcrypto and libssl. Try linking them. libcrypt is part of glibc.
Also, your code is invalid.
Your error is that you're linking with -lcrypt rather than -lcrypto, quite simply.
libcrypt is a small part of glibc that provides the standard Unix functions crypt(3) and the like, and is not related to OpenSSL at all.
For your information it's an linker error because it is searching for the object files but unable to find those. while compiling you are passing wrong library name. You should pass -lcrypto instead of -lcrypt

Why am I getting this error when trying to use log from math.h in C? [duplicate]

This question already has answers here:
Why do you have to link the math library in C?
(14 answers)
Closed 9 years ago.
I need to use logs in a program for an assignment. I ran this test program on my machine to see how the log function works (and if it would), and I get the following error during build.
Code
/* log example */
#include <stdio.h> /* printf */
#include <math.h> /* log */
int main()
{
double param, result;
param = 5.5;
result = log (param);
printf ("log(%f) = %f\n", param, result );
return 0;
}
ERROR
gcc -Wall -o "test" "test.c" (in directory: /home/linux/Development/codetest)
/tmp/ccCDqX7x.o: In function `main':
test.c:(.text+0x1b): undefined reference to `log'
collect2: ld returned 1 exit status
Compilation failed.
Link
This is C99 code, grabbed from this tutorial site.
Add -lm to your compilation command to link in the math library.
gcc -Wall -o "test" "test.c" -lm

Resources