Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Help please! strchr problems

ascotilla
Beginner
872 Views
Hi all,

I hope someone can help me. I'm a newbie in linux and programming and it's taking me a lot of time to go forward with my project. I have to execute a makefile which involves the compilation of two sections of a program: the main program is in Fortran 77, for which I use the ifort compiler, and the input-output module (programio.cpp) is in C++, for which I use the intel compiler. The execution of the makefile leads to the following error:

icc -IC:/usr/local/inputdata/include -O3 -o programio-std.obj -c programio.cpp
programio.cpp(3251): error: identifier "strchr" is undefined
if (strchr(buff, '\\n')) *strchr(buff, '\\n') = '\\0';
^

compilation aborted for programio.cpp (code 2)
make: *** [prgramio-std.obj] Error 2

I have the string.h file, as well as the strchr.al, but they doesn't seem to be in the right place, and I tried to copi them in other places and it's still not working. Do you know where exactly does the program read the libraries from? It seems to be quite stupid, but it's taking me days to sort that out, and I'm still not able, so any help will be more thant welcome.

Thank you in advance!!
0 Kudos
12 Replies
Om_S_Intel
Employee
872 Views
The string.h file is a standard header. It should be in the path. You can check by compiling with gcc.

I am providing the sample below that I tried out.

$ cat tstcase.cpp

#include

#include

int main()

{

char buf[]="Hero jump off the plane\n";

printf("%s\n", buf);

if (strchr(buf, '\n'))

*strchr(buf,'\n')='\0';

printf("%s\n", buf);

return 0;

}

$ icc -V

Intel C Intel 64 Compiler XE for applications running on Intel 64, Vers

ion 12.0.1.107 Build 20101116

Copyright (C) 1985-2010 Intel Corporation. All rights reserved.


$ icc tstcase.cpp

$ ./a.out

Hero jump off the plane

Hero jump off the plane

0 Kudos
ascotilla
Beginner
872 Views
Thank you Sachan!

I won't be able to try it before next week (I'm out of UK and for some reason I'm not being able to access the VPN of the college, where the compilers are). However, I tried another program a friend send to me a couple of days ago, and it worked perfectly with gcc...

Compiling just the c++ part leads to the same error, so it's obviously not a problem of the makefile, and the program has worked properly to other people...so I don't what else can it be..

Any more ideas?
0 Kudos
Judith_W_Intel
Employee
872 Views

This looks wrong for Linux:

-IC:/usr/local/inputdata/include

Linux doesn't have drives like "C:/"

This should probably be:

-I/usr/local/inputdata/include

or as Om stated the directory containing the string.h header file should already be included by icpc.

Are you sure you have an #include before this code?

Using the -E -P options should tell you where the string.h header is located and what is in it, i.e. why there's no prototype for strchr().
0 Kudos
ascotilla
Beginner
872 Views
Hi Judith

Thank you for your answer. I already changed the directory in the makefile and checked that the #include was in the program. Following instructions from a colleague, I also added #include .

The fist thing didn't change anything, and the last lead to hundreds of errors I dind't have before, most of them "undefined references"

I also verified with another colleague that the libraries were in the correct place, so that shouldn't be the problem.

This is really driving me crazy, because I need to have it solved by the end of the month, but I'm not going forward with it at all.

Help me please!

0 Kudos
Judith_W_Intel
Employee
872 Views

You mean #include not #include , correct?

You shouldn't need to also say #include since that's just a C++ version of the string.h header but all the declarations are inside namespace std.

So when you looked at the preprocessed header file what did you see? Where was the string.h file coming from? This is what I see:

sptxl8-541> cat t.cpp

#include

sptxl8-542> icpc -E t.cpp | more
# 1 "t.cpp"

# 1 "/usr/include/string.h" 1 3


...

As you can see my string.h is coming from /usr/include. The strchr prototype should be in there...

sptxl8-543> icpc -E t.cpp >t.E
sptxl8-544> grep strchr t.E
extern char *strchr (__const char *__s, int __c)
extern char *strchrnul (__const char *__s, int __c)



Judy

0 Kudos
ascotilla
Beginner
872 Views
Hi Judy!

Thank you for that, I really need to learn the basics with linux..;-P

The string library is called by #include , no , but i tried to change it and it leads to much more errors not just the strchr (the effect is similar as including the )


I tried:

cat programio.cpp

but the program is really very long, so I can't copy everything here!

After that, I got

[ascotilla@lb-ascotilla-lap bin]$ icpc -E programio.cpp | more

# 1 "lpjio.cpp"

# 1 "/usr/include/c++/4.4.4/iostream" 1 3

(...)

I haven't seen the in there, but it seems that my libraries are in /usr/include/c++/4.4.4/. The strchr is not there; apparently the file is strchr.al, and in that folder I just found files with the .h extension. Should I copy it directly to the folder?


I also tried:


[ascotilla@lb-ascotilla-lap bin]$ icpc -E programio.cpp >programio.E
[ascotilla@lb-ascotilla-lap bin]$ grep strchr programio.E

and I got:

if (strchr(buff, '\n')) *strchr(buff, '\n') = '\0';

which is the only line in the whole program that is driving me crazy..;-P





0 Kudos
aazue
New Contributor I
872 Views
Hi

int count_occ(char* a, char* b)
{
int la =strlen(a);
int lb =strlen(b);
int j;
int x;
int k;
int noc = 0;
for (j= 0;j <= la-1 ;j++)
{
x=0;
if (a == b[0] )

for (k=j; k <= j +lb-1 ; k++)
if (a == b && x <= lb- 1 )
{
x++;
}
if (x == lb)
{
noc++;
}
}
return noc;
}



if (count_occ(buff,"\n")==0)
{

}

Add this function you take your money end month.......
with more time to resolve your strange result compiler...
Sometimes bugs very very strange....
I think is not header is absent
is an fault form interpreted resulting this message header absent..
Difficult to help you more side with only one line is given
is bug relational

Regards
0 Kudos
Judith_W_Intel
Employee
872 Views

Please do the following to see where the string header is coming from.

grep string programio.E | grep "#"


This is what I see (I'm using g++ 4.1.1, looks like you're using 4.4.4):

sptxl8-785> grep string t.E | grep "#"
# 1 "/usr/include/c++/4.1.1/string" 1 3
# 45 "/usr/include/c++/4.1.1/string" 2 3
# 1 "/usr/include/c++/4.1.1/bits/stringfwd.h" 1 3
# 46 "/usr/include/c++/4.1.1/string" 2 3
# 1 "/usr/include/c++/4.1.1/cstring" 1 3
# 51 "/usr/include/c++/4.1.1/cstring" 2 3
# 1 "/usr/include/string.h" 1 3
# 34 "/usr/include/string.h" 2 3
# 119 "/usr/include/string.h" 2 3
# 152 "/usr/include/string.h" 3
# 164 "/usr/include/string.h" 3
# 279 "/usr/include/string.h" 3
# 419 "/usr/include/string.h" 3
# 53 "/usr/include/c++/4.1.1/cstring" 2 3
# 77 "/usr/include/c++/4.1.1/cstring" 3
# 47 "/usr/include/c++/4.1.1/string" 2 3
# 48 "/usr/include/c++/4.1.1/string" 2 3
# 52 "/usr/include/c++/4.1.1/string" 2 3
# 1 "/usr/include/c++/4.1.1/bits/basic_string.h" 1 3
# 46 "/usr/include/c++/4.1.1/bits/basic_string.h" 2 3
# 53 "/usr/include/c++/4.1.1/string" 2 3
# 56 "/usr/include/c++/4.1.1/string" 2 3
# 1 "/usr/include/c++/4.1.1/bits/basic_string.tcc" 1 3
# 57 "/usr/include/c++/4.1.1/string" 2 3
0 Kudos
ascotilla
Beginner
872 Views
Hi Judith,

This is what I got:

# 1 "/usr/include/c++/4.4.4/bits/stringfwd.h" 1 3
# 74 "/usr/include/c++/4.4.4/bits/stringfwd.h" 3
# 1 "/usr/include/c++/4.4.4/string" 1 3
# 44 "/usr/include/c++/4.4.4/string" 2 3
# 47 "/usr/include/c++/4.4.4/string" 2 3
# 51 "/usr/include/c++/4.4.4/string" 2 3
# 1 "/usr/include/c++/4.4.4/bits/basic_string.h" 1 3
# 44 "/usr/include/c++/4.4.4/bits/basic_string.h" 2 3
# 488 "/usr/include/c++/4.4.4/bits/basic_string.h" 3
# 547 "/usr/include/c++/4.4.4/bits/basic_string.h" 3
# 829 "/usr/include/c++/4.4.4/bits/basic_string.h" 3
# 895 "/usr/include/c++/4.4.4/bits/basic_string.h" 3
# 1014 "/usr/include/c++/4.4.4/bits/basic_string.h" 3
# 1058 "/usr/include/c++/4.4.4/bits/basic_string.h" 3
# 1514 "/usr/include/c++/4.4.4/bits/basic_string.h" 3
# 2688 "/usr/include/c++/4.4.4/bits/basic_string.h" 3
# 54 "/usr/include/c++/4.4.4/string" 2 3
# 1 "/usr/include/c++/4.4.4/bits/basic_string.tcc" 1 3
# 240 "/usr/include/c++/4.4.4/bits/basic_string.tcc" 3
# 57 "/usr/include/c++/4.4.4/string" 2 3

Thanks

0 Kudos
ascotilla
Beginner
872 Views
Hi bustaf

I think that as the proram is quite big and complicated, it has had already problems, which is quite anoying.

What's the program you sent me for?? I'm sorry I didn'te get it

Cheers
0 Kudos
aazue
New Contributor I
872 Views
Hi
sorry i am unable to translate correctly an first part of your last answer

you have wrote first exchange
I hope someone can help me. I'm a newbie in linux and programming
and it's taking me a lot of time to go forward with my project.
I have to execute a makefile which involves the compilation
of two sections of a program: the main program is in Fortran 77,
for which I use the ifort compiler, and the input-output module (programio.cpp)
is in C++, for which I use the intel compiler.
The execution of the makefile leads to the following error)


(if (strchr(buff, '\n')) *strchr(buff, '\n') = '\0';)

Function i have wrote to you could result exactly same value in other form
for resolve where you are locked.without call to (strchr)
for find if return line("\n") is present in buff..


(What's the program you sent me for?? I'm sorry I didn'te get it)

if noc return zero, is (return line) is not present in buff..

Regards
nb:
strch work perfectly all (g++ and icpc version) ,only your (input-output module)
is wrong or obsolete.

0 Kudos
Judith_W_Intel
Employee
872 Views

If you do a "man strchr" then you will see it's in the header file not .

It looks like the header file only contains the C++ basic_string class and any necessary headers needed for that class (of which string.his not one of them).


If you need the prototype for the strchr function you need to either

(1) #include
or
(2) #include and then add a "using namespace std;" after it.
Judy
0 Kudos
Reply