链接静态C库和C ++代码时出现“未定义的引用”错误
我有一个测试文件(仅用于链接测试),其中我使用我自己的/ 库调用来重载new
/ delete
运算符。但是在链接静态库时,我一直得到“未定义的引用”错误,即使我改变了和的顺序。但是一切都适用于连接这个库的其他C程序。我对这个问题很困惑,并且感谢任何线索。malloc
free
libxmalloc.a
test.o
-lxmalloc
错误消息:
g++ -m64 -O3 -I/usr/include/ethos -I/usr/include/nacl/x86_64 -c -o test.o test.cpp g++ -m64 -O3 -L. -o demo test.o -lxmalloc test.o: In function `operator new(unsigned long)': test.cpp:(.text+0x1): undefined reference to `malloc(unsigned long)' test.o: In function `operator delete(void*)':test.cpp:(.text+0x11): undefined reference to `free(void*)' test.o: In function `operator new[](unsigned long)': test.cpp:(.text+0x21): undefined reference to `malloc(unsigned long)'test.o: In function `operator delete[](void*)': test.cpp:(.text+0x31): undefined reference to `free(void*)' test.o: In function `main':test.cpp:(.text.startup+0xc): undefined reference to `malloc(unsigned long)' test.cpp:(.text.startup+0x19): undefined reference to `malloc(unsigned long)' test.cpp:(.text.startup+0x24): undefined reference to `free(void*)'test.cpp:(.text.startup+0x31): undefined reference to `free(void*)' collect2: ld returned 1 exit status make: *** [demo] Error 1
我的test.cpp
档案:
#include <dual/xalloc.h>#include <dual/xmalloc.h>void*operator new (size_t sz){ return malloc(sz);}voidoperator delete (void *ptr){ free(ptr);}void*operator new[] (size_t sz){ return malloc(sz);}voidoperator delete[] (void *ptr){ free(ptr);}intmain(void){ int *iP = new int; int *aP = new int[3]; delete iP; delete[] aP; return 0;}
我的Makefile
:
CFLAGS += -m64 -O3 -I/usr/include/ethos -I/usr/include/nacl/x86_64 CXXFLAGS += -m64 -O3 LIBDIR += -L.LIBS += -lxmalloc all: demo demo: test.o $(CXX) $(CXXFLAGS) $(LIBDIR) -o demo test.o $(LIBS)test.o: test.cpp $(CXX) $(CFLAGS) -c -o $@ $<clean:- rm -f *.o demo
扬帆大鱼