I get linker error while compiling a minimal program that uses getaddrinfo_a on Linux. The program in question
#define _GNU_SOURCE
#include <stdio.h>
#include <netdb.h>
int main(int argc, char **argv) {
int err;
err = getaddrinfo_a(0, NULL, 0, NULL);
}
Compiler output:
$ cc -lanl minimal.c
/tmp/cc89BuFU.o: In function `main':
minimal.c:(.text+0x24): undefined reference to `getaddrinfo_a'
collect2: error: ld returned 1 exit status
$ cc --version
cc (Ubuntu 4.9.2-10ubuntu13) 4.9.2
You are using command in wrong way. Use
cc minimal.c -lanl
-lanl should come after not before file name.
gcc [options] [source files] [object files] [-Ldir] -llibname [-o outfile]
https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
-l 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.
According to standard also, order of library matters. Linker didn't check for symbol from previously specified libraries. Ref
The library to link must be put after the object (and source files in your case):
$ cc minimal.c -lanl
Related
I'm trying to write my own shared library to link to an executable, but can't get the .so to link.
I'm using a very basic example to try and get it working. The shared library (test_lib.c):
#include "test_lib.h" //stdlib includes and function prototype
char *hello(void) {
char *c = malloc(100);
memcpy(c, "hello\n", 7);
return c;
}
The executable (test.c):
#include "test_lib.h"
int main() {
printf("%s", hello());
return 0;
}
Following all the guides I can find, I compile the .so with gcc -I . -fPIC -shared -o test_lib.so test_lib.c, and then the executable (in the same directory) with gcc -I . -L . test.c -ltest_lib
This gives the error:
/usr/bin/ld: cannot find -ltest_lib
collect2: error: ld returned 1 exit status
As I understand including the path through the -L flag should tell gcc where to find the .so, but this isn't working. What am I missing here?
When linking a library, the library usually has to be named libxxx.a|so for the linker to find it.
Compiling the library:
gcc -I . -fPIC -shared -o libtest.so test_lib.c
Then you can link with:
gcc -I . -L . test.c -ltest
How to change the entry point of a C program compiled with gcc ?
Just like in the following code
#include<stdio.h>
int entry() //entry is the entry point instead of main
{
return 0;
}
It's a linker setting:
-Wl,-eentry
the -Wl,... thing passes arguments to the linker, and the linker takes a -e argument to set the entry function
You can modify your source code as:
#include<stdio.h>
const char my_interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
int entry() //entry is the entry point instead of main
{
exit(0);
}
The ".interp" section will let your program able to call external shared library.
The exit call will make your entry function to exit program instead of return.
Then build the program as a shared library which is executable:
$ gcc -shared -fPIC -e entry test_main.c -o test_main.so
$ ./test_main
If you are on a system that provides GNU Binutils (like Linux),
you can use the objcopy command
to make an arbitrary function the new entry point.
Suppose a file called program.c containing the entry function:
$ cat > program.c
#include <stdio.h>
int entry()
{
return 0;
}
^D
You first compile it using -c to generate a relocatable object file:
$ gcc -c program.c -o program.o
Then you redefine entry to be main:
$ objcopy --redefine-sym entry=main program.o
Now use gcc to compile the new object file:
$ gcc program.o -o program
NOTE: If your program already has a function called main, before step 2, you can perform a separate objcopy invocation:
objcopy --redefine-sym oldmain=main program.o
Minimal runnable example and notes on other answers
main.c
#include <stdio.h>
#include <stdlib.h>
int mymain(void) {
puts("hello");
exit(0);
}
compile and run:
gcc -nostartfiles -Wl,--entry=mymain -o main.out main.c
# or -Wl,-emymain
./main.out 1 2 3
The notes:
without -nostartfiles, the link fails with:
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
presumably because the glibc setup code that runs before main in _start normally calls main.
command line arguments are not setup for you, presumably because they would be setup by the glibc code that runs before main, so trying to use them prints undefined values. I haven't found a method that works for them.
Tested in Ubuntu 20.10.
How to change the entry point of a C program compiled with gcc ?
Just like in the following code
#include<stdio.h>
int entry() //entry is the entry point instead of main
{
return 0;
}
It's a linker setting:
-Wl,-eentry
the -Wl,... thing passes arguments to the linker, and the linker takes a -e argument to set the entry function
You can modify your source code as:
#include<stdio.h>
const char my_interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
int entry() //entry is the entry point instead of main
{
exit(0);
}
The ".interp" section will let your program able to call external shared library.
The exit call will make your entry function to exit program instead of return.
Then build the program as a shared library which is executable:
$ gcc -shared -fPIC -e entry test_main.c -o test_main.so
$ ./test_main
If you are on a system that provides GNU Binutils (like Linux),
you can use the objcopy command
to make an arbitrary function the new entry point.
Suppose a file called program.c containing the entry function:
$ cat > program.c
#include <stdio.h>
int entry()
{
return 0;
}
^D
You first compile it using -c to generate a relocatable object file:
$ gcc -c program.c -o program.o
Then you redefine entry to be main:
$ objcopy --redefine-sym entry=main program.o
Now use gcc to compile the new object file:
$ gcc program.o -o program
NOTE: If your program already has a function called main, before step 2, you can perform a separate objcopy invocation:
objcopy --redefine-sym oldmain=main program.o
Minimal runnable example and notes on other answers
main.c
#include <stdio.h>
#include <stdlib.h>
int mymain(void) {
puts("hello");
exit(0);
}
compile and run:
gcc -nostartfiles -Wl,--entry=mymain -o main.out main.c
# or -Wl,-emymain
./main.out 1 2 3
The notes:
without -nostartfiles, the link fails with:
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
presumably because the glibc setup code that runs before main in _start normally calls main.
command line arguments are not setup for you, presumably because they would be setup by the glibc code that runs before main, so trying to use them prints undefined values. I haven't found a method that works for them.
Tested in Ubuntu 20.10.
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
when i compile and link this code to get disk uuid:
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <blkid/blkid.h>
int main (int argc, char *argv[]) {
blkid_probe pr;
const char *uuid;
if (argc != 2) {
fprintf(stderr, "Usage: %s devname\n", argv[0]);
exit(1);
}
pr = blkid_new_probe_from_filename(argv[1]);
if (!pr) {
err(2, "Failed to open %s", argv[1]);
}
blkid_do_probe(pr);
blkid_probe_lookup_value(pr, "UUID", &uuid, NULL);
printf("UUID=%s\n", uuid);
blkid_free_probe(pr);
return 0;
}
it errors out:
/home/usr/blkid/blkid.c:15: undefined reference to `blkid_new_probe_from_filename'
make[2]: Leaving directory `/home/usr/blkid'
make[1]: Leaving directory `/home/usr/blkid'
/home/usr/blkid/blkid.c:20: undefined reference to `blkid_do_probe'
/home/usr/blkid/blkid.c:21: undefined reference to `blkid_probe_lookup_value'
/home/usr/blkid/blkid.c:25: undefined reference to `blkid_free_probe'
when i compile the code by the following command, the code compiles with no error
gcc -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/blkid.o.d -o build/Debug/GNU-Linux-x86/blkid.o blkid.c
Try to put -lblkid into your gcc command so the linker will know that you need to link your code to that library. Be sure to put this option at the end of the command. The order of options somehow matters. From here:
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.
This command should automatically both compile and link your source code:
gcc -o test -g -MMD -MP -MF build/Debug/GNU-Linux-x86/blkid.o.d blkid.c -lblkid
The error you show comes from the linker.
If you compile one single file to a .o file without linking, no external references will be tried to fulfill.
But if you want to compile into an executable, all needed requirements must be fulfilled. If the program requires the presence of a blkid_do_probe(), you should provide it somehow. Probably this will be done by linking with the appropriate library. As someone mentionned in a comment, this is to be done with -lblkid.