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
Related
I'm trying to learn c and I implemented a bubblesort function and i decided It would be better idea if i made a library that will contain various sorting algorithms, so I compiled my code with this:
gcc -shared -fPIC -o bin/bsort.o sort/Bubblesort.c
my bubblesort.c is working (and not related to question at all and there is nothing other than bubblesort function there):
// Licensed under public domain with no warranty
void bubblesort(int* array) {
//implemention goes here
}
my sort.h file:
void bubblesort(int* array);
my nsort.c
#include "sort/sort.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main() {
int* sortthis = malloc(1000*sizeof(int));
for(int i = 0; i < 1000; i++) {
*(sortthis+i) = random(); //random int is defined somewhere else
}
bubblesort(sortthis);
for(int i = 0; i < 90; i++) {
printf("%d ",*(sortthis+i));
}
free(sortthis);
return 0;
}
my script that i use to compile:
gcc -shared -fPIC -o bin/bsort.o sort/Bubblesort.c
gcc nsort.c sort/sort.h -Lbin/bsort.o -lm -o demo.elf
what could be i'm doing wrong, i tried various things but it didn't work, i kept getting following error:
/usr/bin/ld: /tmp/ccxhd5zd.o: in function `main':
nsort.c:(.text+0x23): undefined reference to `bubblesort'
collect2: error: ld returned 1 exit status
gcc --version (just in case if there is a bug in this version):
gcc (Debian 10.2.1-6) 10.2.1 20210110
You don't put -L before the .o file. -L is for adding directories that -l searches for libraries.
To link with an object file, just add it as an ordinary file argument.
You also don't need to include header files in the compiler arguments. The compiler reads them when it sees #include.
gcc nsort.c bin/bsort.o -lm -o demo.elf
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
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.
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
I have the following piece of code which uses ICU macros in order to determine UTF-8 string length:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unicode/utf.h>
size_t utf8_strlen( uint8_t* str, size_t length ) {
int32_t i = 0;
int32_t result = 0;
UChar32 cur;
while( i < length ) {
U8_NEXT( str, i, length, cur );
if( cur < 0 )
return -1;
result++;
}
return result;
}
However, when compiling and linking it as
cc `icu-config --ldflags` test.c
I get the following error:
/tmp/ccaVwSaO.o: In function `utf8_strlen':
test.c:(.text+0x141): undefined reference to `utf8_nextCharSafeBody_48'
collect2: ld returned 1 exit status
The command above expands to cc -ldl -lm -L/usr/lib -licui18n -licuuc -licudata -ldl -lm test.c, and libicuuc does have utf8_nextCharSafeBody_48 defined in it. Why does the linking error happen?
Try:
$ cc test.c $( icu-config --ldflags )
you typically need to list libraries last.