This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: error: 'string' undeclared (first use in this function)
Hi,
well, it is almost impossible to give a usefull answer without knowing
the source code. The best would be, if you could send a small demo-file,
which shows the problem. But already from your errors:
> I have #include <string.h> , but why am I still can't declare string ?
>
>
> When I compile my code ,show the error msg as follows :
> root@acer:~/probeServer# gcc probec.c -o probec -lm -lpthread
> probec.c:1:18: error: string: No such file or directory
Here gcc claims, that he did not find the file "string". Are you sure,
that you did not tried to include "string" instead of "string.h"
> probec.c: In function 'main':
> probec.c:458: error: 'string' undeclared (first use in this function)
If I remember correctly, "string" is not defined in the header-File
"string.h" (which is a C header)? Do you mean the C++-class std::string?
If yes, you should:
- tell the compiler that you are using C++ (e.g. naming your file
"probec.cc")
- compile with "g++" instead of "gcc" to guarantee he uses the correct
libraries for linking
- use "include <string>" to include the C++-header (containing the
definition of std::string), neither "include <string.h>" (that's
deprecated in C++) nor "include <cstring>" (that includes the
C-header, which is called "string.h" in C, but does NOT contain the
class std::string ...)
- add "using namespace std;" if you want to use the class std::string
by simply writing "string".
But - maybe you want to do something else?
Axel