C++ 对运算符 new 的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3735804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined reference to operator new
提问by mbyrne215
I'm trying to build a simple unit test executable, using cpputest. I've built the cpputest framework into a static library, and am now trying to link that into an executable. However, I'm tied into a fairly complicated Makefile setup, because of the related code.
我正在尝试使用 cputest 构建一个简单的单元测试可执行文件。我已经将 cpputest 框架构建到一个静态库中,现在正试图将它链接到一个可执行文件中。但是,由于相关代码,我陷入了相当复杂的 Makefile 设置。
This is my command line:
这是我的命令行:
/usr/bin/qcc -V4.2.4,gcc_ntoarmle_acpp-ne -lang-c++ -O2 -g -g -o Application/UnitTests/Tests/symbols/UnitTestExe -Wl,--start-group Application/UnitTests/Tests/../.objs/main.o Application/UnitTests/lib/libcpputest.a -Wl,--end-group -lm
I'm getting many errors like the following:
我收到很多错误,如下所示:
Application/UnitTests/lib/libcpputest.a(CommandLineTestRunner.o): In function `CommandLineTestRunner::parseArguments(TestPlugin*)':
Application/UnitTests/cpputest/src/CppUTest/.objs/../CommandLineTestRunner.cpp:114: undefined reference to `operator new(unsigned int, char const*, int)'
I can't figure out what's causing this. Don't I get operator new for free with C++?
我无法弄清楚是什么原因造成的。难道我不能用 C++ 免费获得 operator new 吗?
采纳答案by sbi
There's very little information in your question to work from, but it looks like some code uses some form of placement new, and while that special operator new
is declared(the compiler finds it and compiles the code using it), the linker can't find its definition.
有没有在你的问题,从工作的信息非常少,但它看起来像一些代码使用某种形式的安置新的,虽然是特殊operator new
的声明(编译器发现它并使用它编译的代码),链接器不能找到它定义。
(Since this old answer of mine seems to still get attention: See herefor an extensive discussion on declaration vs. definition.)
(因为我的这个旧答案似乎仍然受到关注:有关声明与定义的广泛讨论,请参见此处。)
回答by zvrba
You probably need to link with the C++ support runtime library. This happens automatically when you invoke g++. On linux, this is achieved by adding -lstdc++ flag to the linker. You have to figure out how to do the same on your platform.
您可能需要链接 C++ 支持运行时库。当您调用 g++ 时,这会自动发生。在 linux 上,这是通过向链接器添加 -lstdc++ 标志来实现的。你必须弄清楚如何在你的平台上做同样的事情。
回答by BavidDowman
You need to rebuild your code from scratch, including the library. I got this error because I inadvertently copied object files compiled on another machine (with the rest of the source) to my machine. Most likely this disturbs the linking step since there are now two types of object files, native (for modified source files) and non-native (all others). I am guessing here, but the operator 'new' means slightly different things on different architectures and that's why you are getting this error.
您需要从头开始重新构建代码,包括库。我收到此错误是因为我无意中将在另一台机器上编译的目标文件(以及其余的源代码)复制到了我的机器上。这很可能会干扰链接步骤,因为现在有两种类型的目标文件,本地(用于修改的源文件)和非本地(所有其他)。我在这里猜测,但是运算符“new”在不同架构上的含义略有不同,这就是您收到此错误的原因。
p.s. I know this is way too late for a useful answer but I'm still posting this for the record.
ps我知道这对于有用的答案来说太晚了,但我仍在发布此记录以供记录。
回答by Shakaron
Maybe you're calling gcc
, the C compiler instead of g++
, which is the C++ compiler.
也许您正在调用gcc
C 编译器而不是g++
C++ 编译器。
回答by Maxim Suslov
For QNX 6.5.0 I have specified flag -lang-c++
for qcc
(gcc
) to avoid the error.
对于 QNX 6.5.0,我-lang-c++
为qcc
( gcc
)指定了标志以避免错误。