Truman Purnell
Truman Purnell

Reputation: 305

How to link all object files in a folder when compiling C?

So I have two folders:

/ffmpeg
/myproj

Inside myproj I have a main method:

#include <stdio.h>
#include <stdlib.h>
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"

int main(int argc, char *argv[])
{
    AVFormatContext *pFormatCtx = NULL;

    if (avformat_open_input(&pFormatCtx, argv[1], NULL, 0) != 0)
        return -1;

    return EXIT_SUCCESS;
}

I am attempting to compile this file like so:

cc main.c -I../ffmpeg ../ffmpeg/libavformat/utils.o

and am receiving this error:

"_ffio_set_buf_size", referenced from:
_ff_configure_buffers_for_index in utils.o
ld: symbol(s) not found for architecture x86_64

I understand what it's saying - I need to include the dependencies of utils.o, stored in file utils.d. But how do I do that on the command line? There are tons, tons, tons of dependencies and I know people don't type these manually!

Upvotes: 2

Views: 2653

Answers (3)

the kamilz
the kamilz

Reputation: 1988

You are going to wrong direction, you should link with shared libraries (libav). Add these lines to the your Makefile (and fix the path accordingly your setup):

LIBAV_PATH = /path/to/ffmpeg/lib/pkgconfig/
PKG_DEPS = libavformat libswscale libswresample libavutil libavcodec

CFLAGS  = `PKG_CONFIG_PATH=$(LIBAV_PATH) pkg-config --cflags $(PKG_DEPS)`
LDFLAGS = `PKG_CONFIG_PATH=$(LIBAV_PATH) pkg-config --libs $(PKG_DEPS)`

In PKG_DEPS I included many libraries, you may not need all of them, remove those unnecessary ones (but do it later - first try as is).

And your all: line should be something like:

all: main.c
        @gcc -I$(INCLUDE) main.c -o test.out $(CFLAGS) $(LDFLAGS)

Upvotes: 2

yvvijay
yvvijay

Reputation: 338

You will need to use a makefile to include all the directories that are required to build your binary. Here is a simple Makefile example:

INCLUDE = \
        $(shell find ~/code/src/root/ -type d | sed s/^/-I/)
all: main.c
        @gcc -I$(INCLUDE) main.c -o test.out
clean: 
        @rm test.out

Running make all will build main.c file and include all directories under ~/code/src/root/. Also, you can run find ~/code/src/root/ -type d | sed s/^/-I/ on your shell to see all the directories that are being included. Hope this helps!

Upvotes: 0

Ajay Brahmakshatriya
Ajay Brahmakshatriya

Reputation: 9203

You can use a Makefile to do all the work for you. In your case you can do something like -

HEADERS=$(wildcard ../ffmpeg/*.h)
DEPENDS=$(wildcard ../../ffmpeg/libavformat/*.o)
TARGET=a.out

all: $(TARGET)

main.o: main.c $(HEADERS)
    $(CC) main.c -c -I../ffmpeg -o main.o


$(TARGET): main.o $(DEPENDS)
    $(CC) $^ -o $@

Upvotes: 0

Related Questions