msvc and strdup?

M

Michael Holm

Ever so often when I try to compile some open source code, I get a error,

pbrtparse.y(205) : error C3861: 'strdup': identifier not found

In the example, I'm trying to compile pbrt (http://www.pbrt.org/).

And I have been searching all over the net for it, and it seems I'm the
only one having a problem with strdup.

What do I do wrong?

I suspect it could be my installation that's bogus, but I'd like to hear
your opinions before formatting my C:\ :)

Any pointers are highly appreciated.

--Michael
 
V

Victor Bazarov

Michael said:
Ever so often when I try to compile some open source code, I get a
error,
pbrtparse.y(205) : error C3861: 'strdup': identifier not found

In the example, I'm trying to compile pbrt (http://www.pbrt.org/).

And I have been searching all over the net for it, and it seems I'm
the only one having a problem with strdup.

What do I do wrong?

You're using non-portable code hoping that it would port easily. It
doesn't, and you get surprised....

'strdup' is a non-standard function. RTFM for a possible replacement.
I suspect it could be my installation that's bogus, but I'd like to
hear your opinions before formatting my C:\ :)

Your C:\ might still benefit from formatting. :)

V
 
M

Michael Holm

You're using non-portable code hoping that it would port easily. It
doesn't, and you get surprised....

Well, I downloaded the win32 version of the source code, with a pre-made
solution for msvc (a .sln file) so I'm not sure that's the correct
explanation. ?

--Michael
 
M

Michael

Ever so often when I try to compile some open source code, I get a error,

pbrtparse.y(205) : error C3861: 'strdup': identifier not found

In the example, I'm trying to compile pbrt (http://www.pbrt.org/).

And I have been searching all over the net for it, and it seems I'm the
only one having a problem with strdup.

What do I do wrong?

<OT>
If your keyboard has an F1 key, perhaps you could use it. If not, get
a new keyboard, or use your mouse to go to the Help menu, and search
for 'C3861'. (MSVC has truly wonderful help functionality, once you
learn to use it.) You'll see that the error message you're getting
means that the identifier 'strdup' has not been declared.
(Admittedly, you might have made an educated guess based on the error
message you're getting.)
</OT>

So in other words, the problem is that the declaration of strdup is
not being seen by the code you're trying to compile.

So where is the declaration of strdup?

At this point, use F1/Help and look up 'strdup', and you'll find that
it's in <string.h>, at least on Windows. Update the code you have to
#include <string.h> (Note: this is different from <string>.)

Now you may get a warning message that strdup is deprecated, and you
should use _strdup instead. If you do, you could potentially read the
help on this warning, or, since you're too pressed for time, here's
the answer: depending on your intentions, you can either ignore the
warning, shut the warning off, or change the code to use _strdup
(perhaps #define strdup _strdup).

Michael
 
V

Victor Bazarov

Michael said:
Well, I downloaded the win32 version of the source code, with a
pre-made solution for msvc (a .sln file) so I'm not sure that's the
correct explanation. ?

This is off-topic here (look for VC++ newsgroup), but are you sure
you're using the _same_version_ of VC++ as the one specified in the
build instructions? 'win32' is not a promise of portability to any
VC++ version...

V
 
M

Michael Holm

Thanks for your answer.

I did press F1, and found this:

Function Information

Minimum DLL Version shlwapi.dll version 4.71 or later
Custom Implementation No
Header shlwapi.h
Import library shlwapi.lib
Minimum operating systems Windows 2000, Windows NT 4.0 with Internet
Explorer 4.0, Windows 98, Windows 95 with Internet Explorer 4.0
Unicode Implemented as ANSI and Unicode versions.


and

Strings.StrDup Method

and

Run-Time Library Reference
strdup, wcsdup

Language Filter: All Language Filter: Multiple Language Filter: Visual
Basic Language Filter: C# Language Filter: C++ Language Filter: J#
Language Filter: JScript
Visual Basic (Declaration)
Visual Basic (Usage)
C#
C++
J#
JScript

These POSIX functions are deprecated beginning in Visual C++ 2005. Use
the ISO C++ conformant _strdup, _wcsdup, _mbsdup instead.



None of which made sense.

So - thanks alot - I'll try again :)

//Michael
 
M

Michael Holm

Hmmm, It may be that I have compiled/installed stlport wrong. When
including <string.h> I get the sltport version of string.h, and not the
windows one.


I don't want to remove stlport, as I use it for some cross-platform
stuff (linux)

Does that spawn any thoughts?

--Michael
 
M

Michael Holm

This is off-topic here (look for VC++ newsgroup), b

Searching my news server, I don't see any "vc" or "vc++" or msvc groups,
do you know one by exact name?

I have no wish to be off-topic, and apologize.

--Michael
 
V

Victor Bazarov

Michael said:
Searching my news server, I don't see any "vc" or "vc++" or msvc
groups, do you know one by exact name?

Connect to 'msnews.microsoft.com' server. They carry all the stuff
you will need.
 
L

Larry Smith

Michael said:
Ever so often when I try to compile some open source code, I get a error,

pbrtparse.y(205) : error C3861: 'strdup': identifier not found

In the example, I'm trying to compile pbrt (http://www.pbrt.org/).

And I have been searching all over the net for it, and it seems I'm the
only one having a problem with strdup.

What do I do wrong?

I suspect it could be my installation that's bogus, but I'd like to hear
your opinions before formatting my C:\ :)

Any pointers are highly appreciated.

--Michael

Microsoft has a _strdup function (notice the leading underscore).
They used to have a "#define" that defined "strdup" as "_strdup",
but in their later compilers they have removed that define.
You could add the define yourself, or you could change the
source code to use _strdup.

Microsoft's latest compilers now issue "deprecated" warnings
for many ANSI/POSIX functions (all of the str... functions
for example). There is a define (_CRT_NONSTDC_NO_DEPRECATE)
you can add to turn off those warnings, or you can use the
"/wd4996" compiler switch.
Microsoft wants folks to use their new, non-standard,
functions in place of the ANSI/POSIX functions.
They say for safety, but if you use their new functions
your code will be Windows-specific (non-portable).

You can search www.msdn.com for "Deprecated POSIX functions"
to find a table listing all of the 100+ C Runtime (CRT) and
POSIX functions that Microsoft is now calling "deprecated".
 
J

James Kanze

You're using non-portable code hoping that it would port easily. It
doesn't, and you get surprised....
'strdup' is a non-standard function. RTFM for a possible replacement.

It's part of the Standard Unix specification, so one could
consider it "standard". Just not the right standard for this
group:).

More to the point, his error message indicates a file with a .y
extension. This suggests code generated by yacc or one of its
relatives (e.g. bison). A priori, I'm not sure that such tools
guarantee "portable" code; it might simply be that he has to run
the tool on his target platform. (The code generated by bison
seems OK, however. No strdup there. Note, however, that by
default, it generates C, and not C++. So there could be
problems in that regard as well.)

Also, because C and C++ forbid the presence of strdup in
<string.h>, but other standards require it, it's presence will
often depend on compiler flags. He should check his compiler
documentation to see if he needs special flags or defines along
the lines of XOPEN_SOURCE, or whatever.
 
J

James Kanze

On May 10, 11:09 pm, Michael Holm <[email protected]>
wrote:

[...]
These POSIX functions are deprecated beginning in Visual C++ 2005. Use
the ISO C++ conformant _strdup, _wcsdup, _mbsdup instead.
None of which made sense.

That last line makes a lot of sense (although technically it's
wrong: strdup is not Posix, but Unix---which is a superset of
Posix). Basically, they've decided to conform to the C/C++
standards, and are deprecating the functions which were
previously in <string.h>, but which don't belong there according
to the C/C++ standards. And are renaming them using a name in
the implementation reserved namespace.

It also means that the functions are still available, so you
should find them, if you include <string.h>. (They're present
in VC++ 2005, although they cause a warning by default.)
 
J

James Kanze

Hmmm, It may be that I have compiled/installed stlport wrong. When
including <string.h> I get the sltport version of string.h, and not the
windows one.
I don't want to remove stlport, as I use it for some cross-platform
stuff (linux)

I'd get rid of the STL port. The quality is considerably less
than that of the standard library delivered with VC++. (FWIW:
standard conforming code, not addressing the system API, seems
to port without problems between g++ 4.x and VC++ 2005.)

For any real portability issues, I'd look for some sort of a
intermediate library to mask them. Something like wxWidgets,
Boost::threads, or whatever you happen to need.
 
J

James Kanze

Microsoft has a _strdup function (notice the leading underscore).

They also have an strdup function, at least in VC 2005.
They used to have a "#define" that defined "strdup" as "_strdup",
but in their later compilers they have removed that define.
You could add the define yourself, or you could change the
source code to use _strdup.
Microsoft's latest compilers now issue "deprecated" warnings
for many ANSI/POSIX functions (all of the str... functions
for example). There is a define (_CRT_NONSTDC_NO_DEPRECATE)
you can add to turn off those warnings, or you can use the
"/wd4996" compiler switch.

The define does NOT turn off the warnings for Posix functions;
only those functions in ISO C which have a replacement in the
ISO C TR.
Microsoft wants folks to use their new, non-standard,
functions in place of the ANSI/POSIX functions.
They say for safety, but if you use their new functions
your code will be Windows-specific (non-portable).

Well, given that the replacement functions are in a TR defined
by the C standards committee, one would hope that they would be
supported by any up to date C compiler. (Of course, in
practice, even C99 is only supported about like C++ 98 with
export. So "up to date C compiler" is probably a contradiction
in itself.)

The function in question, strdup, has never been part of
standard C/C++, and its presence in <string.h> is forbidden by
both ISO standards. In practice, of course... all of the
compilers I have access to do include it, by default at least.
So much for standards compliance.
 
L

Larry Smith

James said:
They also have an strdup function, at least in VC 2005.



The define does NOT turn off the warnings for Posix functions;
only those functions in ISO C which have a replacement in the
ISO C TR.


Well, given that the replacement functions are in a TR defined
by the C standards committee, one would hope that they would be
supported by any up to date C compiler. (Of course, in
practice, even C99 is only supported about like C++ 98 with
export. So "up to date C compiler" is probably a contradiction
in itself.)

The function in question, strdup, has never been part of
standard C/C++, and its presence in <string.h> is forbidden by
both ISO standards. In practice, of course... all of the
compilers I have access to do include it, by default at least.
So much for standards compliance.

--
James Kanze (GABI Software) email:[email protected]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

strdup is a POSIX Standard (IEEE Std 1003.1-2001) function.

As a Unix/Linux developer working with a cross-platform
codebase (Windows, AIX, Solaris, Linux), the POSIX functions
are important to me.
 
D

Default User

Larry Smith wrote:

strdup is a POSIX Standard (IEEE Std 1003.1-2001) function.

As a Unix/Linux developer working with a cross-platform
codebase (Windows, AIX, Solaris, Linux), the POSIX functions
are important to me.


That may be so, but it doesn't make POSIX functions topical here.




Brian
 
J

James Kanze

James Kanze wrote:

[...]
strdup is a POSIX Standard (IEEE Std 1003.1-2001) function.

The must have removed it since then, then, since in the on-line
version of IEEE Std 1003.1-2004, it is documented as an "X/Open
System Interfaces Extension"---standard Unix, but not Posix.
As a Unix/Linux developer working with a cross-platform
codebase (Windows, AIX, Solaris, Linux), the POSIX functions
are important to me.

Linux isn't really Posix compliant (and doesn't claim to be),
although it does support much of both Posix and the X/Open
System extensions. Windows from what I've seen, has a Posix
compatible layer, but it is for all practical purposes
unusable---it's just there to be able to claim Posix compliance
when that is required. And Solaris isn't really Posix compliant
by default, either; you need to use special options with the
compiler.

Posix helps, but it isn't really a guarantee. (In this, it's no
different than the C++ standard, which is ignored by most of the
major vendors whenever they find it convenient.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,126
Latest member
FastBurnketoIngredients
Top