mbyrne215
mbyrne215

Reputation: 2334

Undefined reference to operator new

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

Upvotes: 32

Views: 63378

Answers (7)

Stepan Yakovenko
Stepan Yakovenko

Reputation: 9216

Sometimes adding -lstdc++ is not enough. You should add it to the right place. For example I had list like this, not working:

target_link_libraries(cfr2 pthread m stdc++ "${CMAKE_SOURCE_DIR}/compressor/libcompressor.a" )

But this one works fine:

target_link_libraries(cfr2 pthread m "${CMAKE_SOURCE_DIR}/compressor/libcompressor.a" stdc++)

It'd be great if someone explained it in the comment section.

Upvotes: 0

quent
quent

Reputation: 2185

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.

Upvotes: 0

zvrba
zvrba

Reputation: 24574

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.

Upvotes: 74

sbi
sbi

Reputation: 224139

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.)

Upvotes: 8

Maxim Suslov
Maxim Suslov

Reputation: 5485

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

Upvotes: 0

Shakaron
Shakaron

Reputation: 1065

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

Upvotes: 19

BavidDowman
BavidDowman

Reputation: 101

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.

Upvotes: 4

Related Questions