# Get started with C++

## Compiling with g++

คำสั่ง g++ คือคำสั่งเรียกคอมไพเลอร์ GNU C++ ซึ่งใช้สำหรับการประมวลผลล่วงหน้า การรวบรวม การประกอบ และการเชื่อมโยงซอร์สโค้ดเพื่อสร้างไฟล์ปฏิบัติการ&#x20;

<figure><img src="/files/FcsRt7ApcHfbRqvZr9Pd" alt=""><figcaption><p>Compiler</p></figcaption></figure>

### ![](/files/057vZJyu8rQPOuvsQzMg)![](/files/658OQWYDjk2VOogp0itC)

### Check g++ Version

<pre><code><strong>g++ --version
</strong></code></pre>

<div align="left"><figure><img src="/files/VmhmjHO5AL3a15C3kyPF" alt=""><figcaption><p>ตัวอย่าง</p></figcaption></figure></div>

### Let's start using compiler

```cpp
g++ -c file_name
```

* `g++ -c file_name` ใช้เพื่อคอมไพล์และรวบรวม file\_name เท่านั้น และไม่เชื่อมโยง Object code เพื่อสร้าง execute file. มันจะสร้างไฟล์ file\_name.o ในไดเร็กทอรีการทำงานปัจจุบัน

```cpp
g++ file_name -o target_name
```

* &#x20;g++ file\_name -o target\_name รวบรวมและเชื่อมโยง file\_name และสร้าง executable target file ด้วยตั้งชื่อตาม target\_name (หรือ a.out ตามค่าเริ่มต้น/default)

&#x20;              **ตัวอย่าง**

{% code title="main.cpp" %}

```cpp
#include <iostream>
using namespace std;

int main() {
   cout << "Hello world!!!" << endl;
   return 0;
}
```

{% endcode %}

<div align="left"><figure><img src="/files/pZOTLiLPkiiE1ybEGRio" alt=""><figcaption><p>compiling</p></figcaption></figure></div>

<div align="left"><figure><img src="/files/hRC24RhhDWCvN4t34WTK" alt=""><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="/files/4X3R3bfpVxmdHF6pN42A" alt=""><figcaption></figcaption></figure></div>

## Header file

Header file จะมาช่วยลดความซับซ้อนและจำนวนบรรทัดของโค้ด นอกจากนี้ยังช่วยให้เราได้รับประโยชน์จากการนำฟังก์ชันที่ประกาศไว้ในไฟล์ส่วนหัวกลับมาใช้ใหม่

<figure><img src="/files/dOVOPSPEAg7v57MKCrTx" alt=""><figcaption><p>Ref: <a href="https://www.simplilearn.com/tutorials/cpp-tutorial/guide-to-understand-cpp-header-files">https://www.simplilearn.com/tutorials/cpp-tutorial/guide-to-understand-cpp-header-files</a></p></figcaption></figure>

".h"  เรียกอีกอย่างว่า Header File หมายถึงไฟล์ที่อยู่ส่วนบนของประโยค main() ประกาศเอาไว้ เพื่อเรียกใช้คำสั่งที่ถูกจัดเก็บไว้ใน Header file นั้น ๆ

Ref: <https://www.geeksforgeeks.org/compiling-with-g-plus-plus/>


---

# 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://docs.aic-eec.com/c-c++-for-embedded-programming/principle-c-c++-programming/get-started-with-c++.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.
