undefined reference to `SHA1' at line - c

I try to compile a library on linux. this libary uses <openssl/sha.h> library. I have included this library in source file. After that, i use flag -lssl and flag -lcrypto to compile this project. So here is my command :
gcc -g -Wall -lssl -lcrypto -o bt_client file_a.c file_b.c
But I meet error :
undefined reference to `SHA1' at line 130
Code at line 130 is :
SHA1((unsigned char *) null_padded_name, 20, (unsigned char *)name_sha1);
Do I miss something ? Please correct me. Thanks :)

Try this:
gcc -g -Wall -o bt_client file_a.c file_b.c -lssl -lcrypto
If you are sure that symbol SHA1 exists in libssl.so or libcrypto.so.

When you link your application, the linker looks for dependencies in the order you give them on the command line.
So if you add a library (like -lssl) before the source/object file that depends on that library, the linker will not find any dependencies and ignore the library.
This means that you must always put libraries last on the command line.

You need to provide -lssl and -lcrypto at the end of the command line:
gcc -g -Wall -o bt_client file_a.c file_b.c -lssl -lcrypto

Related

undefined reference to `explain_read' ...... No such file or directory

I need to include libexplain to my project to do certain job. I install it and add the header libexplain/read.h to my code, so far so good and no error reported by the complier. But when I use the function explain_read() provided by libexplain and build the project it says:
/tmp/cc7NjAw0.o: In function `xl45_read(int, unsigned char*)':
connections.cpp:(.text+0x2f): undefined reference to `explain_read'
collect2: error: ld returned 1 exit status
and the build script is:
#!/bin/bash
echo > ./stdafx.h
g++ -O1 -Wall -o ./local_proxy (*.cpp...here is the source file list) -lz -lpthread -lpcap -L/usr/local/lib
actually when I type
whereis libexplain
in terminal, I get
libexplain: /usr/lib/libexplain.so /usr/lib/libexplain.a /usr/include/libexplain
I do a lot of searches and still have no idea what's going wrong. ):
You need to link your object files with libexplain. You can do it using the -l<library name>, like so:
g++ -O1 -Wall -o ./local_proxy *.cpp -lz -lpthread -lpcap -lexplain -L/usr/local/lib
Note the -lexplain flag. For a library with the a file name like libABC.so, you'd use -lABC to refer to that library. The documentation for link options with GCC can shed more light on it.

Linking libssl and libcrypto in GCC [duplicate]

This question already has an answer here:
Errors that refer to a bunch of unresolved OpenSSL symbols that clearly exist?
(1 answer)
Closed 6 years ago.
I'm attempting to use OpenSSL's EVP interface to do some encryption. I'm pretty sure my code is right, but I can't seem to get it to compile. I'm using GCC, and Ubuntu 32-bit precise with libssl-dev installed and at the latest version.
The project currently consists of one file, program.c.
#include <openssl/evp.h>
...
i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1() ... );
...
EVP_CIPHER_CTX_init(e_ctx);
among other various calls.
Here is how I invoke gcc:
gcc -Wall -g -lssl -lcrypto -o program program.c
Then I get output like this
/home/andy/program/program.c:31: undefined reference to `EVP_sha1'
/home/andy/program/program.c:31: undefined reference to `EVP_aes_256_cbc'
/home/andy/program/program.c:31: undefined reference to `EVP_BytesToKey'
/home/andy/program/program.c:44: undefined reference to `EVP_CIPHER_CTX_init'
So the include is clearly working:
andy#ProgStation2:/usr/include$ find . | grep evp.h
./openssl/evp.h
Here is the output of locate libcrypto. My best guess is that this is a stupid location for it and is why my link is failing, so I tried -L/usr/lib/i386-linux-gnu before -lcrypto with no luck as well.
/lib/i386-linux-gnu/libcrypto.so.1.0.0
I'm kind of stumped. If anyone wants to make me feel like a fool, I'd be very excited to figure out what i'm doing wrong!
It turns out it was something stupid. In the linker step, I was using gcc -Wall -g -lssl -lcrypto -o program program.o. I needed to move the library links to after the object file I was linking, and put libssl before libcrypto:
gcc -Wall -g -o program program.o -lssl -lcrypto
Try including headers using -I option, Look into directory for library using -L and finally specifying the library name with -l
Just making guess here, please specify path based on actual location.
gcc -g -Wall -L/usr/lib -I/usr/include -lssl -lcrypto -o program program.c
Hope it may help.

Linking Math Library in GCC 4.6.1 (Ubuntu 11.10)

I find a problem in the linking process of my application. I did not have the same with gcc 4.5. It tries to link math library with the following command.
gcc -Wall -Wno-unused -MD -o mems_seektest mems_seektest.o -lm -L. -g -DASSERTS -I../src// -I../ -I../src//src -DDEBUG -lmems_internals
and report following error massages:
undefined reference to `sqrt'
Any idea ?
recent gcc/ld uses the --as-needed linker flag as default. Practically, that means libraries have to be specified in the reverse order of dependencies on the command line. If the mems_internals library needs the sqrt function your -lm after -lmems_internals.
gcc -Wall -Wno-unused -MD -o mems_seektest mems_seektest.o -L. -g -DASSERTS -I../src// -I../ -I../src//src -DDEBUG -lmems_internals -lm
I've had the same problem with gcc 4.6.1, even with only one library. This doesn't work:
$ gcc -lm eg.o -o eg
eg.o: In function `foo':
/home/nick/tmp/eg.c:5: undefined reference to `sqrt'
collect2: ld returned 1 exit status
But this does:
$ gcc eg.o -o eg -lm
I hit this because I was using "LDFLAGS=-lm" in my Makefile. Works fine if you use "LDLIBS=-lm" instead.
You didn't tell us what -lmems_internals is, but maybe the unresolved symbol comes from there. The order of the -l options is generally important to the linker, you should always put system libraries last.
You can check where the unresolved symbol comes from by using something like
nm yourLibrary | grep sqrt
if there is a U in front of sqrt the symbol is undefined.
I'd say the linker is using the wrong libm.

Undefined reference to SSL_library_init and SSL_load_error_strings

I am implementing a OpenSSL code and have already included required header files but still I am getting errors like *
undefined reference to SSL_library_init
I guess it's a linking error rather than a compilation error.
I am implementing it in Linux box using slickeditor.
Link against libssl and libcrypto. Your LDFLAGS and LDLIBS would be as follows. Order matters for LDLIBS:
LDFLAGS = -L/usr/local/ssl/lib
LDLIBS = -lssl -lcrypto
Don't worry about adding the "lib" in front of library name, or the "so" or "a" suffix. The linker will do it for you.
If you are building from the command line, then you would use the following. Again, order matters.
gcc foo.c -o foo.exe -L/usr/local/ssl/lib -lssl -lcrypto
If you are using the system's OpenSSL, then you can omit -L/usr/local/ssl/lib.
For me this meant to install
apt install libssl1.0-dev
These methods are deprecated in OpenSSL 1.1. You don't need to use it more. You can just remove it. More info in OpenSSL manual.
ldd libssl.so -> libcrypto.so.1.1 => not found
sudo ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
libcrypto.so.1.1 => /lib64/libcrypto.so.1.1 (0x00007f17d46c7000)
Simply add -lssl -lcrypto to your Makefile and it should work.
Example of Makefile:
foo: foo.o
g++ -std=c++17 -o foo foo.cpp -lcrypto -lssl

librrd link problem

i use rrd (graphic programming ) under rrdtool, i have installed; and i rund gcc
gcc -I/usr/include -I/usr/local/include -L/usr/lib -L/usr/local/lib -lrrd -o myprog test.c
myprog is execute file
and test.c ist testprogram who i use function rrd_create from libary but gcc put out error like this:
/usr/bin/ld: cannot find -lrrd
why!!!!
This error message indicates that the linker cannot locate librrd.a or librrd.so* in /usr/lib/ or /usr/local/lib. You should make sure that you have librrd installed, and not just some other binary rrd package.

Resources