Link to home
Start Free TrialLog in
Avatar of izomax
izomax

asked on

makefile & include problems

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?
Avatar of van_dy
van_dy

see if  this works

extern "C" {
        char *strdup(const char*);
}

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

#g++ -Wall -ansi -c -g -I./ -I/usr/include -I/usr/include -I/usr/incl -o Test.o Test.cpp  -lc

hope this helps
van_dy
Check string.h - it should have something like this at the top of the file:

#ifdef __cplusplus
  extern "C" {
#endif

and at the end of the file:

#ifdef __cplusplus
}
#endif

If it doesn't work, looks like you have a non-C++ aware include directory. So try this in your source file:

extern "C" {
#include <string.h>
}

(Not certain this will work, but give it a shot.)

If this too doesn't work, then I can't think of any other way than to hand-roll every function prototype you need with an extern "C", as van_dy has written.

- Ravs
ravs' solution will work, in case you
have the problem i am thinking(c headers
not written with __cplusplus define gaurds)
ASKER CERTIFIED SOLUTION
Avatar of Peter-Darton
Peter-Darton

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial