RobertoP
RobertoP

Reputation: 637

errors compiling a program that uses the WordNet library in C

I have successfully installed WordNet library. Now I am trying to use the library in a program, but I cannot get a sample program to build. I get linker errors (undefined reference) even though the library search path is correct.

Here is the error:

gcc -g -I/usr/local/WordNet-3.0/include  -o wordnet_sample.o -c wordnet_sample.c
gcc -L/usr/local/WordNet-3.0/lib -lWN -o wordnet_sample wordnet_sample.o
wordnet_sample.o: In function `main':
/home/user/wordnet_sample/wordnet_sample.c:6: undefined reference to `wninit'

This is the Makefile

WORDNET_INSTALL=/usr/local/WordNet-3.0
CFLAGS=-g -I$(WORDNET_INSTALL)/include
LDFLAGS=-L$(WORDNET_INSTALL)/lib -lWN

wordnet_sample: wordnet_sample.o
        gcc $(LDFLAGS) -o wordnet_sample wordnet_sample.o

wordnet_sample.o: wordnet_sample.c
        gcc $(CFLAGS) -o wordnet_sample.o -c wordnet_sample.c

This is the sample program

#include <wn.h>
#include <stdlib.h>
int main(int argc, char **argv) {
    int isOpen = 0 == wninit();
    if (isOpen) {
            printf("wordnet opened!\n");
    }
    else {
            printf("Could not open WordNet dictionary!\n");
    }
    return 0;
}

The WordNet library is installed

[user@local wordnet_sample]$ ls -l /usr/local/WordNet-3.0/lib/
total 180
-rw-r--r-- 1 root root 172344 May  3 15:34 libWN.a
drwxr-xr-x 2 root root   4096 May  3 15:34 wnres

I suspect that I may need more arguments to the linker, but I am really not sure. Does anyone come across this? Does anyone know why the undefined reference error is happening even though I have the right library and library search path?

Upvotes: 1

Views: 440

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 120041

You need to add -c to CFLAGS.

Also the first gcc command needs to have the libraries after the objects. You may put LDFLAGS in the end:

gcc -o wordnet_sample wordnet_sample.o $(LDFLAGS)

Better yet, have separate LDFLAGS (linker options) and LIBS (actual libraries to link). So:

LDFLAGS=-L$(WORDNET_INSTALL)/lib
LIBS=-lWN
....
gcc $(LDFLAGS) -o wordnet_sample wordnet_sample.o $(LIBS)

Upvotes: 1

Related Questions