mahmood
mahmood

Reputation: 24805

How can I build mixed C/C++ code together?

I have a .cc file which uses both iostream and malloc. How can I compile that? Using g++, it says

error: 'malloc' was not declared in this scope

using gcc, it says

fatal error: iostream: No such file or directory

The source code is located at http://sequitur.info/sequitur_simple.cc

I changed malloc to new and chaned free to delete, but I still get a lot of errors. For example

/usr/include/c++/4.6/new:103:14: error: initializing argument 2 of âvoid* operator new(std::size_t, void*)â [-fpermissive]

Upvotes: 0

Views: 220

Answers (3)

Karthik T
Karthik T

Reputation: 31972

Either include <stdlib.h> or include <cstdlib> and change malloc to std::malloc - compile with g++. Including <cstdlib> is the prefered way for new C++ code, "name.h" style is deprecated in C++.

While this will fix you problem, it might be a better idea to migrate to new/delete, to be more consistantly C++.

Upvotes: 5

Josh Petitt
Josh Petitt

Reputation: 9589

use new and delete in C++ code. Don't mix new and malloc. From the code you posted, there isn't any reason AFAIK you can't use new and delete

Upvotes: 0

ChaosCakeCoder
ChaosCakeCoder

Reputation: 367

Have you tried to include

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

and use g++?

Upvotes: 0

Related Questions