undefined reference to `BIO_s_mem' - c

Here's my code:
#include <openssl/bio.h>
int main (void)
{
BIO* bo = BIO_new(BIO_s_mem());
}
I'm compiling my code by doing gcc -lcrypto test.c.
Any idea what's wrong?

You have the arguments in the wrong order, try gcc test.c -lcrypto
See why order matters

Related

gcc - undefined reference to `libusb_init'

I'm trying to compile a piece of code which uses libusb:
#include <stdio.h>
#include <libusb-1.0/libusb.h>
#include <assert.h>
int main(void) {
libusb_context *context = NULL;
int rc = 0;
rc = libusb_init(&context);
assert(rc == 0);
libusb_exit(context);
return 0;
}
Upon compiling with gcc -lusb -lusb-1.0 sample.c -o sample the following errors emerge:
/tmp/ccr65JBT.o: In function `main':
sample.c:(.text+0x2e): undefined reference to `libusb_init'
sample.c:(.text+0x62): undefined reference to `libusb_exit'
collect2: error: ld returned 1 exit status
To make sure libusb is availible on my system:
raven#enforcer:~/sample$ pkg-config --libs libusb-1.0
-lusb-1.0
raven#enforcer:~/sample$ pkg-config --libs libusb
-lusb
I'm running Ubuntu 18.04 with gcc 7.3.0-16ubuntu3, how to fix?
Got it working by appending the flags after the .c: gcc sample.c -o sample -lusb -lusb-1.0
Autotools: sample_LDADD instead of sample_LDFLAGS
Thanks to Felix Palmen.

zlib and gdi32 with OpenSSL?

This is a follow up question to Undefined reference to RSA_generate_key in OpenSSL? I have an OpenSSL program to generate an RSA key, as follows :
#include <stdio.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
int main(int argc, char* argv[])
{
RSA *r = NULL;
int ret;
r = RSA_new();
BIGNUM *bne = NULL;
ret = RSA_generate_key_ex(r, 2048, bne, NULL);
return 0;
}
When I compile this with
gcc -I../include rsatest.c -lcrypto -L.
I get undefined reference errors to functions in libraries gdi32 and zlib.
So to correct this, I have to add -lz and -lgdi32, as
gcc -I../include/ rsatest.c -lcrypto -lz -lgdi32 -L.
My question is, aren't static libcrypto and libssl self sufficient? As in why am I having to add extra libraries to make this work? Is there an option which I should have added while compiling the library to prevent this?
I'm using a 64 bit Windows with a MinGW compiler. I made the OpenSSL library with the help of MSYS and make.

netfilter queue undefined reference to `nfq_open'

I have writting this code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <linux/types.h>
#include <linux/netfilter.h>
#include <libnetfilter_queue/libnetfilter_queue.h>
int main(int argc, char **argv)
{
struct nfq_handle *h;
printf("opening library handle\n");
h = nfq_open();
nfq_close(h);
exit(0);
}
and when I try to compile it says that:
/tmp/ccEv9MYS.o: In function `main':
test1.c:(.text+0x1a): undefined reference to `nfq_open'
test1.c:(.text+0x2a): undefined reference to `nfq_close'
collect2: ld returned 1 exit status
I tried checking if the library is found by gcc and it is (when I modifiy the incluse of libnetfilter_queue there is an error), I recompiled the library and made sur that the fonctions I'm calling are in in it.
If you have any clue thanks for helping
Icompile using this:
gcc -o test test1.c
I have also tried:
gcc -o test -lnetfilter_queue test1.c
gcc -o test -L/usr/local/lib test1.c
Well, from the gcc manual page, for the -llibrary linking option
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.
That says, the linker works from left to right, so need to put the dependent on left hand side.
You need to change your compilation statement to
gcc -o test test1.c -lnetfilter_queue

GCC does not recognise libxml2 functions

When trying to compile a simple parsing program using libxml2, GCC returns this error.
/tmp/ccuq3Hc1.o: In function `main':
xmltesting.c:(.text+0x31): undefined reference to `htmlCreatePushParserCtxt'
xmltesting.c:(.text+0x50): undefined reference to `htmlCtxtReadFile'
xmltesting.c:(.text+0x64): undefined reference to `xmlDocGetRootElement'
collect2: error: ld returned 1 exit status
Code:
#include <stdio.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
int main()
{
htmlDocPtr doc;
htmlNodePtr org_node;
htmlNodePtr curnt_node = NULL;
htmlParserCtxtPtr parser = htmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL, 0);
doc = htmlCtxtReadFile( parser, "html.txt", NULL, 0);
org_node = xmlDocGetRootElement( parser->myDoc );
for ( curnt_node = org_node; curnt_node; curnt_node = curnt_node->next ) {
if ( curnt_node->type == XML_TEXT_NODE ) {
printf ("%s", curnt_node->content );
}
}
}
It seems to be reading the structs fine, but the functions are not to be found?
Or is something wrong with my code?
You probably simply have to tack an -lxml2 at the end of the gcc build line.
As Dietricg Epp points out in the comments, if your library has the right .pc file pkg-config is the preferred way to get the neccesary flags. You can get both compile and link flags.
$ pkg-config libxml-2.0 --cflags --libs
-I/usr/include/libxml2 -lxml2

Calling functions from a shared library

I'm trying to call a "core" function from a shared library's function but I get:
./a.out: symbol lookup error: ./libtest.so: undefined symbol: testf
The code I'm using is very basic because I'm just getting into writing shared libraries and it's just for testing purposes:
main.h
extern void testf();
main.c
#include <stdio.h>
#include <dlfcn.h>
extern void testf()
{
printf("bla bla\n");
}
int main () {
void *handle = NULL;
void (*testlib)(void) = NULL;
handle = dlopen("./libtest.so" ,RTLD_LAZY);
testlib = dlsym(handle, "testfunc");
if ( testlib == NULL )
{
printf("Error: %s \n", dlerror());
}
else
{
testlib();
}
}
libtest.c
#include <stdio.h>
#include "main.h"
void testfunc() {
printf("Test plugin\n");
testf();
}
And the commands I compile it with:
gcc -fPIC -g -c -Wall libtest.c
gcc -shared -Wl,-soname,libtest.so.1 -o libtest.so libtest.o -lc
gcc main.c -ldl
Is it possible to achieve this? Tried to find the answer, but don't really know how to form the question right so I can search better for it.
Thanks!
here you are trying to call a function of a executable from the library. I think you actually required reverse of this.
Sorry, managed to find the answer:
I was compiling the main program with wrong parameters, should use:
gcc main.c -ldl -rdynamic

Resources