Is there a way for me to use an obj-C class with my C++ program? If so, what special parameters do I have to specify during the compilation phase, and if I needed, changes to my source code files?:-/

Recommended Answers

All 6 Replies

there is no such thing as an "obj-c" class. C language knows nothing about classes. Maybe you mean a structure object declared in a c translation unit (*.c program) ?

// *.cpp file
extern C int myint;

if you have a large group of objects

// *.cpp file
extern C
{
    int myint;
    // blabla
};

What I am guessing is that you dont have the source of of the .obj(or .o) file.
Don't worry. If you know what all prototypes where there in the source file, you can create a header file which have all the prototypes and then compile it. It will compile easily. But when you are linking, be sure to link the object file along the header file and your main source file.
I know its getting bit confusing so heres a brief example.
Lets say the object file contains a structure and a function

someobj.c

struct myType
{
int a;
float b;
};

int f1(myType x)
{
return( x.a);
}

But you dont have the code have the code of the above file right? You just have the precompiled obj file( which will be a .o file or a .obj file).

Now I want you to create a new file containing the prototype(not the definition) of all the data structure and function used. In our example this will be:
myobj.h

struct myType;
int f1(myType);

Now just include the myobj.h in your main cpp program
main.cpp

#include<iostream>
#include "myobj.h"
using namespace std;
int main()
{
some code 
return 0;
}

Now compile the main.cpp. In g++ you compile with the compile only flag -c

g++ -c  main.cpp

And then you link the main.o and the myobj.o together to make your executable

g++ -o mainProgram.exe main.o myobj.o

And you are done;

Note that you can avoid writing the myobj.h by directly declaring the prototype in your main program.

I hope it helps

Also note that your code of the myobj.o i.e myobj.c should not contain any c++ incompatible code. Also, the object file should have been of the same platform as your program(i.e. it should not been that you compiled the .obj file on a Unix and now try to link in a windows box)

siddhant3s, it seems your wonderful post does not bear a relation to the original question about Object-C/C++ mixture ;)...

siddhant3s, it seems your wonderful post does not bear a relation to the original question about Object-C/C++ mixture ;)...

Oh sh*t,
I thought he meant it as object files of C,
:-/

Anyways. I wasted typing so much :@ I guess someone would be helped while google indexes dani very efficiently!!!
Its upto the moderator to do whatever with my post:
stupid me:$

Never mind! ;)
It was a very sensible post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.