I am in a bit of a pickle. I do not seem to be able to figure out where the problem is: I have a simple test file (test.cpp):
#include <RMF.h>
#include <RMF/FileHandle.h>
#include <iostream>
int main(){
RMF::FileHandle fh;
fh = RMF::create_rmf_file("/home/kkarius/junk/test_rmfs/run_109_frame_6963.rmf3");
std::cout << "yep" << std::endl;
return 0;
}
And I use the following two commands to compile and link my test file.
g++ -Wall -I./include -c test.cpp
g++ -v -g -L./lib64/ -o test test.o -lRMF
The olders and files I link are:
bash-4.2$ ls lib64/
cmake libimp_display.so libimp_isd.so.0 libimp_modeller.so libimp_pepdock.so.0 libimp_scratch.so
libimp_algebra.so libimp_display.so.0 libimp_kernel.so libimp_modeller.so.0 libimp_pmi.so libimp_scratch.so.0
libimp_algebra.so.0 libimp_domino.so libimp_kernel.so.0 libimp_mpi.so libimp_pmi.so.0 libimp_statistics.so
libimp_atom.so libimp_domino.so.0 libimp_kinematics.so libimp_mpi.so.0 libimp_rmf.so libimp_statistics.so.0
libimp_atom.so.0 libimp_em.so libimp_kinematics.so.0 libimp_multi_state.so libimp_rmf.so.0 libimp_symmetry.so
libimp_benchmark.so libimp_em.so.0 libimp_kmeans.so libimp_multi_state.so.0 libimp_rotamer.so libimp_symmetry.so.0
libimp_benchmark.so.0 libimp_example.so libimp_kmeans.so.0 libimp_npc.so libimp_rotamer.so.0 libimp_test.so
libimp_container.so libimp_example.so.0 libimp_misc.so libimp_npc.so.0 libimp_saxs.so libimp_test.so.0
libimp_container.so.0 libimp_foxs.so libimp_misc.so.0 libimp_parallel.so libimp_saxs.so.0 libRMF.so
libimp_core.so libimp_foxs.so.0 libimp_mmcif.so libimp_parallel.so.0 libimp_score_functor.so python2.7
libimp_core.so.0 libimp_isd.so libimp_mmcif.so.0 libimp_pepdock.so libimp_score_functor.so.0
and
bash-4.2$ ls include/
IMP IMP.h RMF RMF.h
While the compile command works out nicely, the linking doesn't:
bash-4.2$ g++ -v -g -L./lib64/ -o test test.o -lRMF
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.3.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure --disable-multilib --enable-languages=c,c++
Thread model: posix
gcc version 7.3.0 (GCC)
COMPILER_PATH=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.3.0/:/usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.3.0/:/usr/local/libexec/gcc/x86_64-pc-linux-gnu/:/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.3.0/:/usr/local/lib/gcc/x86_64-pc-linux-gnu/
LIBRARY_PATH=/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.3.0/:/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-g' '-L./lib64/' '-o' 'test' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.3.0/collect2 -plugin /usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.3.0/liblto_plugin.so -plugin-opt=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.3.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccKz3qw4.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o test /lib/../lib64/crt1.o /lib/../lib64/crti.o /usr/local/lib/gcc/x86_64-pc-linux-gnu/7.3.0/crtbegin.o -L./lib64/ -L/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.3.0 -L/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../.. test.o -lRMF -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/local/lib/gcc/x86_64-pc-linux-gnu/7.3.0/crtend.o /lib/../lib64/crtn.o
test.o: In function `main':
test.cpp:(.text+0x45): undefined reference to `RMF::create_rmf_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: error: ld returned 1 exit status
bash-4.2$ g++ -g -o test test.o lib64/libRMF.so
test.o: In function `main':
test.cpp:(.text+0x45): undefined reference to `RMF::create_rmf_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: error: ld returned 1 exit statusexec/gcc/x86_64-pc-linux-gnu/7.3.0/liblto_plugin.so -plugin-opt=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.3.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccKz3qw4.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o test /lib/../lib64/crt1.o /lib/../lib64/crti.o /usr/local/lib/gcc/x86_64-pc-linux-gnu/7.3.0/crtbegin.o -L./lib64/ -L/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.3.0 -L/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../.. test.o -lRMF -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/local/lib/gcc/x86_64-pc-linux-gnu/7.3.0/crtend.o /lib/../lib64/crtn.o
test.o: In function `main':
test.cpp:(.text+0x45): undefined reference to `RMF::create_rmf_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: error: ld returned 1 exit status
bash-4.2$ g++ -g -o test test.o lib64/libRMF.so
test.o: In function `main':
test.cpp:(.text+0x45): undefined reference to `RMF::create_rmf_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: error: ld returned 1 exit status
So I had a look into the file libRMF.so where I found the method I need:
bash-4.2$ nm lib64/libRMF.so | grep create_rmf_file
00000000009fd6e2 T _ZN3RMF15create_rmf_fileESs
Now I am in the situation that I think I did everything right, yet obviously I didn't! Can someone point out my mistake?
All the best
Kai
Related
friends, I have a question about linking. I have a problem with linking the project.
I have tree of files:
ffmpeg4.1_custom
fftools
ffmpeg.o
libavfilter
libavfilter.so.
make has only one command to finish - this:
$ make -n
printf "LD\t%s\n" ffmpeg_g; gcc -Llibavcodec -Llibavdevice -Llibavfilter -Llibavformat -Llibavresample -Llibavutil -Llibpostproc -Llibswscale -Llibswresample -L/usr/local/lib -Wl,--as-needed -Wl,-z,noexecstack -Wl,--warn-common -Wl,-rpath-link=libpostproc:libswresample:libswscale:libavfilter:libavdevice:libavformat:libavcodec:libavutil:libavresample -o ffmpeg_g fftools/ffmpeg_opt.o fftools/ffmpeg_filter.o fftools/ffmpeg_hw.o fftools/cmdutils.o fftools/ffmpeg.o -lavdevice -lavfilter -lavformat -lavcodec -lavresample -lpostproc -lswresample -lswscale -lavutil -lm -lxcb -lxcb-shape -lxcb-xfixes -lasound -lSDL2 -lsndio -lXv -lX11 -lXext -pthread -lm -lva -lm -lz -lssl -lcrypto -lvpx -lm -lvpx -lm -lvpx -lm -lvpx -lm -pthread -lm -llzma -lz -lfdk-aac -lopenjp2 -lx264 -lva -lm -lm -lm -lm -pthread -lva-drm -lva -lva-x11 -lva -lm -lva -lXv -lX11 -lXext -ldl
printf "STRIP\t%s\n" ffmpeg; strip -o ffmpeg ffmpeg_g
I check my function in ffmpeg.o:
$ nm fftools/ffmpeg.o | grep my_func
U my_func
I check the same in libavfilter.so:
$ nm libavfilter/libavfilter.so | grep my_func
00000000000b2f8e t my_func
I run full command that links these 2 files:
$ gcc -Llibavcodec -Llibavdevice -Llibavfilter -Llibavformat -Llibavresample -Llibavutil -Llibpostproc -Llibswscale -Llibswresample -L/usr/local/lib -Wl,--as-needed -Wl,-z,noexecstack -Wl,--warn-common -Wl,-rpath-link=libpostproc:libswresample:libswscale:libavfilter:libavdevice:libavformat:libavcodec:libavutil:libavresample -o ffmpeg_g fftools/ffmpeg_opt.o fftools/ffmpeg_filter.o fftools/ffmpeg_hw.o fftools/cmdutils.o fftools/ffmpeg.o -lavdevice -lavfilter -lavformat -lavcodec -lavresample -lpostproc -lswresample -lswscale -lavutil -lm -lxcb -lxcb-shape -lxcb-xfixes -lasound -lSDL2 -lsndio -lXv -lX11 -lXext -pthread -lm -lva -lm -lz -lssl -lcrypto -lvpx -lm -lvpx -lm -lvpx -lm -lvpx -lm -pthread -lm -llzma -lz -lfdk-aac -lopenjp2 -lx264 -lva -lm -lm -lm -lm -pthread -lva-drm -lva -lva-x11 -lva -lm -lva -lXv -lX11 -lXext -ldl
fftools/ffmpeg.o: In function `configure_filtergraph':
ffmpeg.c:(.text+0x6892): undefined reference to `my_func'
collect2: error: ld returned 1 exit status
If it plays any role, my function's prototype is in avfilter.h, code is in avfilter.c and it is used in ffmpeg.c
What am I doing wrong?
Linux Mint 19
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)
The 'nm' output indicate my_func is "local" to libavfilter.so - it is marked with 't'. You can only refer to "global" functions - those will be marked with 'T'
From nm man page:
The symbol type. At least the following types are used; others are, as well, depending on the object file
format. If lowercase, the symbol is usually local; if uppercase, the symbol is global (external). There
are however a few lowercase symbols that are shown for special global symbols ("u", "v" and "w").
Access to local symbols is limited to the same module.
This question already has an answer here:
errors when compiling c code with ffmpeg library
(1 answer)
Closed 6 years ago.
After updating my ubuntu OS from 14.04 to 16.04, I installed the ffmpeg library using the following configurations:
PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure \
--prefix="$HOME/ffmpeg_build" \
--pkg-config-flags="--static" \
--extra-cflags="-I$HOME/ffmpeg_build/include" \
--extra-ldflags="-L$HOME/ffmpeg_build/lib" \
--bindir="$HOME/bin" \
--enable-gpl \
--enable-libass \
--enable-libfdk-aac \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libopus \
--enable-libtheora \
--enable-libvorbis \
--enable-libvpx \
--enable-libx264 \
--enable-nonfree
PATH="$HOME/bin:$PATH" make
make install
It seemed to me the installation process was ok. After that, I tried to compile my own C source code with the following Makefile:
EDITTED(adding -lva-drm -lva-x11 at line 10)
FFMPEG_LIBS= libavdevice \
libavformat \
libavfilter \
libavcodec \
libswresample \
libswscale \
libavutil \
TARGET = video_analysis
LIBS = -lva -lX11 -lvdpau -lm -lva-drm -lva-x11
CC = gcc
CFLAGS += -O2 -g -O0
CFLAGS := $(shell pkg-config --cflags $(FFMPEG_LIBS)) $(CFLAGS)
LDLIBS := $(shell pkg-config --libs $(FFMPEG_LIBS)) $(LDLIBS)
.PHONY: default all clean
default: $(TARGET)
all: default
OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c))
HEADERS = $(wildcard *.h)
%.o: %.c $(HEADERS)
$(CC) $(CFLAGS) -c $< -o $#
.PRECIOUS: $(TARGET) $(OBJECTS)
$(TARGET): $(OBJECTS)
$(CC) $(OBJECTS) $(LDLIBS) $(LIBS) -o $#
clean:
-rm -f *.o
-rm -f $(TARGET)
However, my compiler complained the following errors:
/root/ffmpeg_build/lib/libavutil.a(hwcontext_vaapi.o): In function `vaapi_device_create':
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:896: undefined reference to `vaGetDisplay'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:917: undefined reference to `vaGetDisplayDRM'
collect2: error: ld returned 1 exit status
Makefile:30: recipe for target 'video_analysis' failed
make: *** [video_analysis] Error 1
My question is: in which library do 'vaGetDisplay' and `vaGetDisplayDRM' exist? It's for sure that libva functions properly. I have no clue how to fix the bugs...Thank you in advance!
I got the exact same problem when I was trying to compile the latest ffmpeg source code examples. (2016-08-25)
When I have done compiling ffmpeg source, (all dependencies good), then I go to dir:
~/ffmpeg_build/share/ffmpeg/examples$
do:
export PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig"
and as instructed as README:
make
I got the C files compiled to .o files but cannot get linked. Same error output as:
jack#jack-virtual-machine:~/ffmpeg_build/share/ffmpeg/examples$ make
cc avio_reading.o -pthread -L/home/jack/ffmpeg_build/lib -lavdevice -lavfilter -lpostproc -lavformat -lavcodec -lvdpau -lva-drm -lva-x11 -lva -lxcb-shm -lxcb-xfixes -lxcb-render -lxcb-shape -lxcb -lXau -lXdmcp -lSDL -lasound -lpulse-simple -lpulse -lX11 -lXext -lcaca -lx265 -lstdc++ -lrt -lx264 -ldl -lvpx -lpthread -lvorbisenc -lvorbis -ltheoraenc -ltheoradec -logg -lopus -lmp3lame -lfdk-aac -lass -lharfbuzz -lfontconfig -lfribidi -lexpat -lfreetype -lpng12 -llzma -lbz2 -lz -lswresample -lswscale -lavutil -lm -o avio_reading
/home/jack/ffmpeg_build/lib/libavutil.a(hwcontext_vaapi.o): In function `vaapi_device_free':
/home/jack/ffmpeg_source/FFmpeg/libavutil/hwcontext_vaapi.c:861: undefined reference to `XCloseDisplay'
/home/jack/ffmpeg_build/lib/libavutil.a(hwcontext_vaapi.o): In function `vaapi_device_create':
/home/jack/ffmpeg_source/FFmpeg/libavutil/hwcontext_vaapi.c:891: undefined reference to `XOpenDisplay'
/home/jack/ffmpeg_source/FFmpeg/libavutil/hwcontext_vaapi.c:896: undefined reference to `vaGetDisplay'
/home/jack/ffmpeg_source/FFmpeg/libavutil/hwcontext_vaapi.c:903: undefined reference to `XDisplayName'
/home/jack/ffmpeg_source/FFmpeg/libavutil/hwcontext_vaapi.c:917: undefined reference to `vaGetDisplayDRM'
/home/jack/ffmpeg_source/FFmpeg/libavutil/hwcontext_vaapi.c:893: undefined reference to `XDisplayName'
/home/jack/ffmpeg_source/FFmpeg/libavutil/hwcontext_vaapi.c:898: undefined reference to `XDisplayName'
/home/jack/ffmpeg_build/lib/libavutil.a(hwcontext_vdpau.o): In function `vdpau_device_create':
/home/jack/ffmpeg_source/FFmpeg/libavutil/hwcontext_vdpau.c:431: undefined reference to `XOpenDisplay'
/home/jack/ffmpeg_source/FFmpeg/libavutil/hwcontext_vdpau.c:437: undefined reference to `XDisplayString'
/home/jack/ffmpeg_source/FFmpeg/libavutil/hwcontext_vdpau.c:439: undefined reference to `XDefaultScreen'
/home/jack/ffmpeg_source/FFmpeg/libavutil/hwcontext_vdpau.c:439: undefined reference to `vdp_device_create_x11'
/home/jack/ffmpeg_source/FFmpeg/libavutil/hwcontext_vdpau.c:433: undefined reference to `XDisplayName'
/home/jack/ffmpeg_build/lib/libavutil.a(hwcontext_vdpau.o): In function `vdpau_device_free':
/home/jack/ffmpeg_source/FFmpeg/libavutil/hwcontext_vdpau.c:410: undefined reference to `XCloseDisplay'
collect2: error: ld returned 1 exit status
make: *** [avio_reading] Error 1
I checked your solution here and discovered that although ffmpeg team are using
pkg-config tool to config, but in the end it is a gcc command line to compile. (see the seconde line of code snippet above)
All the flags below shall be moved to the end of gcc command line just before the -o option:
-lX11 -lm -lvdpau -lva -lva-drm -lva-x11
So instead of compiling with the next command line:(the command is long, scroll to right to see full)
cc avio_reading.o -pthread -L/home/jack/ffmpeg_build/lib -lavdevice -lavfilter -lpostproc -lavformat -lavcodec -lvdpau -lva-drm -lva-x11 -lva -lxcb-shm -lxcb-xfixes -lxcb-render -lxcb-shape -lxcb -lXau -lXdmcp -lSDL -lasound -lpulse-simple -lpulse -lX11 -lXext -lcaca -lx265 -lstdc++ -lrt -lx264 -ldl -lvpx -lpthread -lvorbisenc -lvorbis -ltheoraenc -ltheoradec -logg -lopus -lmp3lame -lfdk-aac -lass -lharfbuzz -lfontconfig -lfribidi -lexpat -lfreetype -lpng12 -llzma -lbz2 -lz -lswresample -lswscale -lavutil -lm -o avio_reading
I move the flags to the right side end and compile with:(the command is long, scroll to right to see full)
cc avio_dir_cmd.o -pthread -L/home/jack/ffmpeg_build/lib -lavdevice -lavfilter -lpostproc -lavformat -lavcodec -lxcb-shm -lxcb-xfixes -lxcb-render -lxcb-shape -lxcb -lXau -lXdmcp -lSDL -lasound -lpulse-simple -lpulse -lXext -lcaca -lx265 -lstdc++ -lrt -lx264 -ldl -lvpx -lpthread -lvorbisenc -lvorbis -ltheoraenc -ltheoradec -logg -lopus -lmp3lame -lfdk-aac -lass -lharfbuzz -lfontconfig -lfribidi -lexpat -lfreetype -lpng12 -llzma -lbz2 -lz -lswresample -lswscale -lavutil -lX11 -lm -lvdpau -lva -lva-drm -lva-x11 -o avio_dir_cmd
And all the compile errors are gone~!
I think this is a common error when we compile other examples, or our own projects.
after adding -lva-drm and -lva-x11 into Makefile, my compiler complains no error any more :-)
I write a program including two files main.c and comp.c
main.c
#include <stdio.h>
extern int secure_func(int, int);
void main()
{
printf("hello, world\n");
int result = secure_func(1, 1);
}
comp.c
int secure_func(int text, int key)
{
return text * key * key;
}
First, I directly use gcc to build program and run executable, that is fine.
gcc -o main main.c comp.c
./main
hello, world
Then I try to use toolchain to build program step by step like this
gcc -c -o comp.o comp.c
gcc -c -o main.o main.c
ld -o main main.o comp.o -lc --entry main
ld generate a file main. But if I try to run it, it show error
bash: ./main: No such file or directory
The executable has X permission.
List Info
> ~/test/segtest2$ ls -l
> total 24
> -rw-rw-r-- 1 kail kail 88 Sep 28 21:20 comp.c
> -rw-rw-r-- 1 kail kail 37 Sep 28 21:20 comp.h
> -rw-rw-r-- 1 kail kail 1248 Sep 28 21:22 comp.o
> -rwxrwxr-x 1 kail kail 3241 Sep 28 21:22 main
> -rwxrwxr-- 1 kail kail 137 Sep 28 21:20 main.c
> -rw-rw-r-- 1 kail kail 1568 Sep 28 21:21 main.o
Do I lost anything? Any suggestion will be appreciated. Thanks!
The argument --entry=main you pass to ld is not doing what you think it does. The entry point of a program is where the program starts. This is not main though, it's a function called _start defined in an object file crt0.o found somewhere on your system. To manually link a C program, invoke ld like this:
ld -o main main.o comp.o /path/to/crt0.o -lc
Do not supply --entry unless you know what it does.
Let's see how gcc works in verbose mode.
gcc -v -o main main.o comp.o
gcc links objects using following command
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/collect2 --build-id --eh-frame-hdr
-m elf_x86_64 --hash-style=both -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o main -z relro
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crti.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/crtbegin.o
-L/usr/lib/gcc/x86_64-linux-gnu/4.4.3 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../.. main.o comp.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc
--as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.4.3/crtend.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crtn.o
If you replace /usr/lib/gcc/x86_64-linux-gnu/4.4.3/collect2 with ld then the linking process completes successfully.
ld --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=both
-dynamic-linker /lib64/ld-linux-x86-64.so.2 -o main -z relro /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crti.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/crtbegin.o
-L/usr/lib/gcc/x86_64-linux-gnu/4.4.3 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../.. main.o comp.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc
--as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.4.3/crtend.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crtn.o
Refer this answer for further information
I see something similar when I try to run:
-bash: ./main: /lib/ld64.so.1: bad ELF interpreter: No such file or directory
So you're probably not calling ld correctly. Rather than using ld, use gcc instead to invoke it, as it takes care of details you'd otherwise have to worry about with ld.
gcc -c -o comp.o comp.c
gcc -c -o main.o main.c
gcc -o main main.o comp.o
I am trying to learn the gobject system, so I read some of the documentation on the gnome site and made a simple gobject parented to GObject. I don't wan't to keep running the gcc job every time I want to compile, and I also wanted to learn a build system at the same time. I tried autotools, which I got working to the point where pkg-config searches for gobject-2.0.
To compile my project from the command line, I do: gcc *.c $(pkg-config --cflags --libs gobject-2.0) (I had to take out the extra ticks for formatting). Anyway <--- this command works.
However: gcc $(pkg-config --cflags --libs gobject-2.0) *.c, which SHOULD BE the SAME COMMAND, returns this:
main.c: In function 'main':
main.c:10:10: warning: initialization from incompatible pointer type [enabled by default]
/tmp/ccxO7wkX.o: In function `main':
main.c:(.text+0x1a): undefined reference to `g_type_init'
main.c:(.text+0x27): undefined reference to `g_type_create_instance'
/tmp/ccPu2beU.o: In function `a_get_type':
myobject.c:(.text+0x57): undefined reference to `g_type_register_static'
collect2: ld returned 1 exit status
Yes, it is the EXACT SAME COMMAND WITH ANOTHER ORDER. I have no clue what's going wrong. Here is how autotools/automake does the build:
gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 -o GObjectTest main.o myobject.o
main.o: In function `main':
/home/aj/Desktop/GObjectTest/src/main.c:9: undefined reference to `g_type_init'
/home/aj/Desktop/GObjectTest/src/main.c:10: undefined reference to `g_type_create_instance'
myobject.o: In function `a_get_type':
/home/aj/Desktop/GObjectTest/src/myobject.c:19: undefined reference to `g_type_register_static'
collect2: ld returned 1 exit status
make: *** [GObjectTest] Error 1
Is this some quirky gcc compile thing? I tried with clang too no avail, which makes me think that this is a linker problem? I really have no idea, and was hoping that someone out there will. This is pretty frustrating.
Here is the "offending code":
GType a_get_type (void)
{
if (type == 0) {
GTypeInfo info = {
sizeof(AClass),
NULL,
NULL,
(GClassInitFunc) a_class_init,
NULL,
NULL,
sizeof(A),
0,
(GInstanceInitFunc) NULL
};
type = g_type_register_static (G_TYPE_OBJECT,
"AType",
&info, 0);
}
return type;
}
Edit: Here is the output of my autotools build, the make part anyway:
$ make
make all-recursive
make[1]: Entering directory `/home/aj/Desktop/GObjectTest'
Making all in src
make[2]: Entering directory `/home/aj/Desktop/GObjectTest/src'
gcc -DHAVE_CONFIG_H -I. -I.. -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
main.c: In function 'main':
main.c:10:10: warning: initialization from incompatible pointer type [enabled by default]
mv -f .deps/main.Tpo .deps/main.Po
gcc -DHAVE_CONFIG_H -I. -I.. -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -MT myobject.o -MD -MP -MF .deps/myobject.Tpo -c -o myobject.o myobject.c
mv -f .deps/myobject.Tpo .deps/myobject.Po
gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 -o GObjectTest main.o myobject.o
main.o: In function `main':
/home/aj/Desktop/GObjectTest/src/main.c:9: undefined reference to `g_type_init'
/home/aj/Desktop/GObjectTest/src/main.c:10: undefined reference to `g_type_create_instance'
myobject.o: In function `a_get_type':
/home/aj/Desktop/GObjectTest/src/myobject.c:19: undefined reference to `g_type_register_static'
collect2: ld returned 1 exit status
make[2]: *** [GObjectTest] Error 1
make[2]: Leaving directory `/home/aj/Desktop/GObjectTest/src'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/aj/Desktop/GObjectTest'
make: *** [all] Error 2
Here is the interesting part: replace gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 -o GObjectTest main.o myobject.o with gcc -o GObjectTest main.o myobject.o -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 and it works.
#include<stdio.h>
#include "flite.h"
cst_voice *register_cmu_us_kal();
int main()
{
cst_voice *v;
cst_wave *w;
char *text = "Hello world programming";
//Initialising the flite variables used
flite_init();
w = new_wave();
v = register_cmu_us_kal(NULL);
flite_text_to_speech(text,v,"hello_wave");
if(cst_wave_load_riff(w,"hello_wave")!=CST_OK_FORMAT){
printf("\nCompare_wave:Can read file or wrong format!\n");
}
else{
play_wave(w);
}
return 0;
}
Makefile
all:compile \
./compile
compile:eg1.o
gcc -o $# eg1.o
eg1.o:eg1.c $(LIBS_DIR) $(INC_DIR) $(LIBS)
gcc -c $<
LIBS_DIR = -L /home/b/flite-1.4-release/build/i386-linux-gnu/lib
INC_DIR = -I /home/b/flite-1.4-relase/include
LIBS = -lflite_cmu_us_slt -lflite -lflite_cmulex -lflite_cmu_time_awb -lflite_cmu_us_kal16 -lflite_cmu_us_kal -lflite_cmu_usenglish
INCLUDE:
clean:
rm -f *.o
I tried by giving he library and header file paths as LIBS_DIR = ../build/i386-linux-gnu/lib and INC_DIR = ../include
I tried the folowing c program by including a third party library. This program an a makefile is located in b\flite-1.4-release\Learnin_though_example folder. Th flite libraries are located in b\flite-1.4-release\build\i386-linux-gnu\lib and the header files are in b\flite-1.4-release\include .
I assume that i have given the makefile th correct path to search for the files. But its not identifyin it and i'm gettin an error as,
make clean all
rm -f *.o
gcc -c eg1.c
eg1.c:2:19: error: flite.h: No such file or directory
eg1.c:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
eg1.c: In function ‘main’:
eg1.c:6: error: ‘cst_voice’ undeclared (first use in this function)
eg1.c:6: error: (Each undeclared identifier is reported only once
eg1.c:6: error: for each function it appears in.)
eg1.c:6: error: ‘v’ undeclared (first use in this function)
eg1.c:7: error: ‘cst_wave’ undeclared (first use in this function)
eg1.c:7: error: ‘w’ undeclared (first use in this function)
eg1.c:17: error: ‘CST_OK_FORMAT’ undeclared (first use in this function)
make: *** [eg1.o] Error 1
Please help me understand what is the mistake i'm doing
EDITED:
I modiied th makefile as per matt's guidance:
all:compile
compile:eg1.o
gcc $(INC_DIR) $(LIB_DIR) -o $# $^ $(LIBS)
eg1.o:eg1.c
gcc $(INC_DIR) -o $# -c $^
LIBS_DIR = -L../build/i386-linux-gnu/lib
INC_DIR = -I../include
LIBS = -lflite -lflite_cmulex -lflite_cmu_time_awb -lflite_cmu_us_kal16 -lflite_cmu_us_kal -lflite_cmu_usenglish -lflite_cmu_us_slt
clean:
rm -f *.o
but i'm getting ifferent error whn compiled with the command "make clean all" as,
rm -f *.o
gcc -I../include -o eg1.o -c eg1.c
gcc -I../include -o compile eg1.o -lflite -lflite_cmulex -lflite_cmu_time_awb -lflite_cmu_us_kal16 -lflite_cmu_us_kal -lflite_cmu_usenglish -lflite_cmu_us_slt
/usr/bin/ld: cannot find -lflite
collect2: ld returned 1 exit status
make: *** [compile] Error 1
EDITED:
rm -f *.o
gcc -I../include -o eg1.o -c eg1.c
gcc -I../include -L../build/i386-linux-gnu/lib -o compile eg1.o -lflite -lflite_cmulex -lflite_cmu_time_awb -lflite_cmu_us_kal16 -lflite_cmu_us_kal -lflite_usenglish -lflite_cmu_us_slt -lflite_cmu_us_rms
../build/i386-linux-gnu/lib/libflite.so: undefined reference to `sin'
../build/i386-linux-gnu/lib/libflite.so: undefined reference to `exp'
../build/i386-linux-gnu/lib/libflite.so: undefined reference to `sqrt'
../build/i386-linux-gnu/lib/libflite.so: undefined reference to `log'
../build/i386-linux-gnu/lib/libflite.so: undefined reference to `fmod'
../build/i386-linux-gnu/lib/libflite.so: undefined reference to `pow'
Your makefile is, I'm afraid to say, completely broken.
The basic Makefile syntax is:
target: pre-requisite(s)
<tab>Stuff to do to build target from pre-reqs (if required)
So this is wrong, eg1.o can't be a pre-requisite for building itself.
compile:eg1.o
gcc -o eg1.o
You should have:
eg1.o: eg1.c
gcc $(INC_DIR) -o $# -c $^
($# is the target, $^ all the pre-reqs.)
Then you can:
myexe: eg1.o
gcc $(INC_DIR) $(LIBS_DIR) -o $# $^ $(LIBS)
This will produce myexe from eg1.o. And your all rule should be all: myexe, with no recipe (no commands), and at the top as you have it.
Then you've got your include directories and library directories mixed up. -I is for include paths, -L for library paths.
Place your variable definitions before the rules, that's more common/usual. And don't put a space between -L/-I and the path that follows it.
The include directories to search is specified by -I flag, not -L.
Change:
LIBS_DIR = -I /home/b/flite-1.4-release/build/i386-linux-gnu/lib
INC_DIR = -L /home/b/flite-1.4-relase/include
to:
LIBS_DIR = -L /home/b/flite-1.4-release/build/i386-linux-gnu/lib
INC_DIR = -I /home/b/flite-1.4-relase/include