4

I am trying to compile the complete example 8 provided at the end of this page: http://www.physics.wisc.edu/~craigm/idl/cmpfit.html

but I am getting this error: error: ‘memset’ was not declared in this scope

I have been looking how to solve this error and I saw that some people solved it by adding #include <string.h> to the head of the code. I tried it but I am still getting the same error.

I am using gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) on Ubuntu 12.04 LTS

I am trying to compile with:

g++ -o example example.cpp -lmpfit -lm
7
  • you need to add <string.h> header - if you haven't done so already? Also, do this g++ -g -Wall example.cpp -o example.o -lmpfit -lm. Please show us how you are doing memset with the actual printout of the message.
    – ha9u63a7
    Commented Nov 9, 2014 at 20:05
  • Yes, I have already added <string.h>. I still see the same error. By adding -g -Wall in the compilation order I additionally got this error: example.cpp:59:10: warning: unused variable ‘pactual’ [-Wunused-variable] Commented Nov 9, 2014 at 20:06
  • That code snippet will not compile by itself. You need to insert it as a part of a function - "example 8" shows a pretty complete program which should compile out-of-the-box. Commented Nov 9, 2014 at 20:10
  • You should use std::memset();.
    – Galik
    Commented Nov 9, 2014 at 20:10
  • The code I am currently trying to compile is exactly that of the example 8 in the web site. I only added the line #include <string.h> to the head. The error message I got is: example.cpp: In function ‘int main(int, char**)’: example.cpp:66:34: error: ‘memset’ was not declared in this scope Commented Nov 9, 2014 at 20:11

2 Answers 2

10

If you use C you should include string.h

Otherwise, if you use C++ you should use cstring

C: #include <string.h>

C++: #include <cstring>
5
  • You should use #include <string.h> in both C and C++, if you want to use ::memset. #include <cstring> gets you std::memset, and if it also makes ::memset available, it does so by accident. The Standard permits it but does not require it to do so.
    – Ben Voigt
    Commented Nov 9, 2014 at 20:23
  • Now I have both includes #include <string.h>, #include <cstring> and std::memset. I am still getting the same error. Commented Nov 9, 2014 at 20:29
  • Please give us in a paste.bin the source code of your program. I will try to compile it with gcc in my PC. Commented Nov 9, 2014 at 22:39
  • Hi, the code is exactly the "example 8" that appears at the end of this web page: physics.wisc.edu/~craigm/idl/cmpfit.html. I have just added #include <string.h> and/or #include <cstring> and tried with "std::memset" instead of "memset" Commented Nov 10, 2014 at 8:14
  • I added "#include <string.h>" and I compiled it with gcc without errors. Commented Nov 10, 2014 at 21:10
1

I updated gcc to gcc-4.9 and I could compile just adding "#include " to the head of the code. Then I tried with gcc-4.8 and gcc-4.7, and again I was able to compile the code with no problem. Perhaps my previous compiler (gcc-4.6) was not correctly installed?¿. I will keep working with the last version of gcc. Thank you all for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.