Solved

strdup question

Posted on 2004-10-10
8
1,417 Views
Last Modified: 2012-05-05
What header file is strdup located in and do I need to link to any libs in my project to use it?  I am trying to compile someone else's C code that uses the function and I get this error:

Link error:
Undefined symbol: _strdup

This code compiled and linked as a dll before I exported any functions.  After I added __declspec(dllexport) to some of the functions, I started to get this error.  How can I fix this?
0
Comment
Question by:thedude112286
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 12272434
#include <string.h>
0
 
LVL 4

Author Comment

by:thedude112286
ID: 12272639
To get the code to compile, I had to change calls to strdup into calls to _strdup.  Now it works fine.  Why did this happen?
0
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 250 total points
ID: 12273192
strdup() is not in the ANSI C spec, so Microsoft makes it difficult to use
if you specify strict ANSI to the compiler.  If you look in string.h, you will
likely see a block like this:


/* Nonstandard routines */
#ifndef _ANSI_SOURCE
...
char    *strdup(const char *);
...
#endif

0
 
LVL 1

Expert Comment

by:manojantony
ID: 12277269
try write a function...

#include <string.h>
/*................*/
char* strdup ( const char *str_in )
{
int len = strlen( str_in ) + 1;
char *str_cpy = malloc (len);
if (str_dup == NULL) return NULL;
return ((char *) memcpy (str_dup, str_in, len) );
}
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 45

Expert Comment

by:Kdo
ID: 12286056
Hi Brett,

That's a good post about the Micro$oft ANSI compliance.  I didn't realize that strdup() isn't ANSI and that Micro$oft treated it this way.

Thanks for the tip!
Kent
0
 
LVL 1

Expert Comment

by:manojantony
ID: 12286502
hey .. "strdup into calls to _strdup."
whats the difference ?
0
 
LVL 45

Expert Comment

by:Kdo
ID: 12286598

The compiler prepends an underscore ('_') onto function names (actually, any external, I believe) prior to inserting the name into the object file.

When you compile strdup(), the compiler places _strdup into the object file.  The linker then "sees" the name _strdup and links it.


As I recall, this the compiler's way of "separating" local variables from externals.  The names of the "local" variables remain intact and the externals have the underscore prepended.

Kent
0
 
LVL 1

Expert Comment

by:manojantony
ID: 12286741
thanks Kent
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now