Issue
I'm trying to check if my installation of libfyaml was successful since I have to use the library in a project. I built it from source and the installation went fine, but when I compile the test program using gcc test.c -o test
, I get this error:
/usr/bin/ld: /tmp/ccPsUk6E.o: in function `main':
test.c:(.text+0x14): undefined reference to `fy_library_version'
collect2: error: ld returned 1 exit status
I checked the path and indeed libfyaml.h
is located in /usr/local/include
. The path is also fine, and GCC lists it as path for headers. I have tried using the -I
, -l
and -L
options, too. However, it still gives the same error.
How do I fix this issue? I'm on Ubuntu 20 (WSL).
Test program for reference:
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <libfyaml.h>
int main(int argc, char *argv[])
{
printf("%s\n", fy_library_version());
return EXIT_SUCCESS;
}
Link to library: Github
Solution
GCC has two major components: the compiler and the linker. Including the headers is the part you are doing right, and you are basically letting the compiler know that fy_library_version
is invoked with the correct arguments and return type.
However, the compiler does not know what happens after calling such function, and the duty of checking that any code for that function exists is deferred to a later stage: the linker.
The linker is indeed what is throwing the error you see
ld returned 1 exit status
This is because the linker does not know which libraries you are using and hence can't know where to find the code for you functions. To fix that you should tell gcc which libraries you are using:
gcc test.c -lfyaml -o test
Answered By - Fra93 Answer Checked By - Willingham (WPSolving Volunteer)