• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

Accessing C++ Compiled libraries.


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();
  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++).

1 Solution
You can write

extern "C" int myfunc(int num){
  myClass myclass();
  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.
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.
aolXFTAuthor Commented:
So that's what extern "C" does, thanks mtmike.

Accepting answer now.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now