Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good project structure in C

Tags:

I have been working in Java/J2ee projects, in which I follow the Maven structure. I want to develop [say a command line interpreter in linux {ubuntu}] in C. I never develop projects in C. I want to know what project structure I should follow.

like image 385
mohit Avatar asked Apr 09 '10 05:04

mohit


People also ask

What is file structure C?

A FILE is a type of structure typedef as FILE. It is considered as opaque data type as its implementation is hidden. We don't know what constitutes the type, we only use pointer to the type and library knows the internal of the type and can use the data. Definition of FILE is in stdio although it is system specific.

How can I make a project in C?

From the menu bar, click File > New > Project. In the Select a wizard window, expand C/C++, select C++ Project, and then click Next. In the Create a C++ Project window, in the Project name field, enter a name for the C++ project. From the Project type list, select Makefile Project/Empty Project, and then click Finish.


2 Answers

There is no one "standard" for C project in this aspect. Certainly if your project is small, then frequently everything will be placed in a single directory.

You can try to download some popular open-source C projects and take a look at their code.

On a lower level, code should be modular. Each module (which in C is usually manifested in a data structure with a set of functions to act upon it) has its own pair of .h and .c files, with the .h file being the public interface visible to the clients of the module, and the .c file being the private implementation.

like image 112
Eli Bendersky Avatar answered Sep 24 '22 04:09

Eli Bendersky


As Eli Bendersky sayd, it strictly depends on how complex is your project.

The standard suggests to split as much as possible into libraries. The point is that you may want to reuse your libraries elsewhere. By example this is a project of mine:

├── AUTHORS ├── COPYING ├── ChangeLog ├── Makefile.am ├── NEWS ├── README ├── configure.ac ├── libs │   ├── featsel │   │   ├── Makefile.am │   │   ├── commander.c │   │   ├── featsel │   │   │   ├── commander.h │   │   │   ├── feattuple.h │   │   │   └── types.h │   │   ├── featsel.h │   │   ├── feattuple.c │   │   ├── headers │   │   │   └── datatypes.h │   │   └── tests │   │       ├── Makefile.am │   │       └── test00.c │   ├── mbox │   │   ├── Makefile.am │   │   ├── README │   │   ├── control.c │   │   ├── error.c │   │   ├── headers │   │   │   ├── datatypes.h │   │   │   ├── mail.h │   │   │   ├── parse.h │   │   │   ├── split.h │   │   │   └── strings.h │   │   ├── interface.c │   │   ├── mail.c │   │   ├── mbox │   │   │   ├── descriptor.h │   │   │   ├── error.h │   │   │   ├── mail.h │   │   │   └── types.h │   │   ├── mbox.h │   │   ├── parse.c │   │   ├── split.c │   │   └── strings.c │   └── thread_queue │       ├── Makefile.am │       ├── thrdqueue.c │       └── thrdqueue.h ├── reconf └── src     ├── Makefile.am     └── main.c 

I personally prefer to put all libraries into a libs directory. Every library except trivial ones has its own private header directory and exports a public header by means of a directory having the same name of the library.

The source file of the program itself is placed in the src directory.

like image 22
Dacav Avatar answered Sep 24 '22 04:09

Dacav