?
Solved

makefile & include problems

Posted on 2004-11-03
4
Medium Priority
?
327 Views
Last Modified: 2013-11-18
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 5

Expert Comment

by:van_dy
ID: 12488004
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
0
 
LVL 7

Expert Comment

by:ravs120499
ID: 12488071
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
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12488261
ravs' solution will work, in case you
have the problem i am thinking(c headers
not written with __cplusplus define gaurds)
0
 
LVL 2

Accepted Solution

by:
Peter-Darton earned 2000 total points
ID: 12502609
It looks like your source code is in "test.cpp" whereas you're actually writing C, not C++, so "test.c" would be more appropriate.

You may find that if you rename test.cpp as test.c, the compiler will understand that it really is C code it's building, not C++.

Note: Depending on the link method used in the makefile, this might then lead to issues at the link stage, if the link method is written to only cope with C++ as you'll be linking C code, hence "main" won't be a mangled name.  That can either be dealt with by having a C++ main method (in a .cpp file) instead, or by telling the makefile and/or linker that you're using C not C++.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

809 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