Libelf : Undefined Reference for functions - c

I am trying to use Libelf library to get some info on some elf files. But I keep getting these "undefined reference to [...]". I installed the libelf from the synaptic (tried to get from website too), and the lib seems to be instaled okay.
My code :
#include <err.h>
#include <fcntl.h>
#include <sysexits.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <libelf.h>
int main(int argc, char * argv[]) {
Elf *elf_file;
Elf_Kind elf_kind_file;
int file_descriptor;
if((argc!=2))
printf("Argumento em formato nao esperado\n");
file_descriptor = open(argv[1], O_RDONLY, 0);
elf_file = elf_begin(file_descriptor, ELF_C_READ, NULL);
elf_kind_file = elf_kind(elf_file);
elf_end(elf_file);
close(file_descriptor);
return 0;
}
Here is what I get from the terminal (Currently using Ubuntu 11.4):
gcc sample.c -o sample
/tmp/ccP7v2DT.o: In function `main':
sample.c:(.text+0x57): undefined reference to `elf_begin'
sample.c:(.text+0x67): undefined reference to `elf_kind'
sample.c:(.text+0x77): undefined reference to `elf_end'
collect2: ld returned 1 exit status
UPDATE
Solved all but one of my problems putting -lelf before my file during the compiling. The last one I could only solve updating my libelf to a new version (that wasn't available on the Synaptic Manager):
wget http://ftp.br.debian.org/debian/pool/main/e/elfutils/libelf-dev_0.153-1_i386.deb
wget http://ftp.br.debian.org/debian/pool/main/e/elfutils/libelf1_0.153-1_i386.deb
sudo dpkg -i libelf-dev_0.153-1_i386.deb libelf1_0.153-1_i386.deb

You probably need to link to the library. The standard way to do this is:
gcc sample.c -l elf -o sample
This searches the standard library directories for libelf.a, and includes the relevant object files in that archive into the build. Assuming the library has been installed correctly, libelf.a or a similarly-named file should exist.

Related

GCC extern libraries

Informations:
OS: Windows 10
Compiler: MinGW gcc
Language: C
SDL & OpenGL are already installed
When i try to compile the test file , i receive this error:
gcc test.c -o test
teste.c:1:10: fatal error: SDL2: No such file or directory
1 | #include <SDL2>
| ^~~~~~
compilation terminated.
The test file content is:
#include <SDL2>
#include <stdio.h>
void main()
{
printf("Testing");
}
This is my problem, help me pls.
In order to use the SDL header you need to use #include <SDL2/SDL.h> instead of #include <SDL2>.

netfilter queue undefined reference to `nfq_open'

I have writting this code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <linux/types.h>
#include <linux/netfilter.h>
#include <libnetfilter_queue/libnetfilter_queue.h>
int main(int argc, char **argv)
{
struct nfq_handle *h;
printf("opening library handle\n");
h = nfq_open();
nfq_close(h);
exit(0);
}
and when I try to compile it says that:
/tmp/ccEv9MYS.o: In function `main':
test1.c:(.text+0x1a): undefined reference to `nfq_open'
test1.c:(.text+0x2a): undefined reference to `nfq_close'
collect2: ld returned 1 exit status
I tried checking if the library is found by gcc and it is (when I modifiy the incluse of libnetfilter_queue there is an error), I recompiled the library and made sur that the fonctions I'm calling are in in it.
If you have any clue thanks for helping
Icompile using this:
gcc -o test test1.c
I have also tried:
gcc -o test -lnetfilter_queue test1.c
gcc -o test -L/usr/local/lib test1.c
Well, from the gcc manual page, for the -llibrary linking option
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.
That says, the linker works from left to right, so need to put the dependent on left hand side.
You need to change your compilation statement to
gcc -o test test1.c -lnetfilter_queue

How do you successfully compile a C program using the GNU Readline library?

I followed the instructions to install GNU Readline, as well as Curses, however I get some linker issues that I am unsure how to resolve. The following is my program:
main.c
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <term.h>
#include <readline/readline.h>
#include <readline/history.h>
int main()
{
char * line = readline ("Enter a line: ");
free (line);
return 0;
}
I compiled using: gcc -o main {,.c} -lreadline -lncurses (and the readline includes were where they were supposed to be, in usr/includes...
Running main gave me:
./main: symbol lookup error: /usr/local/lib/libreadline.so.6: undefined symbol: UP
Any direction as to go about resolving this would be greatly appreciated.
sudo apt-get install libreadline6-dev
gcc -o main {,.c} -lreadline -lncurses

ERROR: ld.so: object 'getpid.so' from LD_PRELOAD cannot be preloaded: ignored

When I try to use LD_PRELOAD as following,
LD_PRELOAD=getpid.so ./testpid
I get the following error...
ERROR: ld.so: object 'getpid.so' from LD_PRELOAD cannot be preloaded: ignored.
I compile getpid.so by using
gcc -Wall -fPIC -shared -o getpid.so getpid.c
and it contains the following code...
// getpid.c
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
pid_t getpid(void)
{
printf("Hello, world!\n");
return syscall(SYS_getpid);
}
tespid.c constains code which uses getpid as shown below and which is compiled by doing
gcc testpid -o testpid.c
What can be the problem here? Why is LD_PRELOAD not working?
// testpid.c
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
printf( "pid = %d!\n", getpid() );
return 0;
}
Looks like the loader is unable to find getpid.so as you've not mentioned the path to the library.
Try:
LD_PRELOAD=/full/path/to/getpid.so ./testpid
I was also facing error as below
ERROR: ld.so: object '/usr/lib64/libjemalloc.so.1' from LD_PRELOAD cannot be preloaded
Below were my steps resolved the error for me.
Go to the path /usr/lib64 and look for the libjemalloc using below commands*
#cd /usr/lib64
#ls | grep libjemalloc
if you don't find you don't have that package installed in your system
$sudo yum whatprovides libjemalloc*
$sudo yum install jemalloc-3.6.0-1.amzn2.x86_64

Calling a Lua script from C: errors

I'm new at Lua and writing bindings in general. At the moment I'm just trying to compile the first example found here (with the functions updated to Lua 5.1).
#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
/* the Lua interpreter */
lua_State* L;
int main ( int argc, char *argv[] )
{
/* initialize Lua */
L = luaL_newstate();
/* load various Lua libraries */
luaL_openlibs(L);
luaopen_table(L);
luaopen_io(L);
luaopen_string(L);
luaopen_math(L);
/* cleanup Lua */
lua_close(L);
return 0;
}
When I compile using gcc -o init init.c -Wall -I/usr/local/include -L/usr/local/lib -llua -lliblua I get the following error:
.../../i486-pc-linux-gnu/bin/ld: cannot find -lliblua
collect2: ld returned 1 exit status
The file liblua.a is in /usr/local/lib, but for some reason the compiler can't find it? What am I doing wrong?
The liblua.a file is included by the -llua parameter. Specifying -lliblua attempts to find a libliblua.a file that does not exist. So, simply remove -lliblua from your build command.
There's no -lliblua in 5.1.

Resources