Skip to content

C++ 环境设置

Visual Studio Setup (Windows)

Installation Steps

  1. Download Visual Studio

  2. Install C++ Workload

    • During installation, select "Desktop development with C++"
    • This installs:
      • MSVC compiler
      • Standard Library
      • Windows SDK
      • Debugging tools
  3. Verify Installation

    cpp
    // Create test.cpp
    #include <iostream>
    
    int main() {
        std::cout << "C++ environment setup successful!" << std::endl;
        return 0;
    }

Compile and Run

Using Developer Command Prompt:

bash
cl test.cpp
test.exe

GCC Setup (Linux/macOS)

Linux Installation

Ubuntu/Debian:

bash
sudo apt update
sudo apt install build-essential gdb

Fedora:

bash
sudo dnf install gcc-c++ gdb

Arch Linux:

bash
sudo pacman -S base-devel gdb

macOS Installation

Using Homebrew:

bash
brew install gcc

Using Xcode Command Line Tools:

bash
xcode-select --install

Verify Installation

bash
g++ --version
gdb --version

Compile and Run

bash
g++ -o test test.cpp
./test

Online IDEs

  1. Compiler Explorer - godbolt.org

    • Real-time compilation
    • Multiple compilers support
    • Assembly output
  2. Replit - replit.com

    • Full development environment
    • Collaboration features
    • GitHub integration
  3. OnlineGDB - onlinegdb.com

    • Built-in debugger
    • Simple interface

IDE Recommendations

Visual Studio Code

  1. Install Extensions:

    • C/C++ (Microsoft)
    • C/C++ Extension Pack (Microsoft)
    • Code Runner (formulahendry)
  2. Configure Settings:

    json
    {
        "C_Cpp.default.compilerPath": "/usr/bin/g++",
        "C_Cpp.default.cppStandard": "c++17"
    }

CLion

  • Professional IDE by JetBrains
  • Built-in CMake support
  • Advanced debugging
  • Cross-platform

Project Setup

Basic Project Structure

my_project/
├── include/          # Header files
├── src/             # Source files
├── lib/             # Libraries
├── tests/           # Test files
├── build/           # Build output
└── CMakeLists.txt   # CMake configuration

CMake Configuration

cmake
cmake_minimum_required(VERSION 3.10)
project(MyProject)

set(CMAKE_CXX_STANDARD 17)

include_directories(include)
add_executable(myapp src/main.cpp)

Build Systems

Using Make

makefile
CC = g++
CFLAGS = -Wall -std=c++17

main: main.o
    $(CC) $(CFLAGS) -o main main.o

main.o: main.cpp
    $(CC) $(CFLAGS) -c main.cpp

clean:
    rm -f *.o main

Using CMake

bash
mkdir build
cd build
cmake ..
make

Common Build Flags

Compiler Flags

bash
# Standard version
-std=c++17

# Warnings
-Wall -Wextra -Wpedantic

# Optimization
-O0 (no optimization)
-O1
-O2
-O3 (maximum optimization)
-Os (optimize for size)

# Debug information
-g

# Debug build
-DDEBUG

Example Compile Command

bash
g++ -std=c++17 -Wall -Wextra -g -o myapp main.cpp

Debugging Setup

Using GDB

bash
g++ -g -o myapp main.cpp
gdb myapp

Common GDB Commands:

(gdb) break main
(gdb) run
(gdb) next
(gdb) step
(gdb) print variable_name
(gdb) continue
(gdb) quit

Using Visual Studio Debugger

  • Set breakpoints by clicking the margin
  • Press F5 to start debugging
  • Use F10 (step over) and F11 (step into)
  • Watch window shows variables

Common Issues and Solutions

Issues

  1. 'g++' is not recognized

    • Solution: Add compiler to PATH
  2. Linker errors

    • Solution: Link required libraries with -l flag
  3. Permission denied

    • Solution: chmod +x your_program (Unix)
  4. Library not found

    • Solution: Use -I for include paths and -L for library paths

Getting Help

bash
g++ --help
man gcc

Next Steps

  1. Create your first program
  2. Practice basic syntax
  3. Learn debugging techniques
  4. Explore standard library
  5. Build a small project

Resources

Content is for learning and research only.