Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1494
  • Last Modified:

strdup question

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
thedude112286
Asked:
thedude112286
1 Solution
 
ozoCommented:
#include <string.h>
0
 
thedude112286Author Commented:
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
 
brettmjohnsonCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
manojantonyCommented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
manojantonyCommented:
hey .. "strdup into calls to _strdup."
whats the difference ?
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
manojantonyCommented:
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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now