Solved

Accessing C++ Compiled libraries.

Posted on 2003-10-23
3
164 Views
Last Modified: 2010-04-15
Hi

I was just wondering if there was anyway to access C++ Libraries through C.

I know that C cannot support the objects that C++ supports, but lets say for example there was a function written in C++ like

int myfunc(int num){
  myClass myclass();
  myclass.printNum();
  return myclass.getNum();
}

Although this serves no useful purpose, I was just wondering, that if you wrapped object handlers up in functions, like in the above, could you use them in C.

What made me curious about this, was that Python is written in C(I believe), and it can Handle QT stuff(which is written in C++).

 
0
Comment
Question by:aolXFT
3 Comments
 
LVL 5

Accepted Solution

by:
mtmike earned 100 total points
ID: 9610978
You can write

extern "C" int myfunc(int num){
  myClass myclass();
  myclass.printNum();
  return myclass.getNum();
}

To make the function callable from C code.

The problem is that C++ allows functions with the same name but different argument types.
0
 
LVL 11

Expert Comment

by:cup
ID: 9612426
To add to mtmike's answer, extern "C" tells the compiler not to mangle the names so that the names can be resolved during the link process.
0
 
LVL 6

Author Comment

by:aolXFT
ID: 9615694
So that's what extern "C" does, thanks mtmike.

Accepting answer now.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now