C++ 环境设置
Visual Studio Setup (Windows)
Installation Steps
Download Visual Studio
- Visit visualstudio.microsoft.com
- Download the Community edition (free)
Install C++ Workload
- During installation, select "Desktop development with C++"
- This installs:
- MSVC compiler
- Standard Library
- Windows SDK
- Debugging tools
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.exeGCC Setup (Linux/macOS)
Linux Installation
Ubuntu/Debian:
bash
sudo apt update
sudo apt install build-essential gdbFedora:
bash
sudo dnf install gcc-c++ gdbArch Linux:
bash
sudo pacman -S base-devel gdbmacOS Installation
Using Homebrew:
bash
brew install gccUsing Xcode Command Line Tools:
bash
xcode-select --installVerify Installation
bash
g++ --version
gdb --versionCompile and Run
bash
g++ -o test test.cpp
./testOnline IDEs
Recommended Online Compilers
Compiler Explorer - godbolt.org
- Real-time compilation
- Multiple compilers support
- Assembly output
Replit - replit.com
- Full development environment
- Collaboration features
- GitHub integration
OnlineGDB - onlinegdb.com
- Built-in debugger
- Simple interface
IDE Recommendations
Visual Studio Code
Install Extensions:
- C/C++ (Microsoft)
- C/C++ Extension Pack (Microsoft)
- Code Runner (formulahendry)
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 configurationCMake 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 mainUsing CMake
bash
mkdir build
cd build
cmake ..
makeCommon 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
-DDEBUGExample Compile Command
bash
g++ -std=c++17 -Wall -Wextra -g -o myapp main.cppDebugging Setup
Using GDB
bash
g++ -g -o myapp main.cpp
gdb myappCommon GDB Commands:
(gdb) break main
(gdb) run
(gdb) next
(gdb) step
(gdb) print variable_name
(gdb) continue
(gdb) quitUsing 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
'g++' is not recognized
- Solution: Add compiler to PATH
Linker errors
- Solution: Link required libraries with
-lflag
- Solution: Link required libraries with
Permission denied
- Solution:
chmod +x your_program(Unix)
- Solution:
Library not found
- Solution: Use
-Ifor include paths and-Lfor library paths
- Solution: Use
Getting Help
bash
g++ --help
man gccNext Steps
- Create your first program
- Practice basic syntax
- Learn debugging techniques
- Explore standard library
- Build a small project