[Xapian-discuss] undefined reference to "function name"

Zhiguo Li zhiguo.li at gmail.com
Wed Nov 26 05:33:10 GMT 2008


Hi,

Maybe my last email was too long, and I will simplify it:

The following is the error I got when I tried to compile.

/bin/bash ./libtool  --tag=CXX   --mode=link g++ -Wall -W 
-Wredundant-decls -Wpointer-arith -Wcast-qual -Wcast-align 
-Wno-long-long -Wformat-security -Wconversion -fno-gnu-keywords -Wundef 
-Wshadow -Winit-self -Wstrict-overflow=5 -fvisibility=hidden -g -O2   -o 
examples/simplesearch examples/simplesearch.o  libxapian.la
g++ -Wall -W -Wredundant-decls -Wpointer-arith -Wcast-qual -Wcast-align 
-Wno-long-long -Wformat-security -Wconversion -fno-gnu-keywords -Wundef 
-Wshadow -Winit-self -Wstrict-overflow=5 -fvisibility=hidden -g -O2 -o 
examples/.libs/simplesearch examples/simplesearch.o  
./.libs/libxapian.so -lz
examples/simplesearch.o: In function `main':
/home/zhiguoli/xapian/examples/simplesearch.cc:83: undefined reference 
to `something()'


Obviously g++ didn't see symbol "something", but then I tried nm and 
found it indeed in libxapian.so.

nm libxapian.so | grep "something"
001cfb70 t _Z9somethingv

So why is this? Could somebody help me please? I know this is more about 
a question on programming instead of Xapian, but if I compile it else 
where it could pass easily.

Thanks,
Kevin

Zhiguo Li wrote:
> Hi,
>
> I am trying to add some functions to the Xapian library but when I tried 
> to compile it, the linker give me the "undefined reference" error.
>
> Here is what  I did:
>
> Under directory matcher, I created two files "something.cc" and 
> "something.h".
>
> File "something.h":
>
> #include "xapian/enquire.h"
> #include "xapian/query.h"
> #include "database.h"
> void something();
>
> File "something.cc":
> #include "something.h"
> void something() {};
>
> And in examples/simplesearch.cc, I included "something.h" and called 
> something() in the main function.
>
> In the Makefile.mk under matcher directory, I added matcher/something.h 
> in section "noinst_HEARDERS" and matcher/something.cc in section 
> "libxapian_la_SOURCES".
>
> Then I tried to compile the project, and from the compiler output it 
> looks "something.cc" was compiled fine:
>
>     /bin/bash ./libtool  --tag=CXX   --mode=compile g++ -DHAVE_CONFIG_H 
> -I. -I./common -I./include     -Wall -W -Wredundant-decls 
> -Wpointer-arith -Wcast-qual -Wcast-align -Wno-long-long 
> -Wformat-security -Wconversion -fno-gnu-keywords -Wundef -Wshadow 
> -Winit-self -Wstrict-overflow=5 -fvisibility=hidden -g -O2 -MT 
> matcher/something.lo -MD -MP -MF $depbase.Tpo -c -o matcher/something.lo 
> matcher/something.cc &&\
>     mv -f $depbase.Tpo $depbase.Plo
>  g++ -DHAVE_CONFIG_H -I. -I./common -I./include -Wall -W 
> -Wredundant-decls -Wpointer-arith -Wcast-qual -Wcast-align 
> -Wno-long-long -Wformat-security -Wconversion -fno-gnu-keywords -Wundef 
> -Wshadow -Winit-self -Wstrict-overflow=5 -fvisibility=hidden -g -O2 -MT 
> matcher/something.lo -MD -MP -MF matcher/.deps/something.Tpo -c 
> matcher/something.cc  -fPIC -DPIC -o matcher/.libs/something.o
>  g++ -DHAVE_CONFIG_H -I. -I./common -I./include -Wall -W 
> -Wredundant-decls -Wpointer-arith -Wcast-qual -Wcast-align 
> -Wno-long-long -Wformat-security -Wconversion -fno-gnu-keywords -Wundef 
> -Wshadow -Winit-self -Wstrict-overflow=5 -fvisibility=hidden -g -O2 -MT 
> matcher/something.lo -MD -MP -MF matcher/.deps/something.Tpo -c 
> matcher/something.cc -o matcher/something.o >/dev/null 2>&1
>
>
> But when "examples/simplesearch.cc" was compiled, it gave the following 
> error:
>
> depbase=`echo examples/simplesearch.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
>     g++ -DHAVE_CONFIG_H -I. -I./common -I./include     -Wall -W 
> -Wredundant-decls -Wpointer-arith -Wcast-qual -Wcast-align 
> -Wno-long-long -Wformat-security -Wconversion -fno-gnu-keywords -Wundef 
> -Wshadow -Winit-self -Wstrict-overflow=5 -fvisibility=hidden -g -O2 -MT 
> examples/simplesearch.o -MD -MP -MF $depbase.Tpo -c -o 
> examples/simplesearch.o examples/simplesearch.cc &&\
>     mv -f $depbase.Tpo $depbase.Po
> /bin/bash ./libtool  --tag=CXX   --mode=link g++ -Wall -W 
> -Wredundant-decls -Wpointer-arith -Wcast-qual -Wcast-align 
> -Wno-long-long -Wformat-security -Wconversion -fno-gnu-keywords -Wundef 
> -Wshadow -Winit-self -Wstrict-overflow=5 -fvisibility=hidden -g -O2   -o 
> examples/simplesearch examples/simplesearch.o  libxapian.la
> g++ -Wall -W -Wredundant-decls -Wpointer-arith -Wcast-qual -Wcast-align 
> -Wno-long-long -Wformat-security -Wconversion -fno-gnu-keywords -Wundef 
> -Wshadow -Winit-self -Wstrict-overflow=5 -fvisibility=hidden -g -O2 -o 
> examples/.libs/simplesearch examples/simplesearch.o  
> ./.libs/libxapian.so -lz
> examples/simplesearch.o: In function `main':
> /home/zhiguoli/xapian/examples/simplesearch.cc:83: undefined reference 
> to `something()'
> collect2: ld returned 1 exit status
> make[2]: *** [examples/simplesearch] Error 1
>
>
> My question is why the linker couldn't find the definition of function 
> "something()"? How to make it work? I am not clear about the overall 
> design structure of Xapian yet, but I would like to make some quick hack 
> and very small changes (for example, add a few functions to libxapian).
>
> Thanks,
> Kevin
>
>
> _______________________________________________
> Xapian-discuss mailing list
> Xapian-discuss at lists.xapian.org
> http://lists.xapian.org/mailman/listinfo/xapian-discuss
>
>   




More information about the Xapian-discuss mailing list