Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

strdup question

Posted on 2004-10-10
8
Medium Priority
?
1,483 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 46

Expert Comment

by:Kent Olsen
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 46

Expert Comment

by:Kent Olsen
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

688 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