问题描述
我有一个测试文件(只为链路测试),我超载新/ delete操作符与我自己的malloc /免费库调用libxmalloc.a。但我不断收到underdefined提到的误差链接静态库时以下,甚至更改test.o和-lxmalloc的顺序。但是,一切都与其他C程序链接这个库工作得很好。我这个问题和AP preciate任何线索很困惑。
错误消息:
G ++ -m64 -O3 -I / usr / include目录/风气-I / usr / include目录/氯化钠/ x86_64的-c -o test.o TEST.CPP
G ++ -m64 -O3 -L。 -o演示test.o -lxmalloc
test.o:在函数'new操作符(无符号长):
TEST.CPP :(文字+为0x1):未定义引用'的malloc(无符号长)
test.o:在函数'delete操作符(无效*):
。TEST.CPP :(文字+ 0×11):未定义的引用`免费(无效*)'
test.o:在功能`运营商新的[](无符号长):
。TEST.CPP :(文字+ 0×21):未定义引用'的malloc(无符号长)
test.o:在函数'的operator delete [](无效*):
。TEST.CPP :(文字+ 0X31):未定义的引用`免费(无效*)'
test.o:在函数'主':
TEST.CPP :( text.startup + 0xC的):未定义引用'的malloc(无符号长)
TEST.CPP :( text.startup +的0x19):未定义引用'的malloc(无符号长)
TEST.CPP :( text.startup + 0X24):未定义的引用`免费(无效*)'
TEST.CPP :( text.startup + 0X31):未定义的引用`免费(无效*)'
collect2:劳工处返回1退出状态
使:*** [试玩]错误1
我TEST.CPP文件:
的#include<双/ xalloc.h>
#包括LT&;双/ xmalloc.h>
无效*
运营商新的(为size_t SZ)
{
返回的malloc(SZ);
}
空虚
经营者删除(无效* PTR)
{
免费(PTR);
}
无效*
运营商新的[](为size_t SZ)
{
返回的malloc(SZ);
}
空虚
运营商删除[](无效* PTR)
{
免费(PTR);
}
INT
主要(无效)
{
为int * IP = INT新;
为int * AP =新INT [3];
删除的iP;
删除[] AP;
返回0;
}
我的Makefile:
CFLAGS + = -m64 -O3 -I / usr / include目录/风气-I / usr / include目录/氯化钠/ x86_64的
CXXFLAGS + = -m64 -O3
LIBDIR + = -L。
LIBS + = -lxmalloc
所有:演示
演示:test.o
$(CXX)$(CXXFLAGS)$(LIBDIR)-o演示test.o $(LIBS)
test.o:TEST.CPP
$(CXX)$(CFLAGS)-c -o $ @ $<
清洁:
- RM -f *的.o演示
Did you notice that C and C++ compilation create different symbol names on object file level? It's called 'name mangling'.
The (C++) linker would show undefined references as demangled symbols in the error message, which might confuse you. If you inspect your test.o
file with nm -u
you'll see that the referenced symbol names don't match with those provided in your library.
If you want to use functions linked in as externals that were compiled using the plain C compiler, you'll need their function declarations enclosed in an extern "C" {}
block which suppresses C++ name mangling for everything declared or defined inside, e.g.:
extern "C"
{
#include <dual/xalloc.h>
#include <dual/xmalloc.h>
}
Even better, you might wrap your function declarations in your header files like this:
#if defined (__cplusplus)
extern "C" {
#endif
/*
* Put plain C function declarations here ...
*/
#if defined (__cplusplus)
}
#endif
这篇关于&QUOT;未定义的引用&QUOT;联用C ++ code静态C库时的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!