# Code Generation

## Introduction

Code generation is always a tricky way of producing code, as the code is generated by a tool (seen as a black-box, behaviour not modifiable) from a model and the obtained code slightly differs from what could have been obtained manually. Some rules have to be considered to avoid desesparation while trying to get expected code:

* **a specific code generator is required for each target platform** . Hence thinking that a single code generator would do the job for all your projects will generate massive frustration when linking obtained code with third party code.
* **know your code generator capabilities** , define properly your software interface and how data/functions are implemented (full force implementation or basic machine)

## Generating C source code

Generating C source code requires a software development project. You should choose this option when you create your project. It is not possible to change the nature of a project after its creation.

![Example of a project status](/files/fX8dJmP6HFpBrWof3oqq)

Code can be generated in two modes:

* **component mode**: you select a component in the component view, you right-click then select "code generator". The dialog below shows up. After having selected the proper translation profile, click OK. Unless you got error messages, the code has been generated into the lang/c directory of your project. If you select a machine, you get a c file and a header file (.h) with the same name as the selected component.

![Component code generation dialog](/files/rZqlMk5v1xvlFmtrDVtz)

* **project mode**: you select your project in the workspaces view, you right-click the select "code generator". The dialog below shows up. After having selected the proper translation profile, the top-level component and possibly checked the "generate main" option, click OK.

![Project code generation dialog](/files/WWORin27X9CBL5Jo1I4E)

Several translation profiles are available:

* C9X
* Project
* Light
* 01
* PIC


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://b-method.gitbook.io/training-resources-for-atelier-b/guides-and-tutorials/applying-the-b-method/code-generation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
