7

I wrote the program kai.c and now am trying to compile it with gcc kai.c -o kai, which returns:

kai.c:5:18: fatal error: string: No such file or directory
 #include <string>
                  ^
compilation terminated.

What can I do?

gcc version is: (Ubuntu 4.8.2-19ubuntu1) 4.8.2
  • ... or replace <string> with <cstring> – αғsнιη Nov 28 '14 at 17:17
  • also program can not see library cstring – qwerty Nov 28 '14 at 17:21
7

C

The string library is the file string.h, so:

#include "string.h"

Example:

#include "string.h"
#include "stdio.h"

void main(){
    char src[2] = "Hi";
    char dest[2];
    strcpy(dest, src);
    printf("%s\n", dest); // Will print Hi
}
8

#include <string> is a C++ directive.

Rename your file to kai.cpp

And compile it with g++ kai.cpp -o kai

  • i need in c, because there is a fork(); – qwerty Nov 28 '14 at 17:17
  • 2
    @qwerty In c++ you also can have/use fork() – αғsнιη Nov 28 '14 at 17:58
  • 1
    @qwerty Everything from C is available in C++ (I think). If you want to use C++, use the g++ command instead of gcc to compile the code. g++ is the GNU C++ compiler, while gcc is the C compiler. On C, <string> in not available in C (if I'm not mistaken, all C headers end in .h). – user180409 Nov 28 '14 at 18:49

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