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

Hightman(马明练) hightman at zuaa.zju.edu.cn
Sat Nov 29 00:48:26 GMT 2008


If you call some function from C library (not C++), you should do it such as:
extern "C" {
 #include "something.h";
 ...
}

==============

>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

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        Hightman(马明练)
        hightman at zuaa.zju.edu.cn
          2008-11-29






More information about the Xapian-discuss mailing list