tags:

views:

164

answers:

1

Hi,

why do i get error: 'strcmp': identifier not found in visual studio 2010 C++ Express

#include <string.h>
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
    printf("%d",(int)strcmp( "str1", "str2" ));

    return 0;
}

Thanks

+15  A: 
:( #include <string.h> :(
#include "stdafx.h"

Fun quirk of the MSVC compiler, it generates the exact same error when you compile it like that. Yes, not a lot of 'fun'. It skips everything to find the stdafx.h precompiled header include directive. The string.h doesn't actually get included. Fix:

#include "stdafx.h"
#include <string.h>

Always put the stdafx.h include first.

Hans Passant
That is quite possibly the most ridiculous behavior I've ever heard of from a compiler. WTF
Tyler McHenry
Good catch. The compiler should warn you about this.
Mark Ransom
Alternatively, you can disable pre-compiled headers setting
AndreyT
@Tyler: no argument from me. Pointing out what C or C++ Standard dictum they violated is however tricky. It is the 'hello world' smoke-out test to a tee, a mistake you only ever make once.
Hans Passant
"Pointing out what C or C++ Standard dictum they violated is however tricky." Perhaps I'm missing something, but how can they not violate the standard? E.g. "6.10.2 Source file inclusion" of C99 standard.
Roman Cheplyaka
I don't know what I did to my VS (*at work; can't check now*), but I can compile C sources without that `stdafx` stuff (possibly, from the comments, I turned off pre-compiled headers) and with a proper `main`.
pmg
@Roman Cheplyaka: MSVC doesn't even *try* to be C99 conformant. Source file inclusion is the least of its problems.
Stephen Canon
@pmg: yes, you can do the sword-fighting thingy if you choose. If you never tried it before, pre-compiled headers done the right way is only a few seconds of your life instead of many minutes.
Hans Passant
@Roman: Precompiled headers are an implementation-specific compiler optimization for improving compile times. In order to improve compile time, an additional requirement is imposed on your source: you must include the designated header first. You can disable this feature, so I can't see how it's a violation of any language standard: it's just an implementation-specific language addition.
James McNellis
Visual C++ should also emit a level 1 warning: "warning C4627: '#include <string.h>': skipped when looking for precompiled header use." If you ignore that warning, then yes, this might be considered "WTF" compiler behavior.
James McNellis