Linking to libx264 library from c code Ubuntu

1k Views Asked by At

I'm trying to write a small C application which uses the x264 API, and I'm having problems compiling the code with a link to the x264 libaray.

In the /project/ directory there are two sub-folders: /project/mycode/ and /project/x264-snapshot-20120120-2245.

I have installed x264 in the latter subdirectory using ./configure and then 'make'. As such the library I think I want to link to is /project/x264-snapshot-20120120-2245/libx264.a

In /project/mycode/ I have a single source code file (prototype.c), which has the following imports:

#include <stdio.h>
#include <inttypes.h>
#include "../x264-snapshot-20120120-2245/x264_config.h"
#include "../x264-snapshot-20120120-2245/x264.h"

As expected, if I try to compile without linking to the x264 library, I get an error:

/project/mycode: gcc -o prototype prototype.c
/tmp/cc5NwRTp.o: In function `main':
prototype.c:(.text+0x6c): undefined reference to `x264_param_default_preset'
prototype.c:(.text+0xf6): undefined reference to `x264_param_apply_profile'
collect2: ld returned 1 exit status

So I try to link the library I mentioned above, but it isn't found:

/project/mycode: gcc -o prototype prototype.c -I../x264-snapshot-20120120-2245/ -llibx264.a
/usr/bin/ld: cannot find -llibx264.a
collect2: ld returned 1 exit status

I've tried a few variations, like:

gcc -o prototype prototype.c -I../x264-snapshot-20120120-2245/ -l ../x264-snapshot-20120120-2245/libx264.a
gcc -o prototype prototype.c -I../x264-snapshot-20120120-2245/ -llibx264
gcc -I ../x264-snapshot-20120120-2245/ -llibx264.a -o prototype prototype.c

As is probably obvious by now, I'm fairly new to this, so I'm hoping there is an easy solution

1

There are 1 best solutions below

0
On

For anyone who looks at this in the future, the answer was:

gcc -pthread -o prototype -L../x264-snapshot-20120120-2245/ -lx264 -lm

-L specifies the directory of the library and -l specifies the name of the library, minus the 'lib' prefix and the '.a' suffix. The -lm and -pthread arguments are also needed for the x264 library.