0

I have a query for creating c++ object inside c file.

I have the sample code below. When trying to import the CPlusHeader it throws an error which i could not understand.

The error is iostream' file not found as one of the error. How could i resolve this issue.

Regards, Lenin

CPlusFile.h

include iostream

include string

using namespace std;

class  CPlusFile {
  int data;
public:
  CPlusFile();
  int getData();
};

CPlusFile.cpp

CPlusFIle::CPlusFIle() {
  data = 10;
}

int CPlusFile::getData() {
  return data;
}

CFile.h

int doSomething();

CFile.c

include "CFile.h"

include "CPlusFile.h"

int doSomething() {
  CPlusFile object; 

}
boom
  • 5,856
  • 24
  • 61
  • 96

5 Answers5

3

It strongly depends on what you call a "C file". Previous answer assumed that you meant a file with a .c suffix. I assume here that you mean a file that shall be compiled with a C compiler.

If my assumption is valid, then the answer is simple: You cannot instantiate C++ classes in a C file. What you can do, though, is call C++ static methods from the C code. Please refer, for example, to In C++ source, what is the effect of extern "C"? to see how to do this.

Community
  • 1
  • 1
Roland Sarrazin
  • 1,203
  • 11
  • 29
  • By the way your code cannot compile. `include` shall read `#include`, sometimes you use `CPlusFIle` instead of `CPlusFile`, etc. – Roland Sarrazin Oct 21 '13 at 09:29
  • Hi the compiler is Apple LLVM 5.0 which is default compiler for c/c++/Objective - c/objective c++. I can include c header using extern "C" { c api } – boom Oct 21 '13 at 09:32
  • One more good source: http://www.parashift.com/c++-faq/mixing-c-and-cpp.html (How to mix C and C++). – Roland Sarrazin Oct 21 '13 at 09:36
  • Okay now i can include c++ header. But my confusion is how can i create c++ object in c file. if not possible then please let me know the alternative way. It's giving compilation error when i did some thing like this. CPlusFIle object; – boom Oct 21 '13 at 09:47
  • Please provide a description of what you mean with "c file". We might then elaborate a scenario fulfilling your expectation. I might repeat myself: You cannot compile a C++ class with a C compiler. – Roland Sarrazin Oct 21 '13 at 09:53
  • c file means file with .c extension – boom Oct 21 '13 at 10:03
  • Ok I understand. I guess every compiler shall be able to compile a file of any extension as a C++ file but to be honest I don't know how. I would simply use the normal ".cpp" extension and everything would be fine, wouldn't it? – Roland Sarrazin Oct 21 '13 at 13:58
1

First of all, it is

#include <iostream>
#include <string>

and not

include iostream
include string

Second, if CFile.c is compiled as C, then this will not work. The C compiler will not understand the class keyword and you cannot create an instance of a class in C.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
0

iostream is a C++ header, and isn't available if you're compiling using a C compiler. You can write C++ code in a .c file, you just need to use the right compiler.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Hi the compiler is Apple LLVM 5.0 which is default compiler for c/c++/Objective - c/objective c++. I can include c header using extern "C" { c api } – boom Oct 21 '13 at 09:31
0

You can use this in your C++ header file to check wether you are going to include it from C or C++ code:

#ifdef __cplusplus

The includes iostream and others, as well as using class, are only available for C++ code.

But if you want to use the CPlusFile class, which is a C++ class, you can only do that in C++ code. Best is to rename your CFile.c to CFile.cpp.

Albert
  • 65,406
  • 61
  • 242
  • 386
0

Yes, it is possible to call C++ object inside the C file. Here I performed a scenario and
it's working fine for me.

 CPlusFile.h 

  #include<iostream>
  #include<string>
     using namespace std;
       class cplus{

                int data;
                public:
                       cplus();
                        int getdata();

       };

   CPlusFile.cpp

      #include "cplusfile.h"
       cplus::cplus(){  data =10; }
       int cplus::getdata(){ return data; }


   CFile.h

     #include "cplusfile.h"
     #include<stdio.h>
           int dosomething();

 CFile.c

       #include "cfile.h"
        int dosomething(){

                cplus c;
                  printf("%d",c.getdata());


           }

     int main() {
                dosomething(); 
                return 0;
          }



     And compile this by g++ CFile.c CPlusFile.cpp and it works fine.
Birendra Kumar
  • 431
  • 1
  • 7
  • 18