Why is strcmp unknown to clang?
Asked Answered
D

4

15

I have a basic program that compares two strings :

#include <string>
#include <iostream>

using namespace std;

int main (int argc, char *argv[]) {
  if(strcmp (argv[0],"./test") != 0) {
    cout << "not equal" << endl;
  } else {
    cout << "equal" << endl;
  }
  return 0;
}

it compiles with gcc but not with clang :

 > clang -o test test_clang.cpp 
test_clang.cpp:7:6: error: use of undeclared identifier 'strcmp'
  if(strcmp (argv[0],"./test") != 0) {
     ^
1 error generated.

Why doesn't it compile with clang ?

EDIT: People are getting harsh on stack overflow, up to the point that I am hesitating to post a question. The question above has a simple answer, fine, but is it normal to down-vote questions (twice in the first minute!) because they have a simple, yet non obvious, answer ?

Deonnadeonne answered 21/6, 2012 at 12:13 Comment(8)
I have no clue why people downvoted this. It's clearly stated and a valid question.Braca
"This question does not show any research effort". The first hit in Google for "strcmp" has a code example with #include <string.h> (which is also a valid solution). Minimal research would have answered this question. That is why I downvoted it. Trivial questions diminish the value of this site.Acetify
Making a minimal example and comparing results on two compilers is research effort.Braca
Before posting, I copied and pasted the code of the first example on Google in a file and thought "I should remove these '.h' from the includes like I do for iostream". I was wrong but certainly not lazy. Moreover, my minimal example worked with gcc, thus my astonishment. I reckon you are a bit fast to draw conclusions and judge me and my intentions on this one.Deonnadeonne
Sorry, I really don't mean to offend! However, not knowing the difference between headers with .h at the end and those without, is a problem that is best solved by reading a book on C++. I think that would be better for you, and I think it would be better for Stack Overflow. Again; I don't mean to be harsh or mean, but Stack Overflow isn't actually suited for everything.Acetify
Ok, no problem, I will read about it :) I guess it would have helped if the down-vote came along with a comment to explain why it was being down-voted. Let's just forget about all this.Deonnadeonne
3 downvotes and 3 compensating upvotes gives you +9 rep net, so it's not so bad :-)Rustproof
@TadeuszKopec Yeah, this is what I noticed :-)Deonnadeonne
C
20

Use

#include <string.h>

or

#include <cstring>

instead of

#include <string>

The string header is for the std::string from C++. string.h is for C zero terminated char* strings. cstring is like string.h but for C++.

The reason it worked with gcc is probably different warning/error level settings. It is possible to compile the code without #including the header and having the declaration of strcmp. The compiler will not be able to do type checking but the symbol still gets resolved by the linker.

You can also avoid using strcmp completely and write

#include <string>
#include <iostream>

int main (int argc, char *argv[]) {
  std::string command = argv[0];

  if( command != "./test" ) {
    std::cout << "not equal" << endl;
  } else {
    std::cout << "equal" << endl;
  }
  return 0;
}

Using a std::string on one side of the comparison will cause the "./test" string to be converted into a std::string as well and the comparison will be done by the == operator of the std::string class.

Cimbri answered 21/6, 2012 at 12:16 Comment(4)
string.h is C header; proper C++ header is cstring, as it keeps everything in namespace std.Anticlerical
@Anticlerical OK, good to know. There seems to be no disadvantage of using string.h even in C++ though.Cimbri
I take this answer as accepted because it gives explanations I found interestingDeonnadeonne
@TomasAndrle Note that string.h is deprecated in C++ and should avoid using it.Antiparticle
R
11

You're not including the correct header file

#include <cstring>
Restitution answered 21/6, 2012 at 12:15 Comment(0)
B
5

You need to #include <cstring> (or possibly #include <string.h>.)

Many compilers include extra standard headers when you include another. The Standard allows this; it's your responsibility to use the headers that guarantee declarations for what you use, not just headers that happen to have the declarations for your compiler.

Braca answered 21/6, 2012 at 12:16 Comment(3)
string.h is C header; only proper C++ header is cstring, as it keeps everything in namespace std.Anticlerical
@DaveS, any proof? I cannot find anything about them being deprecated in standard.Anticlerical
@Griwes: Ooops. I completely misread the section. I've deleted the comment.Homans
S
3

You have to include <cstring>. <string> is the header for C++ strings.

Samuele answered 21/6, 2012 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.