Solved

strdup question

Posted on 2004-10-10
8
1,442 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

821 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