27

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.

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++?

4

6 回答 6

71

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 the -lstdc++ flag to the linker. You have to figure out how to do the same on your platform.

于 2010-09-17T14:05:16.870 回答
9

Maybe you're calling gcc, the C compiler instead of g++, which is the C++ compiler.

于 2017-11-22T05:31:42.640 回答
8

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.

(Since this old answer of mine seems to still get attention: See here for an extensive discussion on declaration vs. definition.)

于 2010-09-17T13:59:11.453 回答
4

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.

p.s. I know this is way too late for a useful answer but I'm still posting this for the record.

于 2012-06-12T06:43:32.340 回答
0

For QNX 6.5.0 I have specified flag -lang-c++ for qcc (gcc) to avoid the error.

于 2017-12-15T06:38:48.657 回答
0

Like the original post, in my case this error happened while trying to link a software using CppUTest framework.

In my case, the source of the problem seems to be related to the fact I disabled the MEMORY_LEAK_DETECTION compile option of CppUTest. I enabled it again, which solved the problem.

于 2021-01-26T09:10:50.700 回答