[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

makefile & include problems

Posted on 2004-11-03
7
Medium Priority
?
268 Views
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");
    free(x);
}

/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?
0
Comment
Question by:izomax
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 672 total points
ID: 12487859
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__;
#endif
0
 
LVL 2

Assisted Solution

by:pb_india
pb_india earned 664 total points
ID: 12488465
Try this:

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


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

    char * x = strdup(buffer);
    free(x);
}

0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 664 total points
ID: 12490850
Lose the -ansi switch and you'll be alright.

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

Expert Comment

by:rstaveley
ID: 12694884
Points to rstaveley.
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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

867 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