I'm trying to link to WordNet library. I downloaded the header file (wn.h), libWN.a file and a bunch of .o files (libWN_a-search.o, etc) from WordNet. I put a main.c, wn.h and libWN.a files in a folder and implemented main.c:
#include <stdio.h>
#include "wn.h"
int main(int argc, char *argv[]) {
char rc, *s;
s = "cat";
rc = in_wn(s, ALL_POS);
printf("rc: %d\n", rc);
return 0;
}
I try to compile using cc main.c -L. but I get the linker error:
/usr/bin/ld: /tmp/ccvzqY4x.o: in function `main':
main.c:(.text+0x27): undefined reference to `in_wn'
collect2: error: ld returned 1 exit status
Documentation on WordNet lib can be found here. I'm just not sure what I'm doing wrong.
Including the wn.h header file is not enough. The .h file only contains the declarations, but the actual implemenation is contained in the library file libWN.a which you failed to link with.
For linking with that library you need to tell the compiler to do so by adding -lWN:
cc main.c -L. -lWN
Related
I am starting out with coding for Wayland. I have been following many tutorials, but I am still stuck at compiling. I wrote this very simple code:
#include<stdio.h>
#include<wayland-client-core.h>
int main(int argc, char *argv[]) {
struct wl_display *display = wl_display_connect(NULL);
if(!display) {
fprintf(stderr, "Failed to connect to Wayland display\n");
return 1;
}
fprintf(stdout, "Connection established!\n");
getchar();
wl_display_disconnect(display);
return 0;
}
Then, I tried to compile it with gcc -o client -lwayland-client client.c
But, compilation fails with this error message:
/usr/bin/ld: /tmp/ccfXeOS8.o: in function `main':
client.c:(.text+0x19): undefined reference to `wl_display_connect'
/usr/bin/ld: client.c:(.text+0x7c): undefined reference to `wl_display_disconnect'
collect2: error: ld returned 1 exit status
This looks strange because there is a file /usr/include/wayland-client-core.h in the system, which has got these declarations:
struct wl_display *wl_display_connect(const char *name);
void wl_display_disconnect(struct wl_display *display);
What am I doing wrong here?
You need to change the command line to:
gcc client.c -o client -lwayland-client
or:
gcc client.c -lwayland-client -o client
or:
gcc -o client client.c -lwayland-client
Basically, the .c file (or .o file) needs to appear before the library linking options.
I'm trying to compile a C program linking two previously created object files but keep getting an 'undefined reference' error.
I'm using Visual Code to write the code and Ubuntu on Windows to compile using a makefile. The two C files, task5.c and reverse.c which have been made into object files both contain #include reverse.h statements which contains the prototypes for the functions in reverse.c.
task5.c
#include <stdio.h>
#include "ctap.h"
#include "reverse.h"
TESTS {
const char *a = "Family";
char *b = reverse(a);
//test 1
ok(string_length(a) == string_length(b), "Strings are the same size");
//test 2
is("ylimaF", b, "Strings match");
}
reverse.c
#include <stdio.h>
#include <stdlib.h>
#include "reverse.h"
char *reverse(const char *str) {
//code
}
int string_length(const char *str) {
//code
}
reverse.h
char *reverse(const char *str);
int string_length(const char *str);
makefile
linked:
gcc -o linked task5.o reverse.o
task5.o
gcc -c -o task5.o task5.c
reverse.o
gcc -c -o reverse.o reverse.c
When I run the command make linked I expect it to be made and return nothing.
But when I run that command I get this error:
cc task5.o -o linked
task5.o: In function `ctap_tests':
task5.c:(.text+0x1abe): undefined reference to `reverse'
task5.c:(.text+0x1ace): undefined reference to `string_length'
task5.c:(.text+0x1adc): undefined reference to `string_length'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'linked' failed
make: *** [task5] Error 1
According to this GNU make documentation, the GNU make program will try to use GNUMakefile, makefile or Makefile.
The make program will not try makefile.mk, which means that e.g. make linked will use no makefile and only the default rules.
You can solve this by either renaming your makefile as Makefile (the most common) or makefile; Or by using the -f option to specify the makefile
$ make -f makefile.mk linked
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
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.
I'm trying to learn how to use the C API to reading Matlab .mat files, but it's not working as I expected:
I'd like to just open a very simple .mat file called test.mat, read a value from the file and store it in a C variable. I've created test.mat in Matlab using the following commands:
> value = 3;
> save ("test.mat", "value")
Below is my C code, which doesn't even compile - the compiler doesn't seem to find the header files. See below the code for compiler output. What am I doing wrong here?
Code:
#include <stdlib.h>
#include <stdio.h>
#include <mat.h>
#include <matrix.h>
int main(int argc, char *argv[]) {
double value;
MATFile *datafile;
datafile = matOpen("test.mat", "r");
mxArray *mxValue;
mxValue = matGetVariable(datafile, "value");
matClose(datafile);
value = *mxGetPr(mxArray);
mxFree(mxArray);
printf("The value fetched from the .mat file was: %f", value);
return 0;
}
Compiler output:
$ make animate_shot
cc -I/usr/local/MATLAB/R2011a/extern/include/ animate_shot.c -o animate_shot
/tmp/cczrh1vT.o: In function `main':
animate_shot.c:(.text+0x1a): undefined reference to `matOpen'
animate_shot.c:(.text+0x2f): undefined reference to `matGetVariable'
animate_shot.c:(.text+0x3f): undefined reference to `matClose'
animate_shot.c:(.text+0x4b): undefined reference to `mxGetPr'
animate_shot.c:(.text+0x5e): undefined reference to `mxFree'
collect2: ld returned 1 exit status
make: *** [animate_shot] Error 1
(the -I flag is specified with the line CPPFLAGS=-I/usr/local/MATLAB/R2011a/extern/include/ in my makefile, and I've verified that the directory exists and contains the header files mat.h and matrix.h).
UPDATE:
I've found that the libraries I need to link in are libmat.so and libmx.so (according to this MathWorks help article), residing in /usr/local/MATLAB/R2011a/bin/glnxa64/ on my system. I've therefore updated my makefile to this:
CPPFLAGS =-I/usr/local/MATLAB/R2011a/extern/include/
LDFLAGS = -L/usr/local/MATLAB/R2011a/bin/glnxa64 -l mat -l mx
Now, running make gives the following command:
cc -I/usr/local/MATLAB/R2011a/extern/include/ -L/usr/local/MATLAB/R2011a/bin/glnxa64 -l mat -l mx animate_shot.c -o animate_shot
However, I still get the same errors. Any ideas?
This is a linker failure, not a compiler failure (and is unrelated to -I compiler option). You need to specify the directory in which the matlab .so files are located using -L flag and add a -l<matlab-lib-name> option to end of the compiler command that specifies the name of the matlab library.
For example:
cc -I/usr/local/MATLAB/R2011a/extern/include/ -L/usr/local/MATLAB/R2011a/lib animate_shot.c -o animate_shot -lmatlab
(I don't know the exact directory into the which .so are located or the name of the matlab library)
Based on the comment providing further information:
cc -I/usr/local/MATLAB/R2011a/extern/include/ -L/usr/local/MATLAB/R2011a/bin/glnxa64 animate_shot.c -o animate_shot -lmat -lmx