Cannot find library using gcc -shared in Eclipse to build a dll - c

I am trying to use the FFMPEG library that I just compiled on Windows using MinGW in a C program in Eclipse. I have managed to build a simple HelloJNI.c file and run it:
gcc $(INCLUDES) -c -g -w HelloJNI.c
gcc -shared -o $(BIN_DIR)/hello.dll HelloJNI.o
I am now trying to compile a ffmpeg_native.c file that uses FFMPEG but I am getting errors running the following:
LIBRARY_PATH = -L:"c:/Dev/msys-1.0/local/lib" -L:"c:/Dev/msys-1.0/local"
INCLUDES = -I$(SRC_DIR) -I"c:/Dev/msys-1.0/local/include" -I"c:/Program Files/Java/jdk1.8.0_45/include" -I"c:/Program Files/Java/jdk1.8.0_45//include/win32"
BIN_DIR = ../bin
gcc $(INCLUDES) -c -g -w ffmpeg_native1.1.4.c
gcc -shared -o $(BIN_DIR)/exportnative.dll ffmpeg_native1.1.4.o $(LIBRARY_PATH) -lffmpeg -lavcodec -lx264 -lavformat -lavutil -lswscale
The first line runs fine but the second shows this output:
c:/Dev/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.3/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lavcodec
[etc for all libraries]
I am puzzled as I am able to run the following in MinGW with success:
ld -o /local/libffmpeg.so -L/local/lib -lavcodec -lx264 -lavformat -lavutil -lswscale

It cannot find library path since -L is missing before $(LIBRARY_PATH). From gcc man:
-Ldir Add directory dir to the list of directories to be searched for -l.
So, if LIBRARY_PATH contains directory with needed libraries and everyting other is fine the following should work:
gcc -shared -o $(BIN_DIR)/exportnative.dll ffmpeg_native1.1.4.o -L$(LIBRARY_PATH) -lffmpeg -lavcodec -lx264 -lavformat -lavutil -lswscale
It is not needed to put colon : between -L and the path. The following list should be added to the command line:
-L"c:/Dev/msys-1.0/local/lib" -L"c:/Dev/msys-1.0/local"`
Note that it is better to use some other environment variable name for building the command line, since LIBRARY_PATH is used by gcc directly:
LIBRARY_PATH
The value of LIBRARY_PATH is a colon-separated list of directories,
much like PATH. When configured as a native compiler, GCC tries the
directories thus specified when searching for special linker files, if
it can't find them using GCC_EXEC_PREFIX. Linking using GCC also uses
these directories when searching for ordinary libraries for the -l
option (but directories specified with -L come first).

Related

Building a C application against an external library on linux

I'm working on a Linux project in C consisting of two different open source applications. "Project A" (libduo) creates an archive used for linking a couple test programs and creates the library like this:
/usr/bin/ar rv libduo.a duo.o http_parser.o https.o match.o parson.o urlenc.o
/usr/bin/ar: creating libduo.a
a - duo.o
a - http_parser.o
a - https.o
a - match.o
a - parson.o
a - urlenc.o
ranlib libduo.a
One of the libduo test programs is built like this:
gcc -g -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -I. -I. -DDUODIR=\"/usr/local/duo/libduo/etc\" -DHAVE_CONFIG_H -c test-duologin.c
gcc -o test-duologin test-duologin.o -L. -lduo -lssl -lcrypto
"Project B" is an OpenLDAP module which I've built with -lduo and a few options to tell it where to find things:
(cd .libs && rm -f pw-apr1.la && ln -s ../pw-apr1.la pw-apr1.la)
../../../libtool --mode=compile gcc -g -O2 -Wall -I../../../include -I../../../include -I../../../servers/slapd -I../../../contrib/slapd-modules/passwd/libduo -c pw-duo.c
gcc -g -O2 -Wall -I../../../include -I../../../include -I../../../servers/slapd -I../../../contrib/slapd-modules/passwd/libduo -c pw-duo.c -fPIC -DPIC -o .libs/pw-duo.o
gcc -g -O2 -Wall -I../../../include -I../../../include -I../../../servers/slapd -I../../../contrib/slapd-modules/passwd/libduo -c pw-duo.c -o pw-duo.o >/dev/null 2>&1
../../../libtool --mode=link gcc -g -O2 -Wall -version-info 0:0:0 \
-rpath /usr/local/libexec/openldap -module -o pw-duo.la pw-duo.lo libduo.a -lduo
*** Warning: Linking the shared library pw-duo.la against the
*** static library libduo.a is not portable!
cc -shared .libs/pw-duo.o libduo.a -lduo -Wl,-soname -Wl,pw-duo.so.0 -o .libs/pw-duo.so.0.0.0
/usr/bin/ld: libduo.a(duo.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
libduo.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
Makefile:51: recipe for target 'pw-duo.la' failed
make: *** [pw-duo.la] Error 1
The Makefile I'm using is the same one distributed with the OpenLDAP project. I've just added a section in the Makefile to build my module, using the same options for the other modules already there but adding -lduo to my section along with the paths to the libduo includes and libduo.a.
As make suggests above, I've recompiled by adding -fPIC after the -Wall option but it the same error was repeated. As a last resort, I tried adding -static to the module build but make was having none of that either:
*** Warning: Linking the shared library pw-duo.la against the
*** static library libduo.a is not portable!
This is the first time I've tried to build a C application against a lib not in the standard Linux locations so not exactly sure what's going on. I suspect libduo is built intended to be statically linked into everything, but the OpenLDAP modules are designed to use shared libraries. Can anyone elucidate?
Update: with help of comments below and this link I created a shared library from the .o files and distributed/built against that.

Compiling a C project along with two libraries

I am working on a C project for Linux-environment (compiled with gcc). I am using two libraries:
SDL image (source is stored at the directory SDL_img).
SDL TTF (source is stored at the directory SDL_ttf).
My CFLAGS variable:
CFLAGS = -std=c99 -pedantic-errors -Wall -g -lm `sdl-config --cflags` -ISDL_img -ISDL_ttf
As you can see, I am including those two library-directories.
My gcc command include the following:
`sdl-config --libs` -lSDLmain -lSDL -lSDL_img -lSDL_ttf
Finally, in my project I have the following includes:
#include "SDL_ttf.h"
#include "SDL_image.h"
For some reason, I get errors of the type:
undefined reference to 'IMG_Load'
Why?
EDIT:
all: Chess.o Commons.o Console.o Controls.o Coords.o File.o GameState.o GUI.o Keyboard.o List.o Minimax.o Move.o Piece.o SettingsState.o Slots.o Square.o Str.o
gcc $^ -lm -std=c99 -pedantic-errors -g -o Chess `sdl-config --libs` -lSDLmain -lSDL -lSDL_img -lSDL_ttf
Just cd to those src/SDL_xxx dirs and do make to build those libraries and then set the correct -L and -I. It failed on linking so the -I is "good" but your path to your libraries is incorect
basically linker is now searching its paths to find libSDL_img and libSDL_ttf then it also searches /usr/lib/x86_64-linux-gnu and it still can not find it, that is why you are getting undefined referecne.
To fix this: first build those SDL_xxx and then search where are those libraries and pass somethig like this along with other to gcc:
-Lsrc/SDL_img/lib -lSDL_img -Lsrc/SDL_ttf/lib -lSDL_ttf

Error compiling C code using MinGW-w64 in Windows 7

I'm trying to compile code from a backtrace project https://code.google.com/p/backtrace-mingw/ which is written for MinGW, but using MinGW-w64.
My old install and fresh install of MinGW-w64 produce the same problem. Path is set in path variables, and also in command prompt:
C:\mingw-w64\i686-4.9.2-win32-sjlj-rt_v3-rev1\mingw32\bin
and C:\mingw-w64\i686-4.9.2-win32-sjlj-rt_v3-rev1\mingw32 although this one isn't needed.
This is the makefile of that project:
.PHONY: all clean
all : backtrace.dll test.exe
backtrace.dll : backtrace.c
gcc -O2 -shared -Wall -o $# $^ -lbfd -lintl -liberty -limagehlp
test.exe : test.c
gcc -g -Wall -o $# $^
clean :
-del -f backtrace.dll test.exe
When compiling I get the warning:
backtrace.c:23:17: fatal error: bfd.h: No such file or directory #include < bfd.h>`
Which is weird because that file exists in ../mingw32/include folder.
If I add this when compilind the dll: -IC:\mingw-w64\i686-4.9.2-win32-sjlj-rt_v3-rev1\mingw32\include it continues but stops at the directive: #error config.h must be included before this header and config.h is missing in MinGW-w64
Any ideas?
That path is definetely missing from gcc include paths in mingw. I don't know why. You have to add it yourself in any way you like: cmake recipe, autoconf recipe, CFLAGS, CPATH, gcc specs.
And, as far as I remember, it uses only HAVE_STRINGIZE macro from config.h and it is used only to define CONCAT4 macro, that's not used anywhere in bfd.h. So, it's safe to cheat a little and put
#define PACKAGE package
before including bfd.h
add this to the end of the compile statement:
-I./mingw32/include
so the whole compile statement would be:
gcc -g -Wall -o $# $^ -I./mingw32/include
so the compiler knows where to find the include files

.gcda files not getting generated in C

Can you please let me know that which flag you have build now . That you were able to generate the .gcda files now. I am using this flag for compilation .
CFLAGS_linux= -m32 -fPIC -g -pg -fprofile-arcs -ftest-coverage --coverage
and for linking I am using this
LDFLAGS_linux= -m elf_i386 -Bstatic -L/usr/lib/gcc/x86_64-redhat-linux/4.1.1/32 --whole-archive -lgcov --no-whole-archive -Bdynamic -shared -L/usr/lib -fprofile-arcs -ftest-coverage -lc
I am able to generate .gcno and my process is also coming up. But when I am trying to run the process and performing some operation the .gcda is not getting generated.

librrd link problem

i use rrd (graphic programming ) under rrdtool, i have installed; and i rund gcc
gcc -I/usr/include -I/usr/local/include -L/usr/lib -L/usr/local/lib -lrrd -o myprog test.c
myprog is execute file
and test.c ist testprogram who i use function rrd_create from libary but gcc put out error like this:
/usr/bin/ld: cannot find -lrrd
why!!!!
This error message indicates that the linker cannot locate librrd.a or librrd.so* in /usr/lib/ or /usr/local/lib. You should make sure that you have librrd installed, and not just some other binary rrd package.

Resources