C++工程(一):学习使用gcc编译C++工程

示例一:HelloWorld

1.1 代码

/*hello_world.cpp*/
#include <iostream>

using namespace std;

int main() {

    cout << "Hello, world!" << endl;
    return 0;
}

1.2 编译

1.3 总结

  • g++ xxx.cpp 默认生成名为a.out的可执行文件
  • g++ xxx.cpp -o exe_filename 生成名为exe_filename的可执行文件
  • Windows的可执行文件一般为.exe文件,Linux下可执行文件并没有文件拓展名
  • 可用ldd命令查看可执行文件的依赖库

示例二:进阶

2.1 vector_example

/*vector_example.cpp*/
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    vector<string> msg = {"Hello", "C++", "World", "from", "VSCode", "and the C++ extension!"};

    for (const string& word : msg) {
        cout << word << " ";
    }
    cout << endl;

    return 0;
}

g++ main.cpp -std=c++11 支持C++11,否则会报错

2.2 thread_example

/*thread_example.cpp*/
#include <iostream>
#include <thread>

int main() {
    std::thread t([](){
        std::cout << "hello world." << std::endl;
    });
    t.join();

    return 0;
}

g++ main.cpp -std=c++11 -lpthread 支持C++11,include pthread(Linux系统)头文件。(包含头文件:-I, 包含lib:-L

三、其他

3.1 Windows系统的C++编译器

  • MSVC (集成在Visual Studio)
  • MinGW-w64: A complete runtime environment for GCC & LLVM for 32 and 64 bit Windows (适用于32/64位Windows系统的完整 GCC & LLVM 运行环境)

3.2 C/C++编译过程

posted @   达可奈特  阅读(139)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示