Compile issues with gcc and Memory Leak headers

689 views
Skip to first unread message

David Cozens

unread,
Sep 21, 2016, 4:22:35 AM9/21/16
to cpputest
I've just started to evaluate CppUTest (I'm already familiar with googletest and practice TDD). I've been finding it a pleasure to use, fast and lightweight.I hit one small issue when trying out the memory leak detection features. I'm doing the evaluation on Ubuntu 16.04, fully updated and having installed CppUTest with apt-get.

My UUT has a #include of malloc.h, and I have added the include files for mocking malloc and new to the CFLAGS and CXXFLAGS as directed in the help. When I compile I get the errors below.
I found that I can completely work around the issue, and the memory leak detection also works by adding -D_MALLOC_H to CFLAGS so that the body of malloc.h is not seen by my UUT. Is this the preferred work around? It may help others if a tip was added to the help.

3:09:29 **** Incremental Build of configuration Debug for project CircularBuffer ****

make test 

Building file: ../CircularBuffer.c

Invoking: GCC C Compiler

gcc -includeCppUTest/MemoryLeakDetectorMallocMacros.h -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"CircularBuffer.d" -MT"CircularBuffer.o" -o "CircularBuffer.o" "../CircularBuffer.c"

/usr/include/malloc.h:38:35: error: expected declaration specifiers or ‘...’ before string constant

 extern void *malloc (size_t __size) __THROW __attribute_malloc__ __wur;

                                   ^

/usr/include/malloc.h:38:35: error: expected declaration specifiers or ‘...’ before numeric constant

 extern void *malloc (size_t __size) __THROW __attribute_malloc__ __wur;

                                   ^

/usr/include/malloc.h:41:51: error: expected declaration specifiers or ‘...’ before string constant

 extern void *calloc (size_t __nmemb, size_t __size)

                                                   ^

/usr/include/malloc.h:41:51: error: expected declaration specifiers or ‘...’ before numeric constant

 extern void *calloc (size_t __nmemb, size_t __size)

                                                   ^

/usr/include/malloc.h:49:49: error: expected declaration specifiers or ‘...’ before string constant

 extern void *realloc (void *__ptr, size_t __size)

                                                 ^

/usr/include/malloc.h:49:49: error: expected declaration specifiers or ‘...’ before numeric constant

 extern void *realloc (void *__ptr, size_t __size)

                                                 ^

/usr/include/malloc.h:53:30: error: expected declaration specifiers or ‘...’ before string constant

 extern void free (void *__ptr) __THROW;

                              ^

/usr/include/malloc.h:53:30: error: expected declaration specifiers or ‘...’ before numeric constant

 extern void free (void *__ptr) __THROW;

                              ^

make: *** [CircularBuffer.o] Error 1

subdir.mk:35: recipe for target 'CircularBuffer.o' failed


23:09:29 Build Finished (took 102ms)

Bas Vodde

unread,
Sep 21, 2016, 7:45:01 AM9/21/16
to cppu...@googlegroups.com

Hi David,

It is probably best to create an include file and use —include for including that file before everything else.

In that include file, you’ll *first* include malloc.h before anything else.

That leads to the #define malloc to be done after the malloc include, and then you won’t get the malloc.h error that you have now.

This is described at:

You just need your own file that first include malloc and THEN MemoryLeakDetectorMallocMacros.h.

Hope that helps!

Thanks,

Bas


--
You received this message because you are subscribed to the Google Groups "cpputest" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cpputest+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Cozens

unread,
Sep 21, 2016, 8:11:13 AM9/21/16
to cpputest
Hi Bas,

I agree that's a nice work around. The example in the documentation that you referred to is under the heading of resolving conflicts with STL. I was testing a pure C application and so hadn't read that section. At least the issue is raised in case others hit the same problem.

As a thought is there a downside to #including malloc.h within MemoryLeakDetectorMallocMacros.h? I can see the argument against doing so for STL - too many headers, but for malloc?

regards David

Bas Vodde

unread,
Sep 21, 2016, 8:25:43 AM9/21/16
to cppu...@googlegroups.com

Hi,

The downside is a bit more header includes. Which for something like malloc doesn’t actually matter.

I’m somewhat surprised you got the error though as CppUTest ought to also include malloc. Not sure where that goes wrong. Perhaps ought to check it more closely :)

Thanks!

Bas
Reply all
Reply to author
Forward
0 new messages