You are not logged in.

#1 2010-05-12 20:12:25

pseudonomous
Member
Registered: 2008-04-23
Posts: 349

Calling Unix/Linux C functions in C++ code

Hi Everybody,

I've been working on teaching myself to program in C++ for awhile, I'm no expert but I have some very basic understanding of the language; I'm much better at shell programming.  Recently I've run into a situation where I want to write a menu-driven interface to calling some korn-shell scripts on OpenBSD.  While I could actually do this completely in ksh, it would look a lot prettier, I think, if I used ncurses instead ... so I've been trying to find some documentation on how one does system calls and what-not from C++ and I've found lots of documentation on how to do this sort of thing in C, and lots of terse documentation on how to compile C++ code on unix systems, but I really haven't found much on how to do this sort of thing in C++.

I'm wondering if anybody could point me in the right direction ... do I need to research how to make function calls from C++ programs to C libraries?  Is this just SO easy that no-one has bothered to document it (I found an FAQ about mixing C and C++ code, but I don 't really want to mix C and C++ code, besides the function calls)?  Do I really just need to learn how to program in C?

Thanks in advance for any help you can give me, I'm sorry that I'm so clueless.

Offline

#2 2010-05-12 20:27:35

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: Calling Unix/Linux C functions in C++ code

Roughly speaking, C++ is a superset of C.  So, almost any code that works in C works in C++.  To use ncurses, you literally #include <curses.h> and call its functions (initscr(), and whatever else -- I've never used it).

Offline

#3 2010-05-12 20:42:39

pseudonomous
Member
Registered: 2008-04-23
Posts: 349

Re: Calling Unix/Linux C functions in C++ code

Okay, cool.  I guess I'll just start hacking away at it, then.

Offline

#4 2010-05-15 20:17:27

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

Re: Calling Unix/Linux C functions in C++ code

C++ can call on C code as long as C++ is given instructions that the function prototypes are in fact C functions, and not C++ functions.
You have to do this since C and C++-s scheme of handling function names at the object code level are different. By invoking the extern "C" {}, the compiler will generate object code that can link against C code.

You have maybe seen C header files with

#ifdef __cplusplus
extern "C" {
#endif

//... prototypes here

#ifdef __cplusplus
}
#endif

Last edited by Themaister (2010-05-15 20:19:35)

Offline

Board footer

Powered by FluxBB