makefile & include problems

Posted on 2004-11-03
Last Modified: 2010-04-01
Can someone please shed light onto the following problem I am having.

If I use the makefile from assignment#1 even simple
things fail.e.g.

#include <string.h>
#include <stdlib.h>

void useless()
    char * x = strdup("hello");

/usr/bin/g++ -Wall -ansi -c -g -I./ -I/usr/include -I/usr/include -I/usr/incl
-o Test.o Test.cpp
Test.cpp: In function `void useless()':
Test.cpp:6: : implicit declaration of function `int strdup(...)'
Test.cpp:6: : initialization to `char *' from `int' lacks a cast

The problem appears to be when using the C functions from C++

Is there a way to bypass or fix?
Question by:izomax
    LVL 86

    Accepted Solution

    Check your 'string.h' - maybe 'strdup()' is hidden by conditional compilation statements. E.g. here I have

    #if defined __USE_SVID || defined __USE_BSD || defined __USE_XOPEN_EXTENDED
    /* Duplicate S, returning an identical malloc'd string.  */
    extern char *strdup (__const char *__s) __THROW __attribute_malloc__;
    LVL 2

    Assisted Solution

    Try this:

    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>

    void useless()
        char buffer[] = "hello";

        char * x = strdup(buffer);

    LVL 17

    Assisted Solution

    Lose the -ansi switch and you'll be alright.

    strdup isn't portable. It only conforms to SVID 3 and BSD 4.3.
    LVL 17

    Expert Comment

    Points to rstaveley.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    733 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

    22 Experts available now in Live!

    Get 1:1 Help Now