Can an Obj-C Class Used with C++?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 33
Reputation: lil_panda is an unknown quantity at this point 
Solved Threads: 0
lil_panda lil_panda is offline Offline
Light Poster

Can an Obj-C Class Used with C++?

 
0
  #1
Feb 22nd, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,639
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Can an Obj-C Class Used with C++?

 
0
  #2
Feb 22nd, 2009
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) ?
  1. // *.cpp file
  2. extern C int myint;

if you have a large group of objects
  1. // *.cpp file
  2. extern C
  3. {
  4. int myint;
  5. // blabla
  6. };
Last edited by Ancient Dragon; Feb 22nd, 2009 at 10:28 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Can an Obj-C Class Used with C++?

 
0
  #3
Feb 23rd, 2009
The question is Objective-C language.
As far as I know, GNU Objective-C++ allows some mix of C++ and Object-C modules (I never used it).
http://en.wikipedia.org/wiki/Objective-C
See also:
http://www.faqs.org/faqs/computer-lang/Objective-C/faq/
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Can an Obj-C Class Used with C++?

 
0
  #4
Feb 23rd, 2009
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
  1. struct myType
  2. {
  3. int a;
  4. float b;
  5. };
  6.  
  7. int f1(myType x)
  8. {
  9. return( x.a);
  10. }

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
  1. struct myType;
  2. int f1(myType);

Now just include the myobj.h in your main cpp program
main.cpp
  1. #include<iostream>
  2. #include "myobj.h"
  3. using namespace std;
  4. int main()
  5. {
  6. some code
  7. return 0;
  8. }

Now compile the main.cpp. In g++ you compile with the compile only flag -c
  1. g++ -c main.cpp
And then you link the main.o and the myobj.o together to make your executable
  1. 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)
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Can an Obj-C Class Used with C++?

 
0
  #5
Feb 23rd, 2009
siddhant3s, it seems your wonderful post does not bear a relation to the original question about Object-C/C++ mixture ...
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Can an Obj-C Class Used with C++?

 
0
  #6
Feb 23rd, 2009
Originally Posted by ArkM View Post
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
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Can an Obj-C Class Used with C++?

 
0
  #7
Feb 23rd, 2009
Never mind!
It was a very sensible post.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 331 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC