multiple definition error while compiling tftp-hpa - c

I wanna compiling tftp-hpa from source code but i get following error:
gcc tftp.o main.o ../common/libcommon.a -lreadline -ltermcap /home/ali/programming/c/opensource/tftp-hpa/lib/libxtra.a -o tftp
/usr/bin/ld: main.o:/home/ali/programming/c/opensource/tftp-hpa/tftp/main.c:98: multiple definition of `toplevel'; tftp.o:/home/ali/programming/c/opensource/tftp-hpa/tftp/tftp.c:51: first defined here
collect2: error: ld returned 1 exit status
make[1]: *** [Makefile:12: tftp] Error 1
make[1]: Leaving directory '/home/ali/programming/c/opensource/tftp-hpa/tftp'
make: *** [Makefile:7: tftp.build] Error 2
And many warnings.
I tried to compile several source code from several resources like :
Linux.org
here
Debian
here
But every source code throw this compile error. Why ?
All of my commands for compiling tftp :
autoreconf -i
./configure
make
And error ...

I fixed its source code on this repository based on this source code.
In this case we should remove toplevel variable from all .c files on tftp folder.
Write following line on common/tftpsubs.h:
#ifndef _TOP_LEVEL_
#define _TOP_LEVEL_
static sigjmp_buf toplevel;
#endif

Related

Error compiling GCC Ubuntu : undefined reference to

I am trying to run a C project "https://github.com/karimaechihabi/lernaean-hydra/tree/91b480fb633ab4730a2cf1f5573d20d55b39b71e/hydra1/code/ucr-suite", following the same steps to auto generate configuration and makefile as in the read me :
>./autogen.sh \
>./configure \
>make
But when i run make file, i got this error of undefined reference :
>usr/bin/x86_64-linux-gnu-ld: src/bin_serial-serial_scan.o: in function `main':
/home/zeraph/lernaean-hydra/hydra1/code/ucr-suite/src/serial_scan.c:143: undefined reference to `init_serial_stats'\
/usr/bin/x86_64-linux-gnu-ld: /home/zeraph/lernaean-hydra/hydra1/code/ucr-suite/src/serial_scan.c:147: undefined reference to `serial_scan'\
collect2: error: ld returned 1 exit status\
make[1]: *** [Makefile:486: bin/serial] Error 1\
make[1]: Leaving directory '/home/zeraph/lernaean-hydra/hydra1/code/ucr-suite'\
make: *** [Makefile:335: all] Error 2
Any Help Please ?
Ps : While runing the Configuration command to generate makefile, i also got this warning :
>/usr/share/automake-1.16/am/library.am: warning: 'lib/libserial.a': linking libraries using a non-POSIX \
/usr/share/automake-1.16/am/library.am: archiver requires 'AM_PROG_AR' in 'configure.ac'
The problems was with make dependencies(trying to build bin/serial before lib/libserial.a has been built);
Solution : first do make lib/libserial.a first, followed by make to build bin/serial.

make doesn't link math lib when compiling

I get the following error while compiling xpdf using make. I've tried using the command:
LIBS=-lm make
However, it doesn't work. I know the problem is that the c compiler cannot recognise the math symbols in the source code because the math library is not available to it, but I don't know how to fix it.
[ 71%] Linking CXX executable pdftohtml
/usr/bin/ld: CMakeFiles/xpdf_objs.dir/Gfx.cc.o: undefined reference to symbol 'acos##GLIBC_2.2.5'
//usr/lib64/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [xpdf/CMakeFiles/pdftohtml.dir/build.make:219: xpdf/pdftohtml] Error 1
make[1]: *** [CMakeFiles/Makefile2:428: xpdf/CMakeFiles/pdftohtml.dir/all] Error 2
make: *** [Makefile:130: all] Error 2
I have put my Makefile and makeLists.txt on pastebin for more information
I realised I had to use the g++ compiler instead of gcc after reading the man page for gcc which stated:
The usual way to run GCC is to run the executable called gcc, or
machine-gcc when cross-compiling, or machine-gcc-version to run a
specific
version of GCC. When you compile C++ programs, you should invoke GCC as g++ instead.

Using cmake with a shared (dynamic) library

I'm trying to use a simple shared library that I made with a file that just contains a main method.
I first ran cmake . which worked fine and didn't return any errors.
Then I ran make but got this error:
$ make
Scanning dependencies of target myprog
[ 50%] Building C object CMakeFiles/myprog.dir/main.c.o
[100%] Linking C executable myprog.exe
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lhello-user
collect2: error: ld returned 1 exit status
clang-3.8: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/myprog.dir/build.make:95: myprog.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/myprog.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
The CMakeLists.txt file
cmake_minimum_required(VERSION 2.8.8)
project(LIB_EXAMPLE)
set(CMAKE_C_COMPILER clang)
add_executable(myprog main.c)
target_link_libraries(myprog hello-user)
The library exists inside of /usr/local/lib/ as libhello-user.dll.a
Note: Im using Cygwin for cmake and make
Turning my comment into an answer
See CMake/Tutorials/Exporting and Importing Targets.
You either have:
to name a full path for the library
CMake is not searching for it automatically
you would have to add something like find_library(_lib_path NAMES hello-user)
or - better - put those into an IMPORTED target
cmake_minimum_required(VERSION 2.8.8)
project(LIB_EXAMPLE)
add_library(hello-user SHARED IMPORTED GLOBAL)
set_target_properties(
hello-user
PROPERTIES
IMPORTED_LOCATION /usr/local/lib/libhello-user.dll
IMPORTED_IMPLIB /usr/local/lib/libhello-user.dll.a
)
add_executable(myprog main.c)
target_link_libraries(myprog hello-user)

fatal error: 'endian.h' file not found

Trying to compile C program, every time I run make or gmake as recommended I get this error.
$ make
/Applications/Xcode.app/Contents/Developer/usr/bin/make -C src all
gcc -g -W -Wall -O3 -D_FILE_OFFSET_BITS=64 -D_REVISION=0 -Iinclude -c -o osdep/radiotap/radiotap.o osdep/radiotap/radiotap.c
In file included from osdep/radiotap/radiotap.c:17:
osdep/radiotap/platform.h:6:10: fatal error: 'endian.h' file not found
#include <endian.h>
^
1 error generated.
make[1]: *** [osdep/radiotap/radiotap.o] Error 1
make: *** [all] Error 2
$ gmake
gmake -C src all
gmake[1]: Entering directory '/Users/silent/Desktop/aircr-1.2-rc1/src'
gcc -g -W -Wall -O3 -D_FILE_OFFSET_BITS=64 -D_REVISION=0 -Iinclude -c -o osdep/radiotap/radiotap.o osdep/radiotap/radiotap.c
In file included from osdep/radiotap/radiotap.c:17:
osdep/radiotap/platform.h:6:10: fatal error: 'endian.h' file not found
#include <endian.h>
^
1 error generated.
<builtin>: recipe for target 'osdep/radiotap/radiotap.o' failed
gmake[1]: *** [osdep/radiotap/radiotap.o] Error 1
gmake[1]: Leaving directory '/Users/silent/Desktop/aircr-1.2-rc1/src'
Makefile:25: recipe for target 'all' failed
gmake: *** [all] Error 2
According to some forms online recommended to check the file in this location ~/usr/include/machine but doesn't say what to do if found or not! nothing else was helpful. Then, I found this http://www.opensource.apple.com/source/CarbonHeaders/CarbonHeaders-18.1/Endian.h
silent:~/usr/include/machine
$ ls
_limits.h _types.h fasttrap_isa.h profile.h vmparam.h
_mcontext.h byte_order.h limits.h signal.h
_param.h `endian.h` param.h types.h
As you can the file I am getting the error for is already existed! Any help would be appreciated. Thank you.
PS: I am newbie, I don't know what is this link above talking about :(!
On OSX and iOS, you can include endian.h this way:
#include <machine/endian.h>
But note that this will fail on Android or Linux because they expect #include <endian.h>.
You can also include sys/types.h, which will include the right endian.h both on iOS/OSX and Android/Linux:
#include <sys/types.h>
You have to tell the c compiler where it can find this file:
export CFLAGS="$CFLAGS -I~/usr/include/machine"
then run make.
Alternatively you can edit the file Makefile to add the -I~/usr/include/machine part where necessary.

'undefined reference' errors when compiling against library

I added a third party library to my code and is getting errors like this when running make. Please help me understand this error.
(.text+0x9b4): undefined reference to `snd_strerror'
/home/bet/Tent/tun/app/Common/hl/lib/libGHAL.a(gfxhal.o): In function `GFX_create_region':
/home/bet/Tent/tun/app/Common/hl/src/GHAL/gfxhal.c:1141: undefined reference to `my_key_handler'
/home/bet/Tent/tun/app/Common/hal/src/GHAL/gfxhal.c:1141: undefined reference to `create_window'
collect2: ld returned 1 exit status
make[2]: *** [all] Error 1
make[2]: Leaving directory `/home/bet/Tent/tun/app/Common/c_app'
make[1]: *** [ctv_all] Error 2
make[1]: Leaving directory `/home/bet/Tent/tun/app/Common'
Those are Linking errors, which tell you the linker cannot find definitions for the library you are using.
You will have to link the library to your project.
gcc <your files for compilation> -lLibName
Read this for better understanding.
I still get the error even if
readline library is installed and
-lreadline is added to gcc compiling command.
The root cause of my issue is the order of the libraries/code: -lreadline (and other libraries) should be put right before -o options.

Resources