Trouble linking ffmpeg example source code - gcc

I managed to build ffmpeg and libx264 on my Ubuntu 11.10 machine from source.
I'm trying to work on the example source file decoding_encoding.c. The examples comes with a makefile, so I can just type in "make all" and it magically compiles and links everything. Now, I'm trying to compile and link from the command line but I can't seem to get it to link.
Here is the makefile:
# use pkg-config for getting CFLAGS abd LDFLAGS
FFMPEG_LIBS=libavdevice libavformat libavfilter libavcodec libswscale libavutil
CFLAGS+=$(shell pkg-config --cflags $(FFMPEG_LIBS))
LDFLAGS+=$(shell pkg-config --libs $(FFMPEG_LIBS))
EXAMPLES=decoding_encoding filtering metadata muxing
OBJS=$(addsuffix .o,$(EXAMPLES))
%: %.o
$(CC) $< $(LDFLAGS) -o $#
%.o: %.c
$(CC) $< $(CFLAGS) -c -o $#
.phony: all clean
all: $(OBJS) $(EXAMPLES)
clean:
rm -rf $(EXAMPLES) $(OBJS)
I can compile the source using this:
gcc -Wall -I/usr/local/include -c -o decoding_encoding.o decoding_encoding.c
When I try to link using this:
gcc -Wall -L/usr/local/lib -lavdevice -lavformat -lavfilter -lavcodec -lswscale -lavutil -o decoding_encoding decoding_encoding.o
At this point, I get a huge list of 'undefined reference error'
decoding_encoding.o: In function `audio_encode_example':
decoding_encoding.c:(.text+0x25): undefined reference to `avcodec_find_encoder'
decoding_encoding.c:(.text+0x6a): undefined reference to `avcodec_alloc_context3'
decoding_encoding.c:(.text+0xad): undefined reference to `avcodec_open'
decoding_encoding.c:(.text+0x1ce): undefined reference to `sin'
decoding_encoding.c:(.text+0x238): undefined reference to `avcodec_encode_audio'
decoding_encoding.c:(.text+0x297): undefined reference to `avcodec_close'
decoding_encoding.c:(.text+0x2a3): undefined reference to `av_free'
decoding_encoding.o: In function `audio_decode_example':
decoding_encoding.c:(.text+0x2f7): undefined reference to `av_init_packet'
decoding_encoding.c:(.text+0x30b): undefined reference to `avcodec_find_decoder'
decoding_encoding.c:(.text+0x359): undefined reference to `avcodec_alloc_context3'
decoding_encoding.c:(.text+0x379): undefined reference to `avcodec_open'
..... etc.
collect2: ld returned 1 exit status
Am I doing something wrong with my linker command? I've examined the makefile and I believe that I'm doing exactly what the makefile is....or am I?
Thanks,
Leo

The -l options should go after any other options.

Related

Makefile - include files not found when building with multiple source files

I have a make file:
# Makefile
CC = gcc
SRC = pixel.c
TARGET = pixel.exe
OBJECTS = bmp.o pixel.o
#CFLAGS_W = -Wall -Wl,-subsystem,windows
CFLAGS_W = -Wall
LFLAGS_W = -lSDL2
INCS_W = -I.\SDL2\include
LIBS_W = -L.\SDL2\lib\x64
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $(INCS_W) $(LIBS_W) $(CFLAGS_W) $(LFLAGS_W) -g -o $(TARGET) $(OBJECTS)
it will build bmp.c, but when it tries to build pixel.c I get the error:
pixel.c:6:10: fatal error: SDL.h: No such file or directory
however, pixel.c has been building fine all the time I was just building it on its own (before I added bmp.c), using the command:
all:
$(CC) $(SRC) $(INCS_W) $(LIBS_W) $(CFLAGS_W) $(LFLAGS_W) -g -o $(TARGET)
I really struggle with makefiles because I just don't use them enough, but this doesn't really feel like a "make" problem.
I'm using mingw32-make and gcc in a windows sandbox.
EDIT:
I have added
.c.o:
$(CC) $(INCS_W) $(LIBS_W) $(CFLAGS_W) $(LFLAGS_W) -g -c $< -o $#
to my makefile, which gets me a step further (I think), but now complains that all of the SDL functions are undefined
C:\Users\WDAGUtilityAccount\Desktop\Projects\Pixel>mingw32-make
gcc -I./SDL2/include -L./SDL2/lib/x64 -Wall -lSDL2 -g -c bmp.c -o bmp.o
gcc -I./SDL2/include -L./SDL2/lib/x64 -Wall -lSDL2 -g -c pixel.c -o pixel.o
gcc -I./SDL2/include -L./SDL2/lib/x64 -Wall -lSDL2 -g -o pixel.exe bmp.o pixel.o
pixel.o: In function `main':
C:\Users\WDAGUtilityAccount\Desktop\Projects\Pixel/pixel.c:119: undefined reference to `SDL_Init'
C:\Users\WDAGUtilityAccount\Desktop\Projects\Pixel/pixel.c:121: undefined reference to `SDL_GetError'
C:\Users\WDAGUtilityAccount\Desktop\Projects\Pixel/pixel.c:126: undefined reference to `SDL_CreateWindow'
C:\Users\WDAGUtilityAccount\Desktop\Projects\Pixel/pixel.c:131: undefined reference to `SDL_GetError'
...
I can get the whole project to compile if I just do this on its own:
SRC = bmp.c pixel.c
...
test:
$(CC) $(SRC) $(INCS_W) $(LIBS_W) $(CFLAGS_W) $(LFLAGS_W) -g -o $(TARGET)
which is fine, it'll do, but it rebuilds everything every time, and I'd like to know why it isn't working anyway :)

How do I link a archive file with C code?

I'm trying to link a static library archive file (libx/libx.a) with a C code. The library requires 2 flags (-lx -lpthread). After linking the static library my ultimate goal is to create a shared library. I have the following Make file,
rsa-engine: rsa/rsa.c rsa/bignum.c rsa/aes.c rsa/x509parse.c rsa/pem.c
gcc -fPIC -o rsa/rsa.o -c rsa/rsa.c
gcc -fPIC -o rsa/bignum.o -c rsa/bignum.c
gcc -fPIC -o rsa/aes.o -c rsa/aes.c
gcc -fPIC -o rsa/x509parse.o -c rsa/x509parse.c
gcc -fPIC -o rsa/pem.o -c rsa/pem.c
gcc -fPIC rsa-engine.c libx/libx.a -L.libx/ -lx -lpthread -o rsa-engine.o
gcc -shared -o librsa_engine.so -lcrypto rsa-engine.o rsa/rsa.o rsa/bignum.o rsa/aes.o rsa/x509parse.o rsa/pem.o
clean:
rm -f *.o rsa/*.o *.so rsa-engine
After using the make command it produces the following output,
/usr/bin/ld: cannot find -lx
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'rsa-engine' failed
make: *** [rsa-engine] Error 1
I found similar questions here. But that did not help. Seems like I can't make the link work. Any help with what I'm doing wrong?
I would like to achieve the same result generated by the following command,
CC := gcc
CFLAGS := -Wall -g -MD -O2 -I ../
LDFLAGS := -lx -lpthread
tests_files := hello
all: $(tests_files)
hello: hello.o ../libx/libx.a
$(CC) $(CFLAGS) -static $(<) -L../libx/ $(LDFLAGS) -o $(#)
It seems that your libx.a is located in libx, yet -L references .libx directory. Anyway, since you reference libx/libx.a directly, you may skip both -L.libx/ -lx and it should link just fine.

ld: undefined reference, but it should leave them unresolved

I am unable to produce a library, which works with another library (SDL). I am using MinGW for make, and ld to link. I am confused because a) it shouldn't be trying to link in these libraries, but do this later when someone links my library in as well; and b) even if I do link in the SDL libraries, it still can't find the SDL functions (SDL_GetTicks, SDL_Delay) it's looking form -- the errors are the same. Also note that some of the missing items are from std.
Here are the errors. As you can see, I'm trying various flags on ld to make it not try to resolve references, but w/o success yet.
C:\Users\...\mcve>make
g++ -c -c -I../../../external/SDL2/include -I../include -o mcve.o mcve.cpp
ld -G --unresolved-symbols=ignore-all --warn-unresolved-symbols -o libmcve.a mcve.o
C:\MinGW\bin\ld.exe: mcve.o:mcve.cpp:(.text+0x8): undefined reference to `SDL_GetTicks'
C:\MinGW\bin\ld.exe: mcve.o:mcve.cpp:(.text+0x23): undefined reference to `SDL_GetTicks'
C:\MinGW\bin\ld.exe: mcve.o:mcve.cpp:(.text+0x2f): undefined reference to `SDL_Delay'
C:\MinGW\bin\ld.exe: mcve.o:mcve.cpp:(.text+0x46): undefined reference to `std::ios_base::Init::~Init()'
C:\MinGW\bin\ld.exe: mcve.o:mcve.cpp:(.text+0x67): undefined reference to `std::ios_base::Init::Init()'
C:\MinGW\bin\ld.exe: mcve.o:mcve.cpp:(.text+0x73): undefined reference to `atexit'
Here's my source file:
#include <SDL.h>
#include <iostream> //If I take this out, I no longer get the
//unresolved references to std::ios_base::Init::Init,
// std::ios_base::Init::~Init, and atexit
Uint32 time;
void doSomething ()
{
if (time > SDL_GetTicks ())
SDL_Delay (time - SDL_GetTicks());
}
This is the Makefile. If I uncomment the rest of the LDFLAGS and let the SDL libraries link in, it does not change the output.
CFLAGS =-c -I../../../external/SDL2/include -I../include
LDFLAGS = --unresolved-symbols=ignore-all --warn-unresolved-symbols #-L. -lSDL2 -lSDL2_ttf -lSDL2_image -lSDL2_mixer
# Files
SOURCE_FILES= mcve.cpp
OBJECT_FILES= mcve.o
libmcve.a: $(OBJECT_FILES)
ld $(LDFLAGS) -o $# $^ -G
$(OBJECT_FILES): %.o: $(SOURCE_FILES)
g++ -c $(CFLAGS) -o $# $<
You are attempting to create the static library libmcve.a from the object file
mcve.o using the linker ld.
The linker cannot produce a static library. A static library is merely
an ar archive of object files that is produced by ar.
The recipe to create or update the static library in your makefile would be:
libmcve.a: $(OBJECT_FILES)
rm -f $# # Delete archive if already exists
ar rcs $# $^ # Recreate archive with contents $(OBJECT_FILES)
BTW, note that you are passing the -c option in your compilation commands
twice:
g++ -c -c -I../../../external/SDL2/include -I../include -o mcve.o mcve.cpp
That is because you have included it in your CFLAGS setting:
CFLAGS =-c -I../../../external/SDL2/include -I../include
(where it should not be), and also in your compilation recipe:
g++ -c $(CFLAGS) -o $# $<
(where it should be).

undefined reference to `__imp_socket'

I'm doing a socket project on win10, and I get these errors.
g++ -ggdb -std=c++11 -Wall -pedantic -o calcserver CalcServer.c
DieWithError.c HandleTCPClient.c CalcFramer.cpp CalcParser.cpp
C:\Users\HARRYS~1\AppData\Local\Temp\cceLC8Xb.o: In function `main':
D:\src/CalcServer.c:35: undefined reference to `__imp_socket'
D:\src/CalcServer.c:41: undefined reference to `__imp_htonl'
D:\src/CalcServer.c:42: undefined reference to `__imp_htons'
D:\src/CalcServer.c:45: undefined reference to `__imp_bind'
D:\src/CalcServer.c:49: undefined reference to `__imp_listen'
D:\src/CalcServer.c:58: undefined reference to `__imp_accept'
D:\src/CalcServer.c:64: undefined reference to `__imp_inet_ntoa'
collect2.exe: error: ld returned 1 exit status
make: *** [calcserver] Error 1
I tried to link Ws2_32.lib. I download Ws2_32.lib under directory src, and modify my makefile like this:
CC=g++
CFLAGS=-ggdb -std=c++11 -Wall -pedantic
LINKFLAGS = -L"D:\src" -lWS2_32
H_FILES=CalcFramer.hpp CalcParser.hpp
FILES=CalcServer.c DieWithError.c HandleTCPClient.c CalcFramer.cpp CalcParser.cpp
all: calcserver
calcserver: $(FILES) $(H_FILES)
$(CC) $(CFLAGS) -o calcserver $(FILES)
clean:
rm -rf calcserver
However, I still get the above errors. I've already changed all the sys/socket.h headers to Winsock.h and Winsock2.h. So I think it's not that part that leads me to the errors.

compiling using libnids

I have been trying to install libnids (Ubuntu LTS and Mac OS X) all the day and now I know how to compile programs with it.
I write this here because there is not much documentation but there are samples in the libnids downloaded folder with a makefile. The important things of this makefile are these:
CC = gcc
PCAPLIB = -lpcap
LNETLIB = -lnet
LIBS = -L../src -lnids $(PCAPLIB) $(LNETLIB) -lgthread-2.0 -lnsl
example:
$(CC) example.c -o example $(LIBS)
And if you're compiling it in Mac OS X ignore this: -lgthread-2.0 -lnsl -L../src
But I don't know if something stop to works because of these omitted things.
I found it. The best way is to compile the library and then use local reference to the ".a" file.
Makefile example
CC = gcc -g -Wall
GLIB = `pkg-config --cflags --libs glib-2.0`
PCAPLIB = -lpcap
LNETLIB = -lnet
LIBS_SRC = libnids-1.24/src/libnids.a
LIBS = $(PCAPLIB) $(LNETLIB) -lgthread-2.0
program: program.c
$(CC) -c $(CFLAGS) program.c -o program.o $(GLIB) $(LIBS)
$(CC) program.o -o program $(LIBS_SRC) $(GLIB) $(LIBS)
But if you want to compile the library and install it into your system you got to use -lnids instead of the above way. But the first solution always works.
I got errors when compiling samples under libnids:
gcc -o overflows overflows.o -L../src -lnids -lpcap -lnet -lgthread-2.0 -lnsl ../src/libnids.a
/usr/bin/ld: ../src/libnids.a(libnids.o): undefined reference to symbol 'g_async_queue_pop'
/lib/x86_64-linux-gnu/libglib-2.0.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: * [overflows] Error 1
You recommendation
GLIB = `pkg-config --cflags --libs glib-2.0`
solves my problem.
PS: my system Ubuntu 13.10. 64bit.

Resources