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

quent
  • 1,301
  • 1
  • 16
  • 22
mbyrne215
  • 2,084
  • 4
  • 19
  • 16
  • It’s really hard to help based on this information. Try to *reduce* the conditions necessary to reproduce the problem. The above command line is far too complex, even if we assume that the code is straightforward and doesn’t do things such as redefining `operator new`. – Konrad Rudolph Sep 17 '10 at 13:43
  • Is the first error reported an operator new error? If not, what is the first error reported? – Joel Rondeau Sep 17 '10 at 13:50
  • Yes, the only errors reported are operator new errors. That one shown is the first one. – mbyrne215 Sep 17 '10 at 13:50
  • 1
    Cases where I see undefined reference error, generally meant improper linking and proper paths not provided where libraries are installed. – DumbCoder Sep 17 '10 at 13:54
  • Edited to remove unnecessary elements from command line. – mbyrne215 Sep 17 '10 at 13:55

6 Answers6

70

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.

Adrian Mole
  • 30,672
  • 69
  • 32
  • 52
zvrba
  • 23,242
  • 3
  • 52
  • 65
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.)

sbi
  • 204,536
  • 44
  • 236
  • 426
  • I'm not sure what other info to add. The code is really simple; it doesn't redefine 'new', so I can't figure why the linker can't find it from the standard library. – mbyrne215 Sep 17 '10 at 14:02
  • 1
    @mbyrne215: You could add the simplest code that reproduces this. The error message clearly mentions an `operator new(unsigned int, char const*, int)` (called from `CommandLineTestRunner::parseArguments(TestPlugin*)`), which is clearly not the standard version of that operator. – sbi Sep 17 '10 at 14:07
  • 2
    You're right; I got so hung up looking for why the standard libs weren't working, I didn't look through the 3rd party library closely. It was secretly redefining new. I removed that part and all is good. Thanks. – mbyrne215 Sep 17 '10 at 14:21
8

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

Shakaron
  • 857
  • 8
  • 22
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.

BavidDowman
  • 101
  • 4
  • in my case there were some .o files, from my predecessor, left after a make clean everything worked like a charm – aldr Apr 15 '16 at 21:15
0

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

Maxim Suslov
  • 2,719
  • 1
  • 20
  • 21
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.

quent
  • 1,301
  • 1
  • 16
  • 22