Accessing C++ Compiled libraries.

Posted on 2003-10-23
Last Modified: 2010-04-15

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

Question by:aolXFT

Accepted Solution

mtmike earned 100 total points
ID: 9610978
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.
LVL 11

Expert Comment

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.

Author Comment

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

Accepting answer now.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
posix semaphore deadlock 13 123
libcurl - taking long time on curl_easy_perform 11 408
Problem to ASCII 1 183
c++ substatte a varabe for a string in a LPCTSTR statment 8 86
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-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.

810 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