0

I have RHEL with GCC 4.1.2 and my C++ code is compiling as expected. But same code when I try to compile on CentOS 7.2 with GCC 6.2.0 it fails with below error:

compiling UpcSummarization.o ...
UpcSummarization.cpp: In member function âvoid UpcSummarization::LoadUpcList(std::__cxx11::string)â:
UpcSummarization.cpp:480:40: error: âmemsetâ was not declared in this scope
         memset(&pBuffer, 0, sizeof(100));
                                        ^

I am including below header file:

string

If I include string.h or cstring it will pass.

Any idea how to make it work with just string include file? Migrating code base to include string.h or cstring is not an option

EDIT

Yes it was string.

May be I should have phrased it as second option instead of not an option

Before starting code migration, I need to rule out the any possibility of compiler options to make it work work as is.

I can install extra libs if required and change the way it is being compile. Not actually editing the code.

Krish Sanj
  • 107
  • 9
  • Well, `memset` is in `cstring`. Why is including the correct header not an option?! – Biffen Jan 25 '17 at 05:48
  • 2
    Unrelated, but are you sure `sizeof(100)` is what you need here? – Moshe Gottlieb Jan 25 '17 at 05:56
  • @Biffen, I am not sure whether I am compiling the correct way. and not sure I should really change the code. Since it is working on older version of OS and GCC, I need to understand that can it be fixed by changing the way it is compiled. – Krish Sanj Jan 25 '17 at 06:49
  • @KrishSanj It's probably working with the earlier version because some header *happens to* include some other header. It does not mean that it's correct. What *is* correct, however, is to include `cstring` when you want to use `memset` (or, arguably, to stop using `memset`). – Biffen Jan 25 '17 at 06:53
  • 2
    @KrishSanj "Not actually editing the code" - in that case you'll most probably won't be able to port to 6.2, sorry. – yugr Jan 25 '17 at 07:15

1 Answers1

0

I am including below header file: sting

string?

Any idea how to make it work with just string include file?

You can create a dummy string file and store it with your other headers. Inside string you do for example

#include <string.h>

Migrating code base to include string.h or cstring is not an option

Note that you most probably won't be able to port your code to 6.2 if you can't modify it even to this small extent. 4.1 and 6.2 are 10 years apart so your code will definitely require changes/fixes to work with newer compiler.

yugr
  • 19,769
  • 3
  • 51
  • 96
  • @KrishSanj If you can fix your code to use `string.h` it's of course preferred. – yugr Jan 25 '17 at 06:36
  • 1
    In C++ use `cstring`, not `string.h`. – Biffen Jan 25 '17 at 07:03
  • @Biffen Is there any particular benefit from doing this? – yugr Jan 25 '17 at 07:14
  • I tried to find a source, but it seems it's more debatable than I thought. Anyway, [here](https://stackoverflow.com/questions/8380805/difference-between-string-h-and-cstring)'s something. – Biffen Jan 25 '17 at 07:22
  • @yugr with the symbols are guaranteed to be in the `std::` namespace making it harder to accidentally referece the wrong symbols. – Galik Jan 25 '17 at 07:28
  • @Galik Yeah, I know about std:: but I wonder how useful it's in practice (anyone "accidentally" referencing `memset`, ever?). – yugr Jan 25 '17 at 07:43
  • If you reference your symbols using `std::` or with `using std::symbol;` etc (as you should) then including `` can lead to broken code if you change/upgrade the compiler. On the other hand `` guarantees your `std::...` references will always be there. – Galik Jan 25 '17 at 07:46
  • But if you don't use `std::`, including `` will also lead to broken code :) – yugr Jan 25 '17 at 07:51