Computing.Net > Forums > Programming > Using a c++ lib from a c program.

Using a c++ lib from a c program.

Reply to Message Icon

Original Message
Name: Ryan
Date: October 26, 2002 at 21:55:23 Pacific
Subject: Using a c++ lib from a c program.
OS: Redhat 7.3
CPU/Ram: 400Mhz/64
Comment:

Greetings,

I would like to use a c++ static library from within a c program.

It works if i use gcc and rename the .c files to .cpp, however, I am not sure if this is a good solution. Is there a better way of doing this?

Thanks in advance.


Report Offensive Message For Removal


Response Number 1
Name: Don Arnett
Date: October 26, 2002 at 23:09:45 Pacific
Reply: (edit)

Sounds like a perfectly good way to me. C is a subset of C++. Anything that's valid in C is valid in C++.

I programmed in C for ten years before learning any C++, so I still use C I/O (printf,scanf, etc) rather than C++ I/O (cin, cout, etc). Some of my "C++" programs could pass for C. They're C++ only because I use a C++ compiler.

I have had at times to use a C library in a C++ program. As I think about it, it might not be possible to use a C++ library with a C compiler because a strictly C compiler wouldn't know how to handle connecting to C++ routines.

If I remember correctly, to get a C function to work in our C++ programs, we did something like this around the function prototypes:

extern C {

void cFunction1();
char *cFunction2();

}

That told the C++ compiler that these functions were C, not C++.

We've only done this for third party libraries that didn't have C++ versions.


Report Offensive Follow Up For Removal

Response Number 2
Name: JSLover
Date: October 29, 2002 at 00:04:51 Pacific
Reply: (edit)

Can't you GetProcAddress & call a function in any dll? Then the compilier don't need to understand. or does it?


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Using a c++ lib from a c program.

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge