I see alot of libraries written in C and I like C++ OOP way.
How can I use a given C library functions in C++?

Recommended Answers

All 9 Replies

you have to declare them extern "C".

#ifdef  __cplusplus
extern "C" {
#endif
// C function prototypes here
#ifdef  __cplusplus
}
#endif

You will find this example in stdio.h as well as other c header files that may also be used in c++ functions.

I see alot of libraries written in C and I like C++ OOP way.
How can I use a given C library functions in C++?

I don't understand your question. All you need to do is include the header file and perhaps link with the library if its something unusual that isn't loaded by default. After all, C++ includes most of C.

For the most part, you can take any basic Win32 Sdk template type program usually associated with C style programming and rebuild the project in whatever development environment you are using as a C++ project; i.e., with .cpp extension instead of .c; and you automatically have the capability to do whatever OOP stuff you wish.

Essentially, that's how I program myself. A good deal of C++ I simply don't like; I have no use for it; I don't use it now; and have no intention of ever using it. I'm speaking most particularly of GUI class frameworks. However, there is a good bit of C++ that I do like so that's the part I use.

Perhaps an eclectic style, but it works for me. That's why your question confuses me. I must be misunderstanding your question because you surely know that.

>>I'm speaking most particularly of GUI class frameworks.
There are no GUI frameworks in standard c++. You must be thinking of some 3d party libraries, such as wxWidgets, MFC, OpenGL, DirectX, etc. etc. None of them are part of standard c++. Maybe you should read a book about c++ to find out what all is included in the standard.

As for the op's question, he wants to know how to call C functions from C++. The answer is that c++ programmers do that all the time, such as by using any of the functions in stdio.h, string.h, stdlib.h, win32 api functions, etc (there are hundreds of them). All those functions are written in C language, and all be called by C++. Its just a matter of telling the c++ compiler that the functions are in fact C functions, not C++, so that the compiler will not mangle the function names.

#include<conio.h>
getch();

I didn't knew the question was confusing. However some geniuses have decoded it correctly. Here I go again. Suppose I want to use bass.dll which have functions written in C, is there special thing i need to include or just give it a go?

Thanks for suggestions and link. I really learn here at DW

I didn't knew the question was confusing. However some geniuses have decoded it correctly. Here I go again. Suppose I want to use bass.dll which have functions written in C, is there special thing i need to include or just give it a go?

Thanks for suggestions and link. I really learn here at DW

#include "bass.inc"

make sure bass.dll is somewhere in your dll search path. The same directory as your exe would work ok.

I didn't knew the question was confusing. However some geniuses have decoded it correctly. Here I go again. Suppose I want to use bass.dll which have functions written in C, is there special thing i need to include or just give it a go?

Thanks for suggestions and link. I really learn here at DW

I think everyone has already answered that question. First, create function prototypes for the functions your c++ program needs to call, surround them with extern "C", then put the DLL somewhere the OS can find it, such as one of the directories in your PATH environment variable, or in the current working directory.

Another way to do it is with dynamic loading -- First call LoadLibrary(), then GetProcAddress(), which will return a function pointer.

It was for anybody I did confuse.
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.