mixing c++ and c

P

puzzlecracker

I am writing a C++ program under linux and one of the modules uses
c for file system calls to traverse and retrieve files, etc. I am
thinking of writing that module purely in C and linking into my program
(would someone argue that I should still complile it in c++?). How is
it done?


would below snippet work?


//cpp file


include<filestat.h>
//regular c++ includes.


using std::cout;
using std:string;



int main(int argc, char* argv []){


// my program;


}


//filestat.h


#ifndef FILESTAT_H
#define FILESTAT_H

extern "C"
{

//all c functions


}

#endif


Unsure how many of you here use g++ as a compiler of choice, but were I
to compile the module above with it, would I be able to link that part
against C program or should I avoid extern "C" business as outdate and
soon to be deprecated?

Thanks.
 
P

paulius-maruska

//filestat.h
#ifndef FILESTAT_H
#define FILESTAT_H

extern "C"
{

//all c functions


}

#endif
It is usualy good idea to put
#ifdef __cplusplus
#endif
on the extern "C". So your filestat.h would look like this:
#ifndef FILESTAT_H
#define FILESTAT_H

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

//all c functions

#ifdef __cplusplus
}
#endif // __cplusplus

#endif // FILESTAT_H
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top