Link error with OpenSSL [duplicate] - c

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

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.

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

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.

undefined reference to `roundf' - C language

I'm new to C and I keep running into the same error. I am typing the code in nano text editor and compiling it in the linux terminal. My operating system is Ubuntu 14.04. Here's an example of code that won't compile,
#include <math.h>
#include <stdio.h>
int main()
{
float x = 23.33f;
x = roundf(x);
printf("%f\n", x);
return (0);
}
The error I am receiving is,
cc example.c -o example
/tmp/ccWV0Or8.o: In function `main':
example.c:(.text+0x1d): undefined reference to `roundf'
collect2: error: ld returned 1 exit status
make: *** [example] Error 1
Any help is much appreciated, thanks!
Link with the math library:
cc example.c -o example -lm
The math library functions are not part of standard C library which is linked by default. So you need link it yourself.
There's an interesting thread about why it's not part of libc:
Why do you have to link the math library in C?

gcc fails with "undefined reference" in Cygwin, but the reference exists in a linked library

I've built and installed libmarpa in Cygwin with the end result being in /usr/local/lib/libmarpa.a.
I have a simple file:
#include "libmarpa/dist/marpa.h"
int main() {
return marpa_check_version(8, 3, 0);
}
But the linker fails to find marpa_check_version:
$ gcc test.cc -L/usr/local/lib -lmarpa
/tmp/ccdYM1vV.o:test.cc:(.text+0x1e): undefined reference to `marpa_check_version(int, int, int)'
/tmp/ccdYM1vV.o:test.cc:(.text+0x1e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `marpa_check_version(int, int, int)'
collect2: error: ld returned 1 exit status
But the symbol exists as a function:
$ nm /usr/local/lib/libmarpa.a | grep marpa_check_version
0000000000002780 T marpa_check_version
So what's happening here? Is there a problem trying to do this within Cygwin, or am I invoking gcc incorrectly?
This symbol:
undefined reference to `marpa_check_version(int, int, int)'
is C++ mangled. This symbol is not:
0000000000002780 T marpa_check_version
The problem is that marpa.h developers did not expect their code to be used by C++, and have not put in proper guards for this. You can fix the problem like so:
extern "C" {
#include "libmarpa/dist/marpa.h"
}
int main() { ...as before ...
P.S. You should also change your command line to use g++ instead of gcc. Contrary to popular belief, they are not the same thing.

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